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
11 changes: 9 additions & 2 deletions internal/core/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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-<hash> 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.
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down
58 changes: 58 additions & 0 deletions internal/core/cache_leak_test.go
Original file line number Diff line number Diff line change
@@ -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-<hash>/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-<hash of appA>/analysis_cache.json <- written by run A
// identity-v2-<hash of appB>/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)
}
}
}
Loading