Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions internal/config/config.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -122,14 +122,18 @@ func Load() (*Config, error) {
}

// Detect the on-disk schema. v1 (cli#83) had no "version" key and a flat
// {env,email,token,active_client_id}; it decodes here as version 0 → migrate.
// {env,email,token,active_client_id}; it decodes here as version 0. Migrate
// only a GENUINE v1 record: an old version AND no v2 `profiles` object — so a
// v2-shaped file with a missing/wrong version is still parsed as v2 and never
// has its profiles silently dropped by migrateV1.
var probe struct {
Version int `json:"version"`
Version int `json:"version"`
Profiles json.RawMessage `json:"profiles"`
}
if err := json.Unmarshal(data, &probe); err != nil {
return nil, fmt.Errorf("parsing %s: %w", path, err)
}
if probe.Version < schemaVersion {
if probe.Version < schemaVersion && len(probe.Profiles) == 0 {
return migrateV1(data, path)
}
var c Config
Expand Down
23 changes: 23 additions & 0 deletions internal/config/config_test.go
Original file line numberDiff line numberDiff line change
Expand Up@@ -176,3 +176,26 @@ func TestSignedInRequiresCurrentEnvToken(t *testing.T) {
t.Error("signed-in should be true once the current env has a token")
}
}

// TestLoadV2ShapedWithoutVersion_NotMigrated pins the Bugbot fix: a config that
// already has v2 `profiles` but a missing/old version must be parsed as v2, not
// misread as v1 and migrated (which reads only flat fields → would drop profiles).
func TestLoadV2ShapedWithoutVersion_NotMigrated(t *testing.T) {
dir := t.TempDir()
t.Setenv("TRACEBLOC_CONFIG_DIR", dir)
// No "version" key, but a v2-shaped profiles object.
raw := `{"current_env":"dev","profiles":{"dev":{"token":"keep-me","active_client_id":"7"}}}`
if err := os.WriteFile(filepath.Join(dir, "config.json"), []byte(raw), 0o600); err != nil {
t.Fatal(err)
}
c, err := Load()
if err != nil {
t.Fatal(err)
}
if !c.SignedIn() {
t.Fatalf("v2-shaped config without a version was misread as v1 (profiles dropped): %+v", c)
}
if p := c.Profile("dev"); p.Token != "keep-me" || p.ActiveClientID != "7" {
t.Errorf("dev profile not preserved: %+v", p)
}
}
Loading