diff --git a/internal/core/analyzer.go b/internal/core/analyzer.go index b9dbf34..cef7c05 100644 --- a/internal/core/analyzer.go +++ b/internal/core/analyzer.go @@ -44,6 +44,10 @@ import ( // Analyzer is the top-level analysis driver. Construct with New() and call Analyze(). type Analyzer struct { opts options.AnalysisOptions + // cacheRoot is the cache directory as the user gave it, before the + // per-application identity-v2- suffix. The repository layer excludes all + // of it, so no application's cache is recorded as a project artifact. + cacheRoot string // l3 is the Phase 5 side table (nil below L3). It stays off // schema.GoApplication: schema must not depend on dataflow, and the // cached v1 model has no L3 facts. @@ -85,11 +89,14 @@ func resolveIdentity(opts options.AnalysisOptions) (v2.Identity, error) { // New creates an Analyzer with a cache isolated by canonical application identity. func New(opts options.AnalysisOptions) *Analyzer { + // The root is kept before the per-application suffix: it is the tool's own cache + // location, and every application's subdirectory lives beneath it. + cacheRoot := opts.CacheDir if identity, err := resolveIdentity(opts); err == nil && opts.CacheDir != "" { key := sha256.Sum256([]byte(identity.ApplicationID)) opts.CacheDir = filepath.Join(opts.CacheDir, fmt.Sprintf("identity-v2-%x", key)) } - return &Analyzer{opts: opts} + return &Analyzer{opts: opts, cacheRoot: cacheRoot} } // Analyze runs the full analysis pipeline and returns a GoApplication. @@ -103,7 +110,7 @@ func (a *Analyzer) Analyze() (*schema.GoApplication, error) { facts, err := repository.Facts(a.opts.InputPath, app, repository.Options{ CaptureText: a.opts.ArtifactText, OutputDir: a.opts.OutputDir, - CacheDir: a.opts.CacheDir, + CacheDir: a.cacheRoot, }) if err != nil { return nil, fmt.Errorf("repository layer: %w", err) diff --git a/internal/core/cache_leak_test.go b/internal/core/cache_leak_test.go new file mode 100644 index 0000000..548f388 --- /dev/null +++ b/internal/core/cache_leak_test.go @@ -0,0 +1,58 @@ +package core_test + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/codellm-devkit/codeanalyzer-go/internal/core" + "github.com/codellm-devkit/codeanalyzer-go/internal/options" +) + +// The cache directory lives inside the project (-c ./cache). Each application +// writes its own identity-v2-/analysis_cache.json beneath it. Before the +// fix, the repository layer excluded only the current application's subdirectory, +// so run B recorded run A's cache as an artifact, with its full source under +// --artifact-text. Those bytes are another application's analysis. +// +// Practical shape of the project: +// +// project/ +// go.mod main.go +// cache/ +// identity-v2-/analysis_cache.json <- written by run A +// identity-v2-/analysis_cache.json <- written by run B +func TestAnalyze_CacheRootInsideProjectIsNotAnArtifact(t *testing.T) { + dir := t.TempDir() + files := map[string]string{ + "go.mod": "module example.com/cl\n\ngo 1.22\n", + "main.go": "package main\n\nfunc main() {}\n", + } + for rel, content := range files { + if err := os.WriteFile(filepath.Join(dir, rel), []byte(content), 0o644); err != nil { + t.Fatal(err) + } + } + cache := filepath.Join(dir, "cache") + + // Run A, then run B: B is the run whose output must not carry A's cache. + for _, app := range []string{"appA", "appB"} { + opts := options.AnalysisOptions{ + InputPath: dir, AppName: app, Level: options.LevelSymbolTable, + SkipTests: true, CacheDir: cache, OutputDir: t.TempDir(), ArtifactText: true, + } + got, err := core.New(opts).Analyze() + if err != nil { + t.Fatalf("%s: %v", app, err) + } + for path := range got.Repository.Artifacts { + if strings.HasPrefix(path, "cache/") || strings.Contains(path, "analysis_cache.json") { + t.Errorf("%s recorded cache file %q as a project artifact", app, path) + } + } + if _, ok := got.Repository.Artifacts["go.mod"]; !ok { + t.Errorf("%s: go.mod missing from artifacts; the project itself must still be discovered", app) + } + } +}