Skip to content

Commit

Permalink
fix: fallback to default config when syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Apr 8, 2024
1 parent 231fac2 commit 48a1d9d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
8 changes: 5 additions & 3 deletions cmd/vale/command.go
Expand Up @@ -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 {
Expand Down
20 changes: 19 additions & 1 deletion internal/core/config.go
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/core/source.go
Expand Up @@ -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"))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/core/source_test.go
Expand Up @@ -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)
}
Expand Down

0 comments on commit 48a1d9d

Please sign in to comment.