Skip to content

Commit

Permalink
fix: announce when a config file is invalid and exit with a non-zero …
Browse files Browse the repository at this point in the history
…code (#1242)

When looking into some other config stuff I realised the current loading
logic just assumes that an error is because a config doesn't exist and
silently falls back to the default one, when really it could be that
there is a config but it's invalid
  • Loading branch information
G-Rath authored Sep 16, 2024
1 parent eccb9a2 commit 3dceabd
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
23 changes: 23 additions & 0 deletions cmd/osv-scanner/__snapshots__/main_test.snap
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,29 @@ overriding license for package Packagist/league/flysystem/1.0.8 with 0BSD

---

[TestRun/config_file_is_invalid - 1]
Scanning dir ./fixtures/config-invalid
Scanned <rootdir>/fixtures/config-invalid/composer.lock file and found 1 package

---

[TestRun/config_file_is_invalid - 2]
Ignored invalid config file at: <rootdir>/fixtures/config-invalid/osv-scanner.toml

---

[TestRun/config_file_is_invalid#01 - 1]
Scanning dir ./fixtures/config-invalid
Scanned <rootdir>/fixtures/config-invalid/composer.lock file and found 1 package
Config file <rootdir>/fixtures/config-invalid/osv-scanner.toml is invalid because: toml: line 1: expected '.' or '=', but got '!' instead

---

[TestRun/config_file_is_invalid#01 - 2]
Ignored invalid config file at: <rootdir>/fixtures/config-invalid/osv-scanner.toml

---

[TestRun/cyclonedx_1.4_output - 1]
{
"$schema": "http://cyclonedx.org/schema/bom-1.4.schema.json",
Expand Down
51 changes: 51 additions & 0 deletions cmd/osv-scanner/fixtures/config-invalid/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/osv-scanner/fixtures/config-invalid/osv-scanner.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!
11 changes: 11 additions & 0 deletions cmd/osv-scanner/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ func TestRun(t *testing.T) {
args: []string{"", "--config=./fixtures/osv-scanner-composite-config.toml", "--experimental-licenses", "MIT", "./fixtures/locks-many", "./fixtures/locks-insecure"},
exit: 1,
},
// invalid config file
{
name: "config file is invalid",
args: []string{"", "./fixtures/config-invalid"},
exit: 127,
},
{
name: "config file is invalid",
args: []string{"", "--verbosity", "verbose", "./fixtures/config-invalid"},
exit: 127,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
10 changes: 8 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -175,6 +176,11 @@ func (c *ConfigManager) Get(r reporter.Reporter, targetPath string) Config {
if configErr == nil {
r.Infof("Loaded filter from: %s\n", config.LoadPath)
} else {
// anything other than the config file not existing is most likely due to an invalid config file
if !errors.Is(configErr, os.ErrNotExist) {
r.Errorf("Ignored invalid config file at: %s\n", configPath)
r.Verbosef("Config file %s is invalid because: %v\n", configPath, configErr)
}
// If config doesn't exist, use the default config
config = c.DefaultConfig
}
Expand Down Expand Up @@ -211,12 +217,12 @@ func tryLoadConfig(configPath string) (Config, error) {

_, err := toml.NewDecoder(file).Decode(&config)
if err != nil {
return Config{}, fmt.Errorf("failed to parse config file: %w", err)
return Config{}, err
}
config.LoadPath = configPath

return config, nil
}

return Config{}, fmt.Errorf("no config file found on this path: %s", configPath)
return Config{}, err
}

0 comments on commit 3dceabd

Please sign in to comment.