Unverified Commit 64d019d2 authored by Cristian Maglie's avatar Cristian Maglie Committed by GitHub

[breaking] Do not prevent CLI startup if `inventory.yaml` is corrupted (#2232)

* Made `inventory` package private.

* If an error occurs reading the inventory.xml just log it and replace it.

* Added integration test
parent 53004ef1
......@@ -32,7 +32,7 @@ import (
"github.com/arduino/arduino-cli/arduino/discovery"
"github.com/arduino/arduino-cli/arduino/httpclient"
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/inventory"
"github.com/arduino/arduino-cli/internal/inventory"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
......
......@@ -31,7 +31,7 @@ import (
"github.com/arduino/arduino-cli/commands"
"github.com/arduino/arduino-cli/configuration"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/inventory"
"github.com/arduino/arduino-cli/internal/inventory"
"github.com/arduino/arduino-cli/legacy/builder"
"github.com/arduino/arduino-cli/legacy/builder/types"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
......
......@@ -4,6 +4,10 @@ Here you can find a list of migration guides to handle breaking changes between
## 0.34.0
### golang package `github.com/arduino/arduino-cli/inventory` removed from public API
The package `inventory` is no more a public golang API.
### `board list --watch` command JSON output has changed
`board list --watch` command JSON output changed from:
......
......@@ -43,7 +43,7 @@ import (
"github.com/arduino/arduino-cli/internal/cli/upgrade"
"github.com/arduino/arduino-cli/internal/cli/upload"
"github.com/arduino/arduino-cli/internal/cli/version"
"github.com/arduino/arduino-cli/inventory"
"github.com/arduino/arduino-cli/internal/inventory"
versioninfo "github.com/arduino/arduino-cli/version"
"github.com/fatih/color"
"github.com/mattn/go-colorable"
......
......@@ -25,7 +25,7 @@ import (
"github.com/arduino/arduino-cli/configuration"
"github.com/arduino/arduino-cli/i18n"
"github.com/arduino/arduino-cli/internal/cli/feedback"
"github.com/arduino/arduino-cli/inventory"
"github.com/arduino/arduino-cli/internal/inventory"
"github.com/arduino/arduino-cli/version"
"github.com/fatih/color"
semver "go.bug.st/relaxed-semver"
......
......@@ -589,3 +589,21 @@ func TestBoardListWithFailedBuiltinInstallation(t *testing.T) {
require.Empty(t, stderr)
require.Contains(t, string(stdout), "Downloading missing tool builtin:serial-discovery")
}
func TestCLIStartupWithCorruptedInventory(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()
_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)
f, err := cli.DataDir().Join("inventory.yaml").Append()
require.NoError(t, err)
_, err = f.WriteString(`data: '[{"name":"WCH;32?'","fqbn":"esp32:esp32:esp32s3camlcd"}]'`)
require.NoError(t, err)
// the CLI should not be able to load inventory and report it to the logs
_, stderr, err := cli.Run("core", "update-index", "-v")
require.NoError(t, err)
require.Contains(t, string(stderr), "Error loading inventory store")
}
......@@ -23,6 +23,7 @@ import (
"github.com/arduino/arduino-cli/i18n"
"github.com/gofrs/uuid"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
......@@ -46,17 +47,15 @@ func Init(configPath string) error {
Store.AddConfigPath(configPath)
// Attempt to read config file
if err := Store.ReadInConfig(); err != nil {
// ConfigFileNotFoundError is acceptable, anything else
// should be reported to the user
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
if err := generateInstallationData(); err != nil {
return err
}
if err := WriteStore(); err != nil {
return err
}
} else {
return fmt.Errorf(tr("reading inventory file: %w"), err)
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
// If an error occurs during initalization of the store, just log it and recreate it from scratch.
logrus.WithError(err).Error("Error loading inventory store")
}
if err := generateInstallationData(); err != nil {
return err
}
if err := WriteStore(); err != nil {
return err
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment