From 2091bd5b2d4f04d53c748cf483bc0b52c3316d12 Mon Sep 17 00:00:00 2001 From: Serag Ahmed Date: Fri, 20 Nov 2026 02:31:12 +0300 Subject: [PATCH] feat: load rules from `.yaml` files as well as `.yml` A style's rules were found by the `.yml` suffix, so a rule saved as `Rule.yaml` was skipped without a word. The same suffix was fixed where a rule is turned on by name (`Style.Rule = YES`), where `extends: Style.Rule` finds its parent, and where a spelling rule is read back for suggestions. All four now accept either extension through core.RuleExtensions. If a style holds both `Rule.yml` and `Rule.yaml`, the rule is read from `Rule.yml`, which is the file Vale loaded before; the other is ignored rather than failing with a duplicate rule name. Fixes #1217 Co-Authored-By: Claude Opus 5.5 --- internal/check/action.go | 6 ++- internal/check/inherit.go | 8 ++-- internal/check/inherit_test.go | 24 ++++++++++++ internal/check/manager.go | 22 +++++++---- internal/core/format.go | 23 ++++++++++++ testdata/e2e/config.yaml | 67 ++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+), 13 deletions(-) diff --git a/internal/check/action.go b/internal/check/action.go index df3194ab..68b496d8 100644 --- a/internal/check/action.go +++ b/internal/check/action.go @@ -261,7 +261,11 @@ func spelling(alert core.Alert, cfg *core.Config) ([]string, error) { if len(name) != 2 { return suggestions, fmt.Errorf("unknown check '%s'", alert.Check) } - path := filepath.Join(cfg.StylesPath(), name[0], name[1]+".yml") + stem := filepath.Join(cfg.StylesPath(), name[0], name[1]) + path, found := core.FindRuleFile(cfg.FS, stem) + if !found { + path = stem + ".yml" + } mgr, err := NewManager(cfg) if err != nil { diff --git a/internal/check/inherit.go b/internal/check/inherit.go index 09417dab..25ca3ca0 100644 --- a/internal/check/inherit.go +++ b/internal/check/inherit.go @@ -118,16 +118,16 @@ func (mgr *Manager) resolveRule(name string) (string, error) { return "", fmt.Errorf("'%s' is not a rule name", name) } - rel := filepath.Join(parts[1:]...) + ".yml" + rel := filepath.Join(parts[1:]...) for _, p := range mgr.Config.SearchPaths() { - candidate := filepath.Join(p, parts[0], rel) - if mgr.Config.FS.Exists(candidate) { + stem := filepath.Join(p, parts[0], rel) + if candidate, found := core.FindRuleFile(mgr.Config.FS, stem); found { return candidate, nil } } return "", fmt.Errorf( - "'%s' does not exist on the search path (looked for '%s')", + "'%s' does not exist on the search path (looked for '%s.yml' or '.yaml')", name, filepath.Join(parts[0], rel)) } diff --git a/internal/check/inherit_test.go b/internal/check/inherit_test.go index a3be742d..ab2323c2 100644 --- a/internal/check/inherit_test.go +++ b/internal/check/inherit_test.go @@ -168,3 +168,27 @@ message: "leaf" t.Errorf("chain flattened wrong: %v", got) } } + +// A parent may be written as `.yaml`; with both spellings beside each other, +// `.yml` is the rule. See #1217. +func TestResolveRuleExtensions(t *testing.T) { + mgr, sp := managerWithStyles(t, map[string]string{ + "Only/Words.yaml": parentRule, + "Both/Words.yml": parentRule, + "Both/Words.yaml": parentRule, + }) + + cases := []struct{ name, want string }{ + {"Only.Words", filepath.Join(sp, "Only", "Words.yaml")}, + {"Both.Words", filepath.Join(sp, "Both", "Words.yml")}, + } + for _, tt := range cases { + got, err := mgr.resolveRule(tt.name) + if err != nil { + t.Fatalf("resolveRule(%q): %v", tt.name, err) + } + if got != tt.want { + t.Errorf("resolveRule(%q) = %q; want %q", tt.name, got, tt.want) + } + } +} diff --git a/internal/check/manager.go b/internal/check/manager.go index c07437da..928c61b5 100755 --- a/internal/check/manager.go +++ b/internal/check/manager.go @@ -32,8 +32,6 @@ type Manager struct { // NewManager creates a new Manager and loads the rule definitions (that is, // extended checks) specified by configuration. func NewManager(config *core.Config) (*Manager, error) { - var path string - mgr := Manager{ Config: config, @@ -71,14 +69,15 @@ func NewManager(config *core.Config) (*Manager, error) { if !mgr.hasStyle(parts[0]) { // If this rule isn't part of an already-loaded style, we load it // individually. Every segment after the style is a path - // component: `Std.dates.TimeFormat` is `Std/dates/TimeFormat.yml`. - fName := filepath.Join(parts[1:]...) + ".yml" + // component: `Std.dates.TimeFormat` is `Std/dates/TimeFormat.yml` + // (or `.yaml`). + stem := filepath.Join(parts[1:]...) for _, p := range mgr.Config.SearchPaths() { - path = filepath.Join(p, parts[0], fName) - if !mgr.Config.FS.Exists(path) { + rule, found := core.FindRuleFile(mgr.Config.FS, filepath.Join(p, parts[0], stem)) + if !found { continue } - if err = mgr.addCheckFile(chk, path); err != nil { + if err = mgr.addCheckFile(chk, rule); err != nil { return &mgr, err } } @@ -223,7 +222,7 @@ func (mgr *Manager) addStyle(path string) error { // underscore prefix keeps a directory inert, the same convention // the Go toolchain and the test-file walk use. return filepath.SkipDir - case info.IsDir() || !strings.HasSuffix(info.Name(), ".yml"): + case info.IsDir() || !core.HasRuleExt(info.Name()): return nil case core.IsTestFile(info.Name()): // A rule's cases live beside it, so the style directory holds YAML @@ -232,6 +231,13 @@ func (mgr *Manager) addStyle(path string) error { return nil } + // `Rule.yml` and `Rule.yaml` would both be `Style.Rule`. Only the + // preferred extension is the rule, whichever the walk reaches first. + stem := strings.TrimSuffix(fp, filepath.Ext(fp)) + if preferred, _ := core.FindRuleFile(mgr.Config.FS, stem); preferred != fp { + return nil + } + chkName, nErr := core.CheckName(path, fp) if nErr != nil { return nErr diff --git a/internal/core/format.go b/internal/core/format.go index ff30b2e6..fd8bf477 100755 --- a/internal/core/format.go +++ b/internal/core/format.go @@ -3,10 +3,12 @@ package core import ( "path/filepath" "regexp" + "slices" "sort" "strings" "github.com/vale-cli/vale/v3/internal/glob" + "github.com/vale-cli/vale/v3/internal/system" ) // TestFileSuffixes name the YAML that holds a rule's test cases rather than a @@ -29,6 +31,27 @@ func IsTestFile(name string) bool { return false } +// RuleExtensions are the extensions a rule file may have, in order of +// preference: a rule written as both `Rule.yml` and `Rule.yaml` is read from +// `Rule.yml`, the one Vale loaded before `.yaml` was accepted. See #1217. +var RuleExtensions = []string{".yml", ".yaml"} + +// HasRuleExt reports whether a file name ends in one of RuleExtensions. +func HasRuleExt(name string) bool { + return slices.Contains(RuleExtensions, filepath.Ext(name)) +} + +// FindRuleFile returns the file that defines the rule at stem -- its path +// without an extension -- trying each of RuleExtensions in turn. +func FindRuleFile(fsys system.FS, stem string) (string, bool) { + for _, ext := range RuleExtensions { + if path := stem + ext; fsys.Exists(path) { + return path, true + } + } + return "", false +} + // CommentsByNormedExt determines what parts of a file we should lint -- e.g., // we only want to lint ; comments in a Clojure file. // diff --git a/testdata/e2e/config.yaml b/testdata/e2e/config.yaml index 493c71de..596e3012 100644 --- a/testdata/e2e/config.yaml +++ b/testdata/e2e/config.yaml @@ -623,6 +623,73 @@ cases: styles/Demo/Rule.yml:4:5:Vale.Spelling:Did you really mean 'typpo'? styles/Demo/Rule.yml:5:11:Vale.Spelling:Did you really mean 'typpo'? + - name: rule-yaml-extension + about: "#1217 -- a rule file may end in `.yaml` as well as `.yml`, whether + it loads with its style, is turned on by name, or is extended by a rule" + files: + _vale: | + StylesPath = styles + MinAlertLevel = suggestion + + [*] + BasedOnStyles = T + U.Case = YES + styles/T/Hedge.yaml: | + extends: existence + message: "Avoid '%s'." + level: warning + tokens: + - very + styles/T/Coined.yml: | + extends: Base.Words + message: "'%s' is not a word." + styles/U/Case.yaml: | + extends: substitution + message: "Use '%s' instead of '%s'." + level: warning + ignorecase: false + swap: + javascript: JavaScript + styles/Base/Words.yaml: | + extends: existence + message: "'%s'" + level: warning + tokens: + - agendize + args: test.md + exit: 0 + want: | + test.md:1:11:T.Hedge:Avoid 'very'. + test.md:1:66:U.Case:Use 'JavaScript' instead of 'javascript'. + test.md:1:77:T.Coined:'agendize' is not a word. + + - name: rule-yml-and-yaml + about: "#1217 -- with both `Hedge.yml` and `Hedge.yaml`, `T.Hedge` is read + from `.yml`, the file Vale loaded before `.yaml` was accepted" + files: + _vale: | + StylesPath = styles + MinAlertLevel = suggestion + + [*] + BasedOnStyles = T + styles/T/Hedge.yml: | + extends: existence + message: "From .yml: '%s'." + level: warning + tokens: + - very + styles/T/Hedge.yaml: | + extends: existence + message: "From .yaml: '%s'." + level: warning + tokens: + - very + args: test.md + exit: 0 + want: | + test.md:1:11:T.Hedge:From .yml: 'very'. + - name: vocab-per-section about: a section's Vocab adds its terms for that section's files only, with Terms and Avoid rules of its own; the top-level Vocab is unchanged