Skip to content

Commit

Permalink
cmd/shfmt: report correct language read from EditorConfig in errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvdan committed Oct 19, 2024
1 parent 21e38aa commit 25c0048
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 9 additions & 3 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ var ecQuery = editorconfig.Query{
RegexpCache: make(map[string]*regexp.Regexp),
}

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

size := uint(0)
Expand All @@ -380,6 +380,8 @@ func propsOptions(lang syntax.LangVariant, props editorconfig.Section) {
syntax.KeepPadding(props.Get("keep_padding") == "true")(printer)
// TODO(v4): rename to func_next_line for consistency with flags
syntax.FunctionNextLine(props.Get("function_next_line") == "true")(printer)

return lang, langErr == nil
}

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

func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
fileLangFromEditorConfig := false
if useEditorConfig {
props, err := ecQuery.Find(path, editorConfigLangs(fileLang))
if err != nil {
return err
}
propsOptions(fileLang, props)
fileLang, fileLangFromEditorConfig = propsOptions(fileLang, props)
} else {
syntax.Variant(fileLang)(parser)
}
Expand All @@ -466,6 +469,9 @@ func formatBytes(src []byte, path string, fileLang syntax.LangVariant) error {
node, err = parser.Parse(bytes.NewReader(src), path)
if err != nil {
if s, ok := err.(syntax.LangError); ok && lang.val == syntax.LangAuto {
if fileLangFromEditorConfig {
return fmt.Errorf("%w (parsed as %s via EditorConfig)", s, fileLang)
}
return fmt.Errorf("%w (parsed as %s via -%s=%s)", s, fileLang, lang.short, lang.val)
}
return err
Expand Down
6 changes: 2 additions & 4 deletions cmd/shfmt/testdata/script/editorconfig.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ exec shfmt

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

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

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

0 comments on commit 25c0048

Please sign in to comment.