From 48a1d9d54e62edc1c41ca5f69e1289e0ddee75a3 Mon Sep 17 00:00:00 2001 From: Joseph Kato Date: Mon, 8 Apr 2024 03:55:40 -0700 Subject: [PATCH] fix: fallback to default config when syncing #798 --- cmd/vale/command.go | 8 +++++--- internal/core/config.go | 20 +++++++++++++++++++- internal/core/source.go | 2 +- internal/core/source_test.go | 2 +- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/cmd/vale/command.go b/cmd/vale/command.go index baff05e8..b61278c1 100644 --- a/cmd/vale/command.go +++ b/cmd/vale/command.go @@ -93,9 +93,11 @@ func sync(_ []string, flags *core.CLIFlags) error { return err } - // NOTE: sync should *only* run for a single config file. In practice, this - // means that we sync only using the local search process. - rootINI := cfg.ConfigFiles[0] + // NOTE: sync should *only* run for a single config file. + rootINI, noRoot := cfg.Root() + if noRoot != nil { + return core.NewE100("sync", noRoot) + } pkgs, err := core.GetPackages(rootINI) if err != nil { diff --git a/internal/core/config.go b/internal/core/config.go index 85d0fc3b..6faecdb6 100644 --- a/internal/core/config.go +++ b/internal/core/config.go @@ -257,7 +257,7 @@ func (c *Config) AddStylesPath(path string) { // ConfigFile returns the last configuration file in the list. // -// This represents the user's project-specific configuration file -- i.e., the +// This represents the user's project-agnostic configuration file -- i.e., the // last one that was added. func (c *Config) ConfigFile() string { if len(c.ConfigFiles) > 0 { @@ -266,6 +266,24 @@ func (c *Config) ConfigFile() string { return "" } +// Root returns the first configuration file in the list. +func (c *Config) Root() (string, error) { + if len(c.ConfigFiles) > 0 { + return c.ConfigFiles[0], nil + } + + root, err := DefaultConfig() + if err != nil { + return "", err + } + + if !FileExists(root) { + return "", fmt.Errorf("no .vale.ini file found") + } + + return root, nil +} + // GetStylesPath returns the last path in the list. // // This represents the user's project-specific styles directory -- i.e., the diff --git a/internal/core/source.go b/internal/core/source.go index f18c9a7e..7c76c362 100644 --- a/internal/core/source.go +++ b/internal/core/source.go @@ -174,7 +174,7 @@ func loadINI(cfg *Config, dry bool) (*ini.File, error) { } cfg.Flags.Local = true cfg.AddConfigFile(defaultCfg) - } else if base == "" && len(cfg.ConfigFiles) == 0 { + } else if base == "" && len(cfg.ConfigFiles) == 0 && !dry { return nil, NewE100(".vale.ini not found", errors.New("no config file found")) } diff --git a/internal/core/source_test.go b/internal/core/source_test.go index e15690dc..c43424c5 100644 --- a/internal/core/source_test.go +++ b/internal/core/source_test.go @@ -26,7 +26,7 @@ func TestNoBaseConfig(t *testing.T) { t.Fatal(err) } - _, err = FromFile(cfg, true) + _, err = FromFile(cfg, false) if err == nil { t.Fatal("Expected error, got nil", cfg.ConfigFiles) }