diff --git a/CLAUDE.md b/CLAUDE.md index c7dbe8f..6681151 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -333,7 +333,7 @@ cd src && go test -cover ./... # With coverage Never let a python.org source write a default location: a sync skips anything already mirrored, so a python.org build there (from a release python.org shipped before python-build-standalone did, or a run where the python-build-standalone listing failed) would stay the default for good. Older releases the mirror filled that way before this rule (3.9.13, for example) keep python.org's build as their default. -**Signed Python builds**: `dtvem install python --signed` installs the build at the python.org location, python.org's Authenticode-signed one. The manifest generator labels each Python entry with `build` (`python-build-standalone` or `python.org`, from the metadata's source URL) and nests the python.org build under the default entry's `alternates`; released CLIs ignore both fields. A version with no default build gets its python.org build as the default instead, until python-build-standalone's arrives. The CLI's manifest cache has a `format` (2); older cache files are refetched. Each install records its build in `.dtvem-build.json` in the version's directory (`src/runtimes/python/build.go`); `dtvem list` shows it, and `dtvem list-all` marks the versions with a signed build. +**Signed Python builds**: `dtvem install python --signed` installs the build at the python.org location, python.org's Authenticode-signed one. The manifest generator labels each Python entry with `build` (`python-build-standalone` or `python.org`, from the metadata's source URL) and nests the python.org build under the default entry's `alternates`; released CLIs ignore both fields. A version with no default build gets its python.org build as the default instead, until python-build-standalone's arrives. The CLI's manifest cache records its `format` (2) and the dtvem version that wrote it (`cli_version`, from `manifest.SetCLIVersion`); a cache file of another format or version is refetched, so a newly installed dtvem never reads manifests an older build cached. Each install records its build in `.dtvem-build.json` in the version's directory (`src/runtimes/python/build.go`); `dtvem list` shows it, and `dtvem list-all` marks the versions with a signed build. ### Reusable Workflows (from `dtvem/.github` repo) diff --git a/src/cmd/root.go b/src/cmd/root.go index 838675f..600c17b 100644 --- a/src/cmd/root.go +++ b/src/cmd/root.go @@ -5,6 +5,7 @@ import ( "fmt" "os" + "github.com/CodingWithCalvin/dtvem.cli/src/internal/manifest" "github.com/CodingWithCalvin/dtvem.cli/src/internal/tui" "github.com/CodingWithCalvin/dtvem.cli/src/internal/ui" "github.com/spf13/cobra" @@ -21,6 +22,9 @@ var rootCmd = &cobra.Command{ } func Execute() { + // Manifests cached by another dtvem version are fetched again + manifest.SetCLIVersion(Version) + // Check for --version or -v flag before Cobra parses for _, arg := range os.Args[1:] { if arg == "--version" || arg == "-v" { diff --git a/src/internal/manifest/build_test.go b/src/internal/manifest/build_test.go index 041e774..001f631 100644 --- a/src/internal/manifest/build_test.go +++ b/src/internal/manifest/build_test.go @@ -195,3 +195,87 @@ func TestCachedSource_IgnoresOlderFormat(t *testing.T) { t.Error("manifest has no python.org alternate; the older-format cache was used") } } + +// A newly installed dtvem fetches the manifests again instead of reading +// ones another version cached, even in the current format. A pre-release +// build may have cached a manifest from before the release's fields existed. +func TestCachedSource_IgnoresOtherVersion(t *testing.T) { + tests := []struct { + name string + writer string // version that cached the manifest + reader string // version reading it + wantFetches int + wantAlternate bool + }{ + {name: "pre-release cache, new release", writer: "dev", reader: "0.7.4", wantFetches: 1, wantAlternate: true}, + {name: "older release cache, new release", writer: "0.7.3", reader: "0.7.4", wantFetches: 1, wantAlternate: true}, + {name: "cache from no recorded version, new release", writer: "", reader: "0.7.4", wantFetches: 1, wantAlternate: true}, + {name: "same version reads its own cache", writer: "0.7.4", reader: "0.7.4", wantFetches: 0, wantAlternate: false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + + // The cached manifest predates the python.org builds + stale, err := ParseManifest([]byte(`{"version":1,"versions":{"3.12.10":{"windows-amd64":{"url":"https://builds.dtvem.io/python/3.12.10/windows-amd64.tar.gz","sha256":"x"}}}}`)) + if err != nil { + t.Fatalf("ParseManifest(stale) error: %v", err) + } + writer := NewCachedSource(newMockSource(), dir, time.Hour) + writer.version = tt.writer + if err := writer.saveToCache("python", stale); err != nil { + t.Fatalf("saveToCache() error: %v", err) + } + + fresh, err := ParseManifest([]byte(buildsManifestJSON)) + if err != nil { + t.Fatalf("ParseManifest(fresh) error: %v", err) + } + mock := newMockSource() + mock.manifests["python"] = fresh + reader := NewCachedSource(mock, dir, time.Hour) + reader.version = tt.reader + + got, err := reader.GetManifest("python") + if err != nil { + t.Fatalf("GetManifest() error: %v", err) + } + if mock.callCount["python"] != tt.wantFetches { + t.Errorf("source called %d times, want %d", mock.callCount["python"], tt.wantFetches) + } + hasAlternate := got.GetDownload("3.12.10", "windows-amd64").ForBuild(BuildPythonOrg) != nil + if hasAlternate != tt.wantAlternate { + t.Errorf("python.org alternate present = %v, want %v", hasAlternate, tt.wantAlternate) + } + + // The refetched manifest is cached for the reading version + if tt.wantFetches > 0 { + if _, err := reader.GetManifest("python"); err != nil { + t.Fatalf("second GetManifest() error: %v", err) + } + if mock.callCount["python"] != tt.wantFetches { + t.Errorf("second call fetched again: source called %d times, want %d", mock.callCount["python"], tt.wantFetches) + } + } + }) + } +} + +func TestSetCLIVersion_DefaultSource(t *testing.T) { + t.Cleanup(func() { + SetCLIVersion("") + ResetDefaultSource() + }) + + ResetDefaultSource() + SetCLIVersion("0.7.4") + DefaultSource() + + if defaultCached == nil { + t.Fatal("DefaultSource() did not create the cached source") + } + if defaultCached.version != "0.7.4" { + t.Errorf("default cache version = %q, want %q", defaultCached.version, "0.7.4") + } +} diff --git a/src/internal/manifest/cache.go b/src/internal/manifest/cache.go index 4f5f4e9..51b0e4c 100644 --- a/src/internal/manifest/cache.go +++ b/src/internal/manifest/cache.go @@ -16,6 +16,21 @@ type CachedSource struct { source Source cacheDir string ttl time.Duration + // version is the dtvem version reading and writing the cache. A cache + // file written by another version is treated as missing. + version string +} + +// cliVersion is the running dtvem version, recorded by SetCLIVersion. +var cliVersion string + +// SetCLIVersion records the running dtvem version. The default source's +// cache then ignores manifests cached by any other version, so installing a +// new dtvem always fetches fresh manifests instead of reading one cached +// before the new version's manifest fields existed. Call it before +// DefaultSource. +func SetCLIVersion(version string) { + cliVersion = version } // cacheFormat is the format of the cache files this version writes. A cache @@ -28,9 +43,10 @@ const cacheFormat = 2 // cacheEntry stores a manifest along with its cache timestamp. type cacheEntry struct { - Format int `json:"format,omitempty"` - CachedAt time.Time `json:"cached_at"` - Manifest *Manifest `json:"manifest"` + Format int `json:"format,omitempty"` + CLIVersion string `json:"cli_version,omitempty"` + CachedAt time.Time `json:"cached_at"` + Manifest *Manifest `json:"manifest"` } // NewCachedSource creates a Source that caches results from the underlying source. @@ -124,9 +140,10 @@ func (s *CachedSource) loadFromCache(runtime string) (*Manifest, error) { return nil, err } - // Check if cache is still valid - if entry.Format != cacheFormat || time.Since(entry.CachedAt) > s.ttl { - return nil, os.ErrNotExist // Treat expired or older-format cache as not found + // A cache written in another format, by another dtvem version, or too long + // ago is treated as not found + if entry.Format != cacheFormat || entry.CLIVersion != s.version || time.Since(entry.CachedAt) > s.ttl { + return nil, os.ErrNotExist } return entry.Manifest, nil @@ -138,9 +155,10 @@ func (s *CachedSource) saveToCache(runtime string, manifest *Manifest) error { } entry := cacheEntry{ - Format: cacheFormat, - CachedAt: time.Now(), - Manifest: manifest, + Format: cacheFormat, + CLIVersion: s.version, + CachedAt: time.Now(), + Manifest: manifest, } data, err := json.Marshal(entry) diff --git a/src/internal/manifest/default.go b/src/internal/manifest/default.go index 97bb7fd..191c878 100644 --- a/src/internal/manifest/default.go +++ b/src/internal/manifest/default.go @@ -37,8 +37,9 @@ func createDefaultSource() Source { // Remote source - fetches from manifests.dtvem.io remote := NewHTTPSource(DefaultRemoteURL) - // Cached source - wraps remote with local disk cache + // Cached source - wraps remote with local disk cache, kept per dtvem version defaultCached = NewCachedSource(remote, cacheDir, DefaultCacheTTL) + defaultCached.version = cliVersion // Embedded source - bundled in binary, always available defaultEmbedded = NewEmbeddedSource()