Skip to content

Commit 25c0048

Browse files
committed
cmd/shfmt: report correct language read from EditorConfig in errors
We had logic to catch mismatching language errors and report what language was currently being used for parsing, following the -ln flag. However, this logic completely ignored EditorConfig, which can also control this behavior via the shell_variant property. The logic to determine what language to use for parsing was correct, but the logic to report a reasonable error to the user was not. Fixes #1102.
1 parent 21e38aa commit 25c0048

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

cmd/shfmt/main.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,9 @@ var ecQuery = editorconfig.Query{
359359
RegexpCache: make(map[string]*regexp.Regexp),
360360
}
361361

362-
func propsOptions(lang syntax.LangVariant, props editorconfig.Section) {
362+
func propsOptions(lang syntax.LangVariant, props editorconfig.Section) (_ syntax.LangVariant, validLang bool) {
363363
// if shell_variant is set to a valid string, it will take precedence
364-
lang.Set(props.Get("shell_variant"))
364+
langErr := lang.Set(props.Get("shell_variant"))
365365
syntax.Variant(lang)(parser)
366366

367367
size := uint(0)
@@ -380,6 +380,8 @@ func propsOptions(lang syntax.LangVariant, props editorconfig.Section) {
380380
syntax.KeepPadding(props.Get("keep_padding") == "true")(printer)
381381
// TODO(v4): rename to func_next_line for consistency with flags
382382
syntax.FunctionNextLine(props.Get("function_next_line") == "true")(printer)
383+
384+
return lang, langErr == nil
383385
}
384386

385387
func formatPath(path string, checkShebang bool) error {
@@ -446,12 +448,13 @@ func editorConfigLangs(l syntax.LangVariant) []string {
446448
}
447449

448450
func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
451+
fileLangFromEditorConfig := false
449452
if useEditorConfig {
450453
props, err := ecQuery.Find(path, editorConfigLangs(fileLang))
451454
if err != nil {
452455
return err
453456
}
454-
propsOptions(fileLang, props)
457+
fileLang, fileLangFromEditorConfig = propsOptions(fileLang, props)
455458
} else {
456459
syntax.Variant(fileLang)(parser)
457460
}
@@ -466,6 +469,9 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
466469
node, err = parser.Parse(bytes.NewReader(src), path)
467470
if err != nil {
468471
if s, ok := err.(syntax.LangError); ok && lang.val == syntax.LangAuto {
472+
if fileLangFromEditorConfig {
473+
return fmt.Errorf("%w (parsed as %s via EditorConfig)", s, fileLang)
474+
}
469475
return fmt.Errorf("%w (parsed as %s via -%s=%s)", s, fileLang, lang.short, lang.val)
470476
}
471477
return err

cmd/shfmt/testdata/script/editorconfig.txtar

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ exec shfmt
1212

1313
stdin stdin-filename-bash
1414
! exec shfmt -filename=foo_posix.sh
15-
# TODO: we didn't parse as bash!
16-
stderr '^foo_posix.sh:.* arrays are a bash.*parsed as bash via -ln=auto'
15+
stderr '^foo_posix.sh:.* arrays are a bash.*parsed as posix via EditorConfig'
1716

1817
stdin stdin-filename-bash
1918
! exec shfmt -filename=${WORK}/foo_posix.sh
20-
# TODO: we didn't parse as bash!
21-
stderr ^${WORK@R}/'foo_posix.sh:.* arrays are a bash.*parsed as bash via -ln=auto'
19+
stderr ^${WORK@R}/'foo_posix.sh:.* arrays are a bash.*parsed as posix via EditorConfig'
2220

2321
# Using a file path should use EditorConfig, including with the use of flags
2422
# like -l.

0 commit comments

Comments
 (0)