Skip to content

Commit 86e7671

Browse files
committed
fix!: options in .editorconfig not work with shfmt
Must pass --filename option to shfmt. When shfmt read content from stdin, it doesn't know the filename and its extension. So it won't match the patterns "[*.sh]" and "[*.bash]" in .editorconfig. Do not pass any Parser and Printer options like -i/-p/-bn/-l. It will cause the .editorconfig not to be loaded. See https://github.com/mvdan/sh/blob/23633a432f903599a4ce46c30c4337e413a26ef1/cmd/shfmt/main.go#L186-L196 Breaking Change: Removed shfmtConfig options. Use the .editorconfig options instead of. The .editorconfig options of shfmt refer to https://github.com/mvdan/sh/blob/master/cmd/shfmt/shfmt.1.scd#examples
1 parent 04a2cb3 commit 86e7671

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

server/src/shfmt/index.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { basename, relative } from 'node:path'
2+
import { format } from 'node:util'
3+
14
import { spawn } from 'child_process'
25
import * as LSP from 'vscode-languageserver/node'
36
import { TextDocument, TextEdit } from 'vscode-languageserver-textdocument'
@@ -41,9 +44,7 @@ export class Formatter {
4144
formatOptions?: LSP.FormattingOptions | null,
4245
shfmtConfig?: Record<string, string | boolean> | null,
4346
): Promise<TextEdit[]> {
44-
const documentText = document.getText()
45-
46-
const result = await this.runShfmt(documentText, formatOptions, shfmtConfig)
47+
const result = await this.runShfmt(document, formatOptions, shfmtConfig)
4748

4849
if (!this._canFormat) {
4950
return []
@@ -61,16 +62,21 @@ export class Formatter {
6162
}
6263

6364
private async runShfmt(
64-
documentText: string,
65+
document: TextDocument,
6566
formatOptions?: LSP.FormattingOptions | null,
6667
shfmtConfig?: Record<string, string | boolean> | null,
6768
): Promise<string> {
68-
const indentation: number = formatOptions?.insertSpaces ? formatOptions.tabSize : 0
69-
const args: string[] = [`-i=${indentation}`] // --indent
70-
if (shfmtConfig?.binaryNextLine) args.push('-bn') // --binary-next-line
71-
if (shfmtConfig?.caseIndent) args.push('-ci') // --case-indent
72-
if (shfmtConfig?.funcNextLine) args.push('-fn') // --func-next-line
73-
if (shfmtConfig?.spaceRedirects) args.push('-sr') // --space-redirects
69+
// documentText: string,
70+
const documentText = document.getText()
71+
const documentUri = document.uri
72+
let filepath = documentUri.substring(7) // trim "files://"
73+
filepath = relative(this.cwd, filepath)
74+
75+
// Do not pass any Parser and Printer options like -i/-p/-bn/-l. It will cause the .editorconfig not to be loaded.
76+
// See https://github.com/mvdan/sh/blob/23633a432f903599a4ce46c30c4337e413a26ef1/cmd/shfmt/main.go#L186-L196
77+
const args: string[] = [
78+
`--filename=${filepath}`, // Must set filename for matching the rules in .editorconfig.
79+
]
7480

7581
logger.debug(`Shfmt: running "${this.executablePath} ${args.join(' ')}"`)
7682

0 commit comments

Comments
 (0)