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
4 changes: 2 additions & 2 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ Made with the user in the 2026-11-19 design loop, anchored on `codeanalyzer-pyth
- **Artifact id:** `can://<app>/artifact/<path>`, app-scoped (Python parity) [user]. The Go projector's `:Artifact` flips to `CanNode=true`.
- **Package id:** version-less `pkg:golang/<module-path>` [user]; version lives on the dependency edge.
- **Source text:** `--artifact-text`, on by default; `--no-artifact-text` drops it (Python and TypeScript parity) [user]. `config_keys` deferred [user].
- **Dependencies:** every `go.mod` require; `direct=false` on `// indirect`; `kind:"runtime"`; `go.sum` supplies `locked_version` only, `go.sum`-only entries excluded [user]. `locked_in` names the `go.sum` artifact that locked it; the projector reads it, and `v2.PackageID` is the one package-id builder.
- **`provides_imports`:** longest-prefix match against `go.mod` module paths [user].
- **Dependencies:** every `go.mod` require; `direct=false` on `// indirect`; `kind:"runtime"`; `go.sum` supplies `locked_version` only, `go.sum`-only entries excluded [user]. `locked_in` names the `go.sum` artifact that locked it; the projector reads it, and `v2.PackageID` is the one package-id builder. A `replace` keeps the dependency's `name` and `spec`, records `replaced_by` (`<path>@<version>` or a local directory, which also adds `"replace"` to `prov`), and takes `locked_version` from the replacement, by Go's replace rule.
- **`provides_imports`:** longest-prefix match against `go.mod` module paths [user]. A `-t` run reads every project package's imports with a light package load, so it matches a full run.
- **Level:** L1, every level, not gated [user].

## Neo4j-projection decisions
Expand Down
57 changes: 57 additions & 0 deletions cmd/codeanalyzer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
)
Expand Down Expand Up @@ -911,3 +912,59 @@ func TestRootCmd_EmitNeo4jTwiceInProjectIsStable(t *testing.T) {
t.Error("two runs on an unchanged project wrote different snapshots")
}
}

// TestRootCmd_TargetFilesKeepFullProvidesImports (#71): under -t the symbol
// table holds only the target packages, but provides_imports must be the same
// as on a full run. The library is a local module (replace => ./lib), so the
// package load needs no network.
func TestRootCmd_TargetFilesKeepFullProvidesImports(t *testing.T) {
dir := t.TempDir()
for name, body := range map[string]string{
"go.mod": "module example.com/app\n\ngo 1.25.0\n\nrequire example.com/lib v0.0.0\n\nreplace example.com/lib => ./lib\n",
"cmd/main.go": "package main\n\nimport \"example.com/lib/a\"\n\nfunc main() { a.A() }\n",
"pkg/p.go": "package pkg\n\nimport \"example.com/lib/b\"\n\nfunc P() { b.B() }\n",
"lib/go.mod": "module example.com/lib\n\ngo 1.25.0\n",
"lib/a/a.go": "package a\n\nfunc A() {}\n",
"lib/b/b.go": "package b\n\nfunc B() {}\n",
} {
p := filepath.Join(dir, filepath.FromSlash(name))
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
provides := func(t *testing.T, flags ...string) []string {
t.Helper()
out, _, err := runCmd(append([]string{"--input", dir, "--cache-dir", t.TempDir()}, flags...)...)
if err != nil {
t.Fatalf("%v: %v", flags, err)
}
var doc struct {
Application struct {
Dependencies []struct {
Name string `json:"name"`
ProvidesImports []string `json:"provides_imports"`
} `json:"dependencies"`
} `json:"application"`
}
if err := json.Unmarshal([]byte(out), &doc); err != nil {
t.Fatal(err)
}
for _, d := range doc.Application.Dependencies {
if d.Name == "example.com/lib" {
return d.ProvidesImports
}
}
t.Fatalf("%v: no example.com/lib dependency", flags)
return nil
}
full := provides(t)
if want := []string{"example.com/lib/a", "example.com/lib/b"}; !slices.Equal(full, want) {
t.Fatalf("full run: provides_imports %v, want %v", full, want)
}
if got := provides(t, "-t", "cmd/main.go"); !slices.Equal(got, full) {
t.Errorf("-t cmd/main.go: provides_imports %v, want the full run's %v", got, full)
}
}
17 changes: 14 additions & 3 deletions docs/design/specs/v2-artifacts.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,26 @@ Each decision was made with the user, one question per decision, in the order be
| `direct` | `false` when the line carries `// indirect`, else `true` |
| `locked_version` | version pinned in `go.sum` for that module, when present |
| `locked_in` | the `go.sum` artifact id that gave `locked_version`, when present (#72) |
| `replaced_by` | the `go.mod` `replace` target, when one applies: `<path>@<version>`, or a local directory (#68) |
| `declared_in` | the `go.mod` artifact id |
| `provides_imports` | import paths in the project that resolve to this module (see below) |
| `prov` | `["declared"]`, plus `"lockfile"` when `go.sum` supplies `locked_version` |
| `prov` | `["declared"]`, plus `"lockfile"` when `go.sum` supplies `locked_version`, plus `"replace"` when a local directory replaces it |
- **`go.sum`-only entries are not dependencies** (user decision, the non-recommended option
would have included them; the recommended default is not to, because `go.sum` retains
entries for pruned modules). `go.sum` contributes only `locked_version` and `prov`.
- **`provides_imports`**: populated by longest-prefix match of each project import path
against the `go.mod` module paths (user decision, Python parity). Import paths that match
no requirement are **not** recorded in this change (see Open items).
no requirement are **not** recorded in this change (see Open items). A `-t` run reads the
import paths of every project package with a light package load (names and imports, no
types; the full run's build tags and test setting), so it gets the same `provides_imports`
as a full run (#71) [user, 2026-11-30].
- **`replace` directives** (#68) [user, 2026-11-30]: `go.sum` records what the build uses,
which is the replacement. A replace applies by Go's rule: one for the required version wins
over a version-less one. With `replace x => y v1.2.0`, the dependency keeps `name` `x` and
`spec` (so the code's imports of `x` still match it, and its package is `pkg:golang/x`), gets
`replaced_by` `y@v1.2.0`, and takes `locked_version` from `y v1.2.0` in `go.sum`. With
`replace x => ../x`, the build uses a local directory: the dependency is kept, with
`replaced_by` `../x`, `"replace"` in `prov`, and no `locked_version`.

### Analysis level
- **L1, every level** (user decision). Parse-only facts, identical at `-a 1` through `-a 4`.
Expand All @@ -107,7 +118,7 @@ Each decision was made with the user, one question per decision, in the order be
- `:Package {id, ecosystem, name}` keyed by `id`. The id comes from `v2.PackageID`, the one
place a package id is built (#72).
- `DECLARES_DEPENDENCY` `(:Artifact)→(:Package)`, `_k = kind` (`runtime`), carrying `spec`,
`direct`, `prov` as properties.
`direct`, `prov` and, when a replace applies, `replaced_by` as properties.
- `LOCKS` `(:Artifact)→(:Package)` from the dependency's `locked_in` artifact, carrying
`version` (the sibling analyzers' name; it was `locked_version` before #72).
- No `:ConfigKey` rows and no `DEFINES_CONFIG` rows in this change.
Expand Down
12 changes: 12 additions & 0 deletions internal/core/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,23 @@ func (a *Analyzer) Analyze() (*schema.GoApplication, error) {
if a.opts.Emit == options.EmitNeo4j && a.opts.OutputDir == "" {
graphFile = "graph.cypher" // neo4j.GraphFileName
}
// Under -t the symbol table has only the target packages; provides_imports
// must still cover the whole project (#71). If the light load fails, the
// symbol table's imports are used, and the warning says they are partial.
var imports []string
if len(a.opts.TargetFiles) > 0 {
var err error
if imports, err = repository.LoadImports(a.opts.InputPath, !a.opts.SkipTests); err != nil {
utils.Warn("repository: project imports not loaded, provides_imports covers only the target files: %v", err)
imports = nil
}
}
facts, err := repository.Facts(a.opts.InputPath, app, repository.Options{
CaptureText: a.opts.ArtifactText,
OutputDir: a.opts.OutputDir,
CacheDir: a.cacheRoot,
GraphFile: graphFile,
Imports: imports,
})
if err != nil {
return nil, fmt.Errorf("repository layer: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/neo4j/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (p *projector) projectRepository() {
)
declaredBy := NodeRef{Label: LabelArtifact, KeyProp: "id", Value: dep.DeclaredIn}
p.b.Edge(RelDeclaresDependency, declaredBy, pkgRef,
Props{"spec": dep.Spec, "direct": dep.Direct, "prov": dep.Prov}, dep.Kind)
Props{"spec": dep.Spec, "direct": dep.Direct, "prov": dep.Prov, "replaced_by": emptyToNil(dep.ReplacedBy)}, dep.Kind)
// The go.sum that locked it, by the id the JSON carries (#72).
if dep.LockedVersion != "" && dep.LockedIn != "" {
p.b.Edge(RelLocks, NodeRef{Label: LabelArtifact, KeyProp: "id", Value: dep.LockedIn}, pkgRef,
Expand Down
12 changes: 10 additions & 2 deletions internal/neo4j/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func repoAnalysis() *v2.Analysis {
DeclaredIn: goMod, Prov: []string{"declared"}},
{Name: "golang.org/x/tools", Ecosystem: "golang", Spec: "v0.52.0", Kind: "runtime", Direct: true,
LockedVersion: "v0.52.0", LockedIn: toolsSum, DeclaredIn: toolsMod, Prov: []string{"declared", "lockfile"}},
{Name: "example.com/x", Ecosystem: "golang", Spec: "v1.0.0", Kind: "runtime", Direct: true,
LockedVersion: "v1.2.0", LockedIn: goSum, ReplacedBy: "example.com/y@v1.2.0", DeclaredIn: goMod,
Prov: []string{"declared", "lockfile"}},
},
},
}
Expand Down Expand Up @@ -65,10 +68,14 @@ func TestProject_RepositoryLayerRows(t *testing.T) {
}

decl := edges[RelDeclaresDependency]
if len(decl) != 3 {
t.Fatalf("DECLARES_DEPENDENCY edges = %d, want 3", len(decl))
if len(decl) != 4 {
t.Fatalf("DECLARES_DEPENDENCY edges = %d, want 4", len(decl))
}
for _, e := range decl {
// #68: replaced_by rides the edge, only on a replaced dependency.
if want := map[string]any{"pkg:golang/example.com/x": "example.com/y@v1.2.0"}[e.To.Value]; e.Props["replaced_by"] != want {
t.Errorf("DECLARES_DEPENDENCY -> %s replaced_by = %v, want %v", e.To.Value, e.Props["replaced_by"], want)
}
if e.To.Label != LabelPackage || e.Key != "runtime" {
t.Errorf("DECLARES_DEPENDENCY %s -> %s:%s _k=%q, want an artifact -> Package with _k runtime", e.From.Value, e.To.Label, e.To.Value, e.Key)
}
Expand All @@ -80,6 +87,7 @@ func TestProject_RepositoryLayerRows(t *testing.T) {
want := map[string]string{
"pkg:golang/github.com/gorilla/mux": base + "go.sum",
"pkg:golang/golang.org/x/tools": base + "tools/go.sum",
"pkg:golang/example.com/x": base + "go.sum",
}
locks := edges[RelLocks]
if len(locks) != len(want) {
Expand Down
2 changes: 1 addition & 1 deletion internal/neo4j/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ var Rels = []RelSpec{

// Repository layer.
{Type: RelHasArtifact, From: []string{LabelApplication}, To: []string{LabelArtifact}},
{Type: RelDeclaresDependency, From: []string{LabelArtifact}, To: []string{LabelPackage}, Props: []string{"spec", "direct", "prov"}, Discriminant: "kind"},
{Type: RelDeclaresDependency, From: []string{LabelArtifact}, To: []string{LabelPackage}, Props: []string{"spec", "direct", "prov", "replaced_by"}, Discriminant: "kind"},
{Type: RelLocks, From: []string{LabelArtifact}, To: []string{LabelPackage}, Props: []string{"version"}},
{Type: RelDefinesConfig, From: []string{LabelArtifact}, To: []string{LabelConfigKey}},
{Type: RelProvides, From: []string{LabelPackage}, To: []string{LabelExternal}},
Expand Down
96 changes: 96 additions & 0 deletions internal/repository/replace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package repository

import (
"reflect"
"testing"

"github.com/codellm-devkit/codeanalyzer-go/internal/schema"
)

// replaceFacts runs Facts on a project with the given go.mod and go.sum, whose
// code imports the given paths.
func replaceFacts(t *testing.T, mod, sum string, imports ...string) map[string]schema.GoDependency {
t.Helper()
root := t.TempDir()
writeTree(t, root, map[string]string{"go.mod": mod, "go.sum": sum, "main.go": "package main\n"})
facts, err := Facts(root, appWithImports(imports...), Options{})
if err != nil {
t.Fatal(err)
}
out := map[string]schema.GoDependency{}
for _, d := range facts.Dependencies {
out[d.Name] = d
}
return out
}

// TestFacts_ModuleReplace (#68): with replace x => y v1.2.0, go.sum has only the
// replacement's lines. locked_version comes from them, replaced_by names the
// replacement, and name stays x, so the code's imports of x still match.
func TestFacts_ModuleReplace(t *testing.T) {
deps := replaceFacts(t, `module example.com/app

go 1.25.0

require example.com/x v1.0.0

replace example.com/x => example.com/y v1.2.0
`, "example.com/y v1.2.0 h1:AAA=\nexample.com/y v1.2.0/go.mod h1:BBB=\n", "example.com/x/sub")

want := schema.GoDependency{
Name: "example.com/x", Ecosystem: "golang", Spec: "v1.0.0", Kind: "runtime", Direct: true,
LockedVersion: "v1.2.0", LockedIn: "go.sum", ReplacedBy: "example.com/y@v1.2.0", DeclaredIn: "go.mod",
ProvidesImports: []string{"example.com/x/sub"}, Prov: []string{"declared", "lockfile"},
}
if got := deps["example.com/x"]; !reflect.DeepEqual(got, want) {
t.Errorf("dependency:\n got %+v\n want %+v", got, want)
}
}

// TestFacts_LocalReplace (#68): a dependency replaced by a local directory is
// kept, marked local: replaced_by is the path, prov has "replace", and it has
// no locked_version (there is no version and no go.sum line).
func TestFacts_LocalReplace(t *testing.T) {
deps := replaceFacts(t, `module example.com/app

go 1.25.0

require example.com/x v1.0.0

replace example.com/x => ../x
`, "example.com/x v1.0.0 h1:AAA=\nexample.com/x v1.0.0/go.mod h1:BBB=\n")

got := deps["example.com/x"]
if got.ReplacedBy != "../x" || got.LockedVersion != "" || got.LockedIn != "" ||
!reflect.DeepEqual(got.Prov, []string{"declared", "replace"}) {
t.Errorf("local replace: got replaced_by %q, locked %q in %q, prov %v; want ../x, none, [declared replace]",
got.ReplacedBy, got.LockedVersion, got.LockedIn, got.Prov)
}
}

// TestFacts_TwoReplaces (#68): Go's rule. A version-specific replace applies
// only to that version and wins over a version-less one.
func TestFacts_TwoReplaces(t *testing.T) {
deps := replaceFacts(t, `module example.com/app

go 1.25.0

require (
example.com/a v1.0.0
example.com/b v1.2.0
)

replace (
example.com/a v1.0.0 => example.com/a v1.0.1
example.com/a => example.com/a v9.0.0
example.com/b v1.5.0 => example.com/b v1.6.0
)
`, "")

if got := deps["example.com/a"].ReplacedBy; got != "example.com/a@v1.0.1" {
t.Errorf("a: replaced_by %q, want the version-specific example.com/a@v1.0.1", got)
}
if got := deps["example.com/b"].ReplacedBy; got != "" {
t.Errorf("b: replaced_by %q, want none (the replace is for v1.5.0, b requires v1.2.0)", got)
}
}
Loading
Loading