From 5565a70886df9d0e7199d9195e33f7cc81cabd4b Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Tue, 10 Nov 2026 10:15:28 +0530 Subject: [PATCH 1/5] Read APM dependency graphs from the lockfile --- README.md | 7 ++ apm_test.go | 193 +++++++++++++++++++++++++++++++++++++ go.mod | 18 ++-- go.sum | 14 ++- parsers/apm.go | 169 ++++++++++++++++++++++++++++++++ testdata/apm/apm.lock.yaml | 55 +++++++++++ 6 files changed, 446 insertions(+), 10 deletions(-) create mode 100644 apm_test.go create mode 100644 parsers/apm.go create mode 100644 testdata/apm/apm.lock.yaml diff --git a/README.md b/README.md index 0cf7bac..225db05 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,13 @@ Each `Dep` includes the ecosystem-native package name, resolved version, a PURL | conan | conan | Custom | | deno | deno | JSON flat | | helm | helm | Tabular | +| apm | apm | Lockfile graph | + +## Directory parsers + +Some managers record their resolved graph in a lockfile rather than in command output. `ParseDir(manager, dir)` reads those files from a project directory. + +APM reads `apm.lock.yaml`, or the legacy `apm.lock` when the current file is absent, without running `apm` or reading `apm_modules/`. Package identities come from the APM parser in [manifests](https://github.com/git-pkgs/manifests), so Git dependencies keep their commit and repository subpath, while local and registry sources have no PURL. Edges come from each entry's `depth` and `resolved_by`. As in APM's own `apm deps tree`, a parent is the single entry at the previous depth whose lock key or `repo_url` matches `resolved_by`. When several entries match, such as two subpackages of one repository, `ParseDir` returns an ambiguous-parent error instead of picking one. Missing parents and duplicate entries are errors too. ## License diff --git a/apm_test.go b/apm_test.go new file mode 100644 index 0000000..aae33a1 --- /dev/null +++ b/apm_test.go @@ -0,0 +1,193 @@ +package resolve_test + +import ( + "errors" + "os" + "path/filepath" + "reflect" + "testing" + + "github.com/git-pkgs/resolve" + "github.com/git-pkgs/resolve/parsers" +) + +const ( + apmSkillsCommit = "7f3c9a4d2e1b8c7f0a9e6d5c4b3a2918f7e6d5c4" + apmCommit = "0123456789abcdef0123456789abcdef01234567" +) + +func writeAPMLockfile(t *testing.T, name, content string) string { + t.Helper() + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil { + t.Fatal(err) + } + return dir +} + +func TestAPM(t *testing.T) { + result, err := resolve.ParseDir("apm", filepath.Join("testdata", "apm")) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if result.Manager != "apm" || result.Ecosystem != "apm" { + t.Errorf("result = %s/%s, want apm/apm", result.Manager, result.Ecosystem) + } + + type node struct{ name, version, purl string } + var roots []node + for _, dep := range result.Direct { + roots = append(roots, node{dep.Name, dep.Version, dep.PURL}) + } + want := []node{ + {"github.com/acme-corp/security-baseline", "a1b2c3d4e5f6789012345678901234567890abcd", + "pkg:git/github.com/acme-corp/security-baseline@a1b2c3d4e5f6789012345678901234567890abcd"}, + {"github.com/composiohq/awesome-claude-skills/brand-guidelines", apmSkillsCommit, + "pkg:git/github.com/composiohq/awesome-claude-skills@" + apmSkillsCommit + "#brand-guidelines"}, + {"github.com/composiohq/awesome-claude-skills/canvas-design", apmSkillsCommit, + "pkg:git/github.com/composiohq/awesome-claude-skills@" + apmSkillsCommit + "#canvas-design"}, + // Registry and local sources have no package coordinates. + {"acme/toolkit", "2.3.1", ""}, + {"./packages/shared-skills", "0.1.0", ""}, + {"github.com/owner/test-helpers", apmCommit, "pkg:git/github.com/owner/test-helpers@" + apmCommit}, + } + if !reflect.DeepEqual(roots, want) { + t.Fatalf("direct =\n%+v\nwant\n%+v", roots, want) + } + + // Transitive chain resolved by repo_url, across hosts. + baseline := findDep(result.Direct, "github.com/acme-corp/security-baseline") + prompts := findDep(baseline.Deps, "github.com/acme-corp/common-prompts") + if prompts == nil || len(baseline.Deps) != 1 { + t.Fatalf("security-baseline deps = %+v", baseline.Deps) + } + style := findDep(prompts.Deps, "gitlab.com/team/style-guide") + if style == nil || style.PURL != "pkg:git/gitlab.com/team/style-guide@0f1e2d3c4b5a69788796a5b4c3d2e1f0a9b8c7d6" { + t.Errorf("style-guide = %+v", style) + } + + // A parent named by its full lock key picks one subpackage, not its sibling. + canvas := findDep(result.Direct, "github.com/composiohq/awesome-claude-skills/canvas-design") + if findDep(canvas.Deps, "github.com/composiohq/design-tokens") == nil { + t.Errorf("canvas-design deps = %+v, want design-tokens", canvas.Deps) + } + brand := findDep(result.Direct, "github.com/composiohq/awesome-claude-skills/brand-guidelines") + if len(brand.Deps) != 0 { + t.Errorf("brand-guidelines deps = %+v, want none", brand.Deps) + } + + for _, dep := range result.Direct { + if dep.Deps == nil { + t.Errorf("%s Deps is nil, want an empty slice for a tree manager", dep.Name) + } + } +} + +func TestAPMLegacyLockfile(t *testing.T) { + content, err := os.ReadFile(filepath.Join("testdata", "apm", "apm.lock.yaml")) + if err != nil { + t.Fatal(err) + } + current, err := resolve.ParseDir("apm", filepath.Join("testdata", "apm")) + if err != nil { + t.Fatal(err) + } + legacy, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock", string(content))) + if err != nil { + t.Fatalf("legacy apm.lock: %v", err) + } + if !reflect.DeepEqual(legacy.Direct, current.Direct) { + t.Error("legacy apm.lock graph differs from apm.lock.yaml") + } +} + +func TestAPMPrefersCurrentLockfile(t *testing.T) { + dir := writeAPMLockfile(t, "apm.lock.yaml", "dependencies:\n - repo_url: owner/current\n resolved_commit: "+apmCommit+"\n") + if err := os.WriteFile(filepath.Join(dir, "apm.lock"), []byte("dependencies:\n - repo_url: owner/legacy\n"), 0o644); err != nil { + t.Fatal(err) + } + result, err := resolve.ParseDir("apm", dir) + if err != nil { + t.Fatal(err) + } + if len(result.Direct) != 1 || result.Direct[0].Name != "github.com/owner/current" { + t.Errorf("direct = %+v, want only github.com/owner/current", result.Direct) + } +} + +func TestAPMParentByHostQualifiedKey(t *testing.T) { + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: + - repo_url: team/rules + host: gitlab.example.com + resolved_commit: `+apmCommit+` + - repo_url: team/shared + host: gitlab.example.com + resolved_commit: `+apmCommit+` + depth: 2 + resolved_by: gitlab.example.com/team/rules +`)) + if err != nil { + t.Fatal(err) + } + if len(result.Direct) != 1 || findDep(result.Direct[0].Deps, "gitlab.example.com/team/shared") == nil { + t.Errorf("direct = %+v, want team/shared under team/rules", result.Direct) + } +} + +func TestAPMGraphErrors(t *testing.T) { + commit := " resolved_commit: " + apmCommit + "\n" + tests := []struct { + name, content string + want error + }{ + {"ambiguous parent", "dependencies:\n" + + " - repo_url: acme/skills\n virtual_path: review\n is_virtual: true\n" + commit + + " - repo_url: acme/skills\n virtual_path: deploy\n is_virtual: true\n" + commit + + " - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n", + parsers.ErrAPMAmbiguousParent}, + {"missing parent", "dependencies:\n" + + " - repo_url: acme/orphan\n" + commit + " depth: 2\n resolved_by: acme/gone\n", + parsers.ErrAPMMissingParent}, + {"parent at wrong depth", "dependencies:\n" + + " - repo_url: acme/root\n" + commit + + " - repo_url: acme/deep\n" + commit + " depth: 3\n resolved_by: acme/root\n", + parsers.ErrAPMMissingParent}, + {"cyclic parents", "dependencies:\n" + + " - repo_url: acme/a\n" + commit + " depth: 2\n resolved_by: acme/b\n" + + " - repo_url: acme/b\n" + commit + " depth: 2\n resolved_by: acme/a\n", + parsers.ErrAPMMissingParent}, + {"duplicate entry", "dependencies:\n" + + " - repo_url: acme/twice\n" + commit + + " - repo_url: acme/twice\n" + commit, + parsers.ErrAPMDuplicateEntry}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", tt.content)) + if !errors.Is(err, tt.want) { + t.Errorf("err = %v, want %v", err, tt.want) + } + }) + } +} + +func TestAPMInvalidLockfile(t *testing.T) { + for name, content := range map[string]string{ + "malformed YAML": "dependencies: [unterminated\n", + "missing repo_url": "dependencies:\n - resolved_commit: " + apmCommit + "\n", + "unsupported version": "lockfile_version: \"3\"\nreplacements: []\ndependencies: []\n", + } { + t.Run(name, func(t *testing.T) { + if _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", content)); err == nil { + t.Error("expected error") + } + }) + } +} + +func TestAPMMissingLockfile(t *testing.T) { + _, err := resolve.ParseDir("apm", t.TempDir()) + if !errors.Is(err, parsers.ErrAPMLockfileMissing) { + t.Errorf("err = %v, want ErrAPMLockfileMissing", err) + } +} diff --git a/go.mod b/go.mod index 89645cb..c5695e1 100644 --- a/go.mod +++ b/go.mod @@ -1,17 +1,19 @@ module github.com/git-pkgs/resolve -go 1.26.0 - -toolchain go1.26.7 - -require github.com/git-pkgs/purl v0.2.1 +go 1.27 require ( - github.com/package-url/packageurl-go v0.1.7 // indirect - go.yaml.in/yaml/v3 v3.0.5 // indirect + github.com/git-pkgs/managers v0.12.0 + github.com/git-pkgs/manifests v0.14.0 + github.com/git-pkgs/purl v0.2.1 + go.yaml.in/yaml/v3 v3.0.6 ) require ( - github.com/git-pkgs/managers v0.12.0 + github.com/BurntSushi/toml v1.6.0 // indirect + github.com/bazelbuild/buildtools v0.0.0-20260716142318-04cf7de1434f // indirect + github.com/bmatcuk/doublestar/v4 v4.10.4 // indirect + github.com/git-pkgs/pom v0.1.8 // indirect github.com/git-pkgs/vers v0.7.2 // indirect + github.com/package-url/packageurl-go v0.1.7 // indirect ) diff --git a/go.sum b/go.sum index f861344..34ad88e 100644 --- a/go.sum +++ b/go.sum @@ -1,10 +1,20 @@ +github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk= +github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= +github.com/bazelbuild/buildtools v0.0.0-20260716142318-04cf7de1434f h1:2mT6QcXmMFtvg7bezs1Fef7nnpJyeCmfWoWNjwtvNZ4= +github.com/bazelbuild/buildtools v0.0.0-20260716142318-04cf7de1434f/go.mod h1:PLNUetjLa77TCCziPsz0EI8a6CUxgC+1jgmWv0H25tg= +github.com/bmatcuk/doublestar/v4 v4.10.4 h1:oTKkRmRuOsKMqFe1AQazd9gGBFNEpOmgKp/xzB97dnA= +github.com/bmatcuk/doublestar/v4 v4.10.4/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= github.com/git-pkgs/managers v0.12.0 h1:Ex0fZ7hOzoxzXYLmY5ghxqel6jAltaehuGOcPFeQz4U= github.com/git-pkgs/managers v0.12.0/go.mod h1:gWk/V63R12skgcTmVsYgED/kxlk2MERS2iWMYKBrJeM= +github.com/git-pkgs/manifests v0.14.0 h1:UE+kxktTLN77XiaklhlhzG0zD8GAd/g2lOjQepO6LYI= +github.com/git-pkgs/manifests v0.14.0/go.mod h1:+yP/Ivk5j9MDfs7G70BlzxWnFB8q+kFOWtbLhXd+OT0= +github.com/git-pkgs/pom v0.1.8 h1:mXCuWw6XkrKKZb6IurRZyIznYLPhV5AYg5ZO38Uj/l8= +github.com/git-pkgs/pom v0.1.8/go.mod h1:sQBeYRzjCUn77fcABPn6I/a1aBHFxm55jTmkNxYtGtw= github.com/git-pkgs/purl v0.2.1 h1:b46hRlOMbrG6aXjjFJdYixGi5WTh0TLlAnH9iNeuOS0= github.com/git-pkgs/purl v0.2.1/go.mod h1:l8rCfvt5iXVrFIcoQ8+vCLOaHhrtEW2id0LENP+IzMw= github.com/git-pkgs/vers v0.7.2 h1:Ctex1O+Ai/FONmyDaw37EolBXg0og6UdxbCjjYbeRHs= github.com/git-pkgs/vers v0.7.2/go.mod h1:ofLiBpPNkQmC0LB1k0zmN1gCv7Hi3MiZx8WtmWZ4A9A= github.com/package-url/packageurl-go v0.1.7 h1:iFWg6tzAjLA6F/qX3M5nZaiMHJgc+p2zxVyr/fY+sZY= github.com/package-url/packageurl-go v0.1.7/go.mod h1:nKAWB8E6uk1MHqiS/lQb9pYBGH2+mdJ2PJc2s50dQY0= -go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= -go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= +go.yaml.in/yaml/v3 v3.0.6 h1:zcDfvjXEqTQmpjniUBLc91xYJMB2c4QOFxEF/LCEckM= +go.yaml.in/yaml/v3 v3.0.6/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= diff --git a/parsers/apm.go b/parsers/apm.go new file mode 100644 index 0000000..1fad6b1 --- /dev/null +++ b/parsers/apm.go @@ -0,0 +1,169 @@ +package parsers + +import ( + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + "strings" + + "github.com/git-pkgs/manifests" + "go.yaml.in/yaml/v3" + + "github.com/git-pkgs/resolve" +) + +// Errors reported for APM lockfile graphs. +var ( + ErrAPMLockfileMissing = errors.New("no apm.lock.yaml or apm.lock") + ErrAPMAmbiguousParent = errors.New("ambiguous parent") + ErrAPMMissingParent = errors.New("parent not found") + ErrAPMDuplicateEntry = errors.New("duplicate lockfile entry") +) + +type apmLock struct { + Dependencies []apmLockEntry `yaml:"dependencies"` +} + +type apmLockEntry struct { + RepoURL string `yaml:"repo_url"` + Host string `yaml:"host"` + Source string `yaml:"source"` + LocalPath string `yaml:"local_path"` + IsVirtual bool `yaml:"is_virtual"` + VirtualPath string `yaml:"virtual_path"` + RegistryPrefix string `yaml:"registry_prefix"` + Depth *int `yaml:"depth"` + ResolvedBy string `yaml:"resolved_by"` +} + +// apmNode is one lockfile entry with its identity and graph position. +type apmNode struct { + entry apmLockEntry + depth int + key string + dep *resolve.Dep +} + +// parseAPMDir builds the graph from manifests' APM identities plus depth and resolved_by. +func parseAPMDir(dir string) ([]*resolve.Dep, error) { + name, content, err := readAPMLockfile(dir) + if err != nil { + return nil, err + } + + parsed, err := manifests.Parse(name, content) + if err != nil { + return nil, err + } + var lock apmLock + if err := yaml.Unmarshal(content, &lock); err != nil { + return nil, fmt.Errorf("reading %s: %w", name, err) + } + + nodes := apmNodes(lock.Dependencies) + if len(nodes) != len(parsed.Dependencies) { + return nil, fmt.Errorf("reading %s: %d graph entries but %d dependencies", name, len(nodes), len(parsed.Dependencies)) + } + keys := make(map[string]bool, len(nodes)) + for i, node := range nodes { + if keys[node.key] { + return nil, fmt.Errorf("%w: %s", ErrAPMDuplicateEntry, node.key) + } + keys[node.key] = true + identity := parsed.Dependencies[i] + node.dep = &resolve.Dep{PURL: identity.PURL, Name: identity.Name, Version: identity.Version, Deps: []*resolve.Dep{}} + } + + var roots []*resolve.Dep + for _, node := range nodes { + if node.depth == 1 { + roots = append(roots, node.dep) + continue + } + parent, err := apmParent(nodes, node) + if err != nil { + return nil, err + } + parent.dep.Deps = append(parent.dep.Deps, node.dep) + } + return roots, nil +} + +func readAPMLockfile(dir string) (string, []byte, error) { + for _, name := range []string{"apm.lock.yaml", "apm.lock"} { + content, err := os.ReadFile(filepath.Join(dir, name)) + if err == nil { + return name, content, nil + } + if !errors.Is(err, fs.ErrNotExist) { + return "", nil, err + } + } + return "", nil, ErrAPMLockfileMissing +} + +// apmNodes keeps the entries manifests reports, skipping and depth 0 as it does. +func apmNodes(entries []apmLockEntry) []*apmNode { + var nodes []*apmNode + for _, entry := range entries { + depth := 1 + if entry.Depth != nil { + depth = *entry.Depth + } + if entry.RepoURL == "" || depth == 0 { + continue + } + nodes = append(nodes, &apmNode{entry: entry, depth: depth, key: apmLockKey(entry)}) + } + return nodes +} + +// apmParent finds the single previous-depth entry named by resolved_by, as APM does. +func apmParent(nodes []*apmNode, child *apmNode) (*apmNode, error) { + ref := child.entry.ResolvedBy + for _, match := range []func(*apmNode) bool{ + func(n *apmNode) bool { return n.key == ref }, + func(n *apmNode) bool { return n.entry.RepoURL == ref }, + } { + var candidates []*apmNode + for _, n := range nodes { + if n.depth == child.depth-1 && match(n) { + candidates = append(candidates, n) + } + } + switch len(candidates) { + case 0: + continue + case 1: + return candidates[0], nil + default: + return nil, fmt.Errorf("%w %q for %s: %d entries at depth %d", ErrAPMAmbiguousParent, ref, child.key, len(candidates), child.depth-1) + } + } + return nil, fmt.Errorf("%w: %s at depth %d has resolved_by %q", ErrAPMMissingParent, child.key, child.depth, ref) +} + +// apmLockKey mirrors APM's lockfile identity key. +func apmLockKey(entry apmLockEntry) string { + if entry.Source == "local" && entry.LocalPath != "" { + return entry.LocalPath + } + host := strings.ToLower(strings.TrimSpace(entry.Host)) + key := entry.RepoURL + if entry.Source == "registry" || entry.RegistryPrefix != "" || host == "" || host == "github.com" || strings.HasSuffix(host, ".ghe.com") { + key = strings.ToLower(key) + } + if entry.IsVirtual && entry.VirtualPath != "" { + key += "/" + entry.VirtualPath + } + if entry.RegistryPrefix == "" && host != "" && host != "github.com" { + key = host + "/" + key + } + return key +} + +func init() { + resolve.RegisterDir("apm", "apm", parseAPMDir) +} diff --git a/testdata/apm/apm.lock.yaml b/testdata/apm/apm.lock.yaml new file mode 100644 index 0000000..2ed5dc8 --- /dev/null +++ b/testdata/apm/apm.lock.yaml @@ -0,0 +1,55 @@ +lockfile_version: "2" +apm_version: "0.33.0" +dependencies: + - repo_url: acme-corp/security-baseline + resolved_commit: a1b2c3d4e5f6789012345678901234567890abcd + resolved_ref: v2.1.0 + version: "2.1.0" + depth: 1 + package_type: apm_package + - repo_url: acme-corp/common-prompts + resolved_commit: f6e5d4c3b2a1098765432109876543210fedcba9 + resolved_ref: main + depth: 2 + resolved_by: acme-corp/security-baseline + package_type: apm_package + - repo_url: team/style-guide + host: gitlab.com + resolved_commit: 0f1e2d3c4b5a69788796a5b4c3d2e1f0a9b8c7d6 + depth: 3 + resolved_by: acme-corp/common-prompts + - repo_url: composiohq/awesome-claude-skills + virtual_path: brand-guidelines + is_virtual: true + resolved_commit: 7f3c9a4d2e1b8c7f0a9e6d5c4b3a2918f7e6d5c4 + resolved_ref: main + depth: 1 + package_type: claude_skill + - repo_url: composiohq/awesome-claude-skills + virtual_path: canvas-design + is_virtual: true + resolved_commit: 7f3c9a4d2e1b8c7f0a9e6d5c4b3a2918f7e6d5c4 + resolved_ref: main + depth: 1 + package_type: claude_skill + - repo_url: composiohq/design-tokens + resolved_commit: 1234567890abcdef1234567890abcdef12345678 + depth: 2 + resolved_by: composiohq/awesome-claude-skills/canvas-design + - repo_url: acme/toolkit + source: registry + version: "2.3.1" + resolved_url: https://registry.example.com/v1/packages/acme/toolkit/versions/2.3.1/download + resolved_hash: "sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9" + depth: 1 + - repo_url: _local/shared-skills + source: local + local_path: ./packages/shared-skills + version: "0.1.0" + depth: 1 + - repo_url: owner/test-helpers + resolved_commit: 0123456789abcdef0123456789abcdef01234567 + depth: 1 + is_dev: true +local_deployed_files: + - .github/skills/my-local-skill/SKILL.md From baa1b417c6d9be93b147cb27a0dd1be33223ea0a Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Tue, 10 Nov 2026 10:37:30 +0530 Subject: [PATCH 2/5] Report repo_url parent references shared with subpackages as ambiguous --- README.md | 2 +- apm_test.go | 28 ++++++++++++++++++++++++++++ parsers/apm.go | 40 ++++++++++++++++++++++------------------ 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 225db05..faf6597 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ result, err := resolve.Parse("npm", output) // result.Direct == []*Dep{ {PURL: "pkg:npm/express@4.18.2", Name: "express", Version: "4.18.2", Deps: [...]}, ... } ``` -`Parse` is the only entry point. It dispatches to the correct parser based on the manager name and returns `ErrUnsupportedManager` for unknown managers. +`Parse` and `ParseDir` are the entry points: `Parse` reads command output and `ParseDir` reads files from a project directory (see [Directory parsers](#directory-parsers)). Each dispatches to the correct parser based on the manager name and returns `ErrUnsupportedManager` for unknown managers. Each `Dep` includes the ecosystem-native package name, resolved version, a PURL string, and a `Deps` slice for transitive dependencies. `Deps` is nil for managers that only produce flat lists (pip, conda, bundler, helm, etc.) and non-nil for managers that provide tree structure. diff --git a/apm_test.go b/apm_test.go index aae33a1..458408b 100644 --- a/apm_test.go +++ b/apm_test.go @@ -134,6 +134,29 @@ func TestAPMParentByHostQualifiedKey(t *testing.T) { } } +func TestAPMParentByVirtualKey(t *testing.T) { + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: + - repo_url: acme/skills + resolved_commit: `+apmCommit+` + - repo_url: acme/skills + virtual_path: review + is_virtual: true + resolved_commit: `+apmCommit+` + - repo_url: acme/shared + resolved_commit: `+apmCommit+` + depth: 2 + resolved_by: acme/skills/review +`)) + if err != nil { + t.Fatal(err) + } + review := findDep(result.Direct, "github.com/acme/skills/review") + whole := findDep(result.Direct, "github.com/acme/skills") + if review == nil || findDep(review.Deps, "github.com/acme/shared") == nil || len(whole.Deps) != 0 { + t.Errorf("direct = %+v, want acme/shared under the review subpackage only", result.Direct) + } +} + func TestAPMGraphErrors(t *testing.T) { commit := " resolved_commit: " + apmCommit + "\n" tests := []struct { @@ -145,6 +168,11 @@ func TestAPMGraphErrors(t *testing.T) { " - repo_url: acme/skills\n virtual_path: deploy\n is_virtual: true\n" + commit + " - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n", parsers.ErrAPMAmbiguousParent}, + {"repo_url shared by package and subpackage", "dependencies:\n" + + " - repo_url: acme/skills\n" + commit + + " - repo_url: acme/skills\n virtual_path: review\n is_virtual: true\n" + commit + + " - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n", + parsers.ErrAPMAmbiguousParent}, {"missing parent", "dependencies:\n" + " - repo_url: acme/orphan\n" + commit + " depth: 2\n resolved_by: acme/gone\n", parsers.ErrAPMMissingParent}, diff --git a/parsers/apm.go b/parsers/apm.go index 1fad6b1..ef595bb 100644 --- a/parsers/apm.go +++ b/parsers/apm.go @@ -123,26 +123,30 @@ func apmNodes(entries []apmLockEntry) []*apmNode { // apmParent finds the single previous-depth entry named by resolved_by, as APM does. func apmParent(nodes []*apmNode, child *apmNode) (*apmNode, error) { ref := child.entry.ResolvedBy - for _, match := range []func(*apmNode) bool{ - func(n *apmNode) bool { return n.key == ref }, - func(n *apmNode) bool { return n.entry.RepoURL == ref }, - } { - var candidates []*apmNode - for _, n := range nodes { - if n.depth == child.depth-1 && match(n) { - candidates = append(candidates, n) - } - } - switch len(candidates) { - case 0: - continue - case 1: - return candidates[0], nil - default: - return nil, fmt.Errorf("%w %q for %s: %d entries at depth %d", ErrAPMAmbiguousParent, ref, child.key, len(candidates), child.depth-1) + // A bare repo_url can also name its virtual subpackages, so only a fuller key is exact. + explicit := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref && n.entry.RepoURL != ref }) + if len(explicit) == 1 { + return explicit[0], nil + } + candidates := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref || n.entry.RepoURL == ref }) + switch len(candidates) { + case 0: + return nil, fmt.Errorf("%w: %s at depth %d has resolved_by %q", ErrAPMMissingParent, child.key, child.depth, ref) + case 1: + return candidates[0], nil + default: + return nil, fmt.Errorf("%w %q for %s: %d entries at depth %d", ErrAPMAmbiguousParent, ref, child.key, len(candidates), child.depth-1) + } +} + +func apmCandidates(nodes []*apmNode, child *apmNode, match func(*apmNode) bool) []*apmNode { + var candidates []*apmNode + for _, n := range nodes { + if n.depth == child.depth-1 && match(n) { + candidates = append(candidates, n) } } - return nil, fmt.Errorf("%w: %s at depth %d has resolved_by %q", ErrAPMMissingParent, child.key, child.depth, ref) + return candidates } // apmLockKey mirrors APM's lockfile identity key. From ad02f32590c1dcc2f570af4d6c375f60dfa9a0d5 Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Wed, 11 Nov 2026 13:08:40 +0530 Subject: [PATCH 3/5] Key anchored local APM entries and use bare repo_url parents in fixtures --- apm_test.go | 51 ++++++++++++++++++++++++++++---------- parsers/apm.go | 6 ++++- testdata/apm/apm.lock.yaml | 2 +- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/apm_test.go b/apm_test.go index 458408b..aea83aa 100644 --- a/apm_test.go +++ b/apm_test.go @@ -58,7 +58,7 @@ func TestAPM(t *testing.T) { // Transitive chain resolved by repo_url, across hosts. baseline := findDep(result.Direct, "github.com/acme-corp/security-baseline") prompts := findDep(baseline.Deps, "github.com/acme-corp/common-prompts") - if prompts == nil || len(baseline.Deps) != 1 { + if prompts == nil || findDep(baseline.Deps, "github.com/composiohq/design-tokens") == nil || len(baseline.Deps) != 2 { t.Fatalf("security-baseline deps = %+v", baseline.Deps) } style := findDep(prompts.Deps, "gitlab.com/team/style-guide") @@ -66,16 +66,6 @@ func TestAPM(t *testing.T) { t.Errorf("style-guide = %+v", style) } - // A parent named by its full lock key picks one subpackage, not its sibling. - canvas := findDep(result.Direct, "github.com/composiohq/awesome-claude-skills/canvas-design") - if findDep(canvas.Deps, "github.com/composiohq/design-tokens") == nil { - t.Errorf("canvas-design deps = %+v, want design-tokens", canvas.Deps) - } - brand := findDep(result.Direct, "github.com/composiohq/awesome-claude-skills/brand-guidelines") - if len(brand.Deps) != 0 { - t.Errorf("brand-guidelines deps = %+v, want none", brand.Deps) - } - for _, dep := range result.Direct { if dep.Deps == nil { t.Errorf("%s Deps is nil, want an empty slice for a tree manager", dep.Name) @@ -115,7 +105,7 @@ func TestAPMPrefersCurrentLockfile(t *testing.T) { } } -func TestAPMParentByHostQualifiedKey(t *testing.T) { +func TestAPMParentOnOtherHost(t *testing.T) { result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: - repo_url: team/rules host: gitlab.example.com @@ -124,7 +114,7 @@ func TestAPMParentByHostQualifiedKey(t *testing.T) { host: gitlab.example.com resolved_commit: `+apmCommit+` depth: 2 - resolved_by: gitlab.example.com/team/rules + resolved_by: team/rules `)) if err != nil { t.Fatal(err) @@ -134,6 +124,7 @@ func TestAPMParentByHostQualifiedKey(t *testing.T) { } } +// Full keys are what replacement lockfiles write; v1 and v2 use bare repo_urls. func TestAPMParentByVirtualKey(t *testing.T) { result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: - repo_url: acme/skills @@ -157,6 +148,40 @@ func TestAPMParentByVirtualKey(t *testing.T) { } } +func TestAPMAnchoredLocalPaths(t *testing.T) { + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: + - repo_url: _local/a + source: local + local_path: ./packages/a + - repo_url: _local/b + source: local + local_path: ./tools/b + - repo_url: _local/sibling + source: local + local_path: ../sibling + anchored_local_path: packages/sibling + depth: 2 + resolved_by: _local/a + - repo_url: _local/sibling + source: local + local_path: ../sibling + anchored_local_path: tools/sibling + depth: 2 + resolved_by: _local/b +`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(result.Direct) != 2 { + t.Fatalf("direct = %+v, want two local parents", result.Direct) + } + for _, parent := range result.Direct { + if len(parent.Deps) != 1 || parent.Deps[0].Name != "../sibling" { + t.Errorf("%s deps = %+v, want one ../sibling", parent.Name, parent.Deps) + } + } +} + func TestAPMGraphErrors(t *testing.T) { commit := " resolved_commit: " + apmCommit + "\n" tests := []struct { diff --git a/parsers/apm.go b/parsers/apm.go index ef595bb..79a1de1 100644 --- a/parsers/apm.go +++ b/parsers/apm.go @@ -31,6 +31,7 @@ type apmLockEntry struct { Host string `yaml:"host"` Source string `yaml:"source"` LocalPath string `yaml:"local_path"` + AnchoredLocal string `yaml:"anchored_local_path"` IsVirtual bool `yaml:"is_virtual"` VirtualPath string `yaml:"virtual_path"` RegistryPrefix string `yaml:"registry_prefix"` @@ -123,7 +124,7 @@ func apmNodes(entries []apmLockEntry) []*apmNode { // apmParent finds the single previous-depth entry named by resolved_by, as APM does. func apmParent(nodes []*apmNode, child *apmNode) (*apmNode, error) { ref := child.entry.ResolvedBy - // A bare repo_url can also name its virtual subpackages, so only a fuller key is exact. + // APM writes bare repo_urls for v1 and v2; a fuller key is exact when one appears. explicit := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref && n.entry.RepoURL != ref }) if len(explicit) == 1 { return explicit[0], nil @@ -152,6 +153,9 @@ func apmCandidates(nodes []*apmNode, child *apmNode, match func(*apmNode) bool) // apmLockKey mirrors APM's lockfile identity key. func apmLockKey(entry apmLockEntry) string { if entry.Source == "local" && entry.LocalPath != "" { + if entry.AnchoredLocal != "" { + return "local:" + entry.AnchoredLocal + } return entry.LocalPath } host := strings.ToLower(strings.TrimSpace(entry.Host)) diff --git a/testdata/apm/apm.lock.yaml b/testdata/apm/apm.lock.yaml index 2ed5dc8..283f422 100644 --- a/testdata/apm/apm.lock.yaml +++ b/testdata/apm/apm.lock.yaml @@ -35,7 +35,7 @@ dependencies: - repo_url: composiohq/design-tokens resolved_commit: 1234567890abcdef1234567890abcdef12345678 depth: 2 - resolved_by: composiohq/awesome-claude-skills/canvas-design + resolved_by: acme-corp/security-baseline - repo_url: acme/toolkit source: registry version: "2.3.1" From 83a1a0b60574a369929c6d876114c4a825884d02 Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Wed, 11 Nov 2026 21:12:12 +0530 Subject: [PATCH 4/5] Match APM parents by case-folded repo_url and keep path case for URL locators --- apm_test.go | 20 ++++++++++++++++++++ parsers/apm.go | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/apm_test.go b/apm_test.go index aea83aa..3b3b5bf 100644 --- a/apm_test.go +++ b/apm_test.go @@ -182,6 +182,21 @@ func TestAPMAnchoredLocalPaths(t *testing.T) { } } +func TestAPMLocatorKeepsPathCase(t *testing.T) { + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: + - repo_url: https://gitlab.com/Team/Rules + resolved_commit: `+apmCommit+` + - repo_url: https://gitlab.com/team/rules + resolved_commit: `+apmCommit+` +`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if findDep(result.Direct, "gitlab.com/Team/Rules") == nil || findDep(result.Direct, "gitlab.com/team/rules") == nil { + t.Errorf("direct = %+v, want both GitLab paths", result.Direct) + } +} + func TestAPMGraphErrors(t *testing.T) { commit := " resolved_commit: " + apmCommit + "\n" tests := []struct { @@ -198,6 +213,11 @@ func TestAPMGraphErrors(t *testing.T) { " - repo_url: acme/skills\n virtual_path: review\n is_virtual: true\n" + commit + " - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n", parsers.ErrAPMAmbiguousParent}, + {"case-folded repo_url on two hosts", "dependencies:\n" + + " - repo_url: ACME/Skills\n" + commit + + " - repo_url: acme/skills\n host: gitlab.com\n" + commit + + " - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n", + parsers.ErrAPMAmbiguousParent}, {"missing parent", "dependencies:\n" + " - repo_url: acme/orphan\n" + commit + " depth: 2\n resolved_by: acme/gone\n", parsers.ErrAPMMissingParent}, diff --git a/parsers/apm.go b/parsers/apm.go index 79a1de1..23ce6f7 100644 --- a/parsers/apm.go +++ b/parsers/apm.go @@ -43,6 +43,7 @@ type apmLockEntry struct { type apmNode struct { entry apmLockEntry depth int + repo string key string dep *resolve.Dep } @@ -92,6 +93,7 @@ func parseAPMDir(dir string) ([]*resolve.Dep, error) { return roots, nil } +// readAPMLockfile returns apm.lock.yaml, or apm.lock when it is absent. func readAPMLockfile(dir string) (string, []byte, error) { for _, name := range []string{"apm.lock.yaml", "apm.lock"} { content, err := os.ReadFile(filepath.Join(dir, name)) @@ -116,7 +118,8 @@ func apmNodes(entries []apmLockEntry) []*apmNode { if entry.RepoURL == "" || depth == 0 { continue } - nodes = append(nodes, &apmNode{entry: entry, depth: depth, key: apmLockKey(entry)}) + repo := apmRepo(entry) + nodes = append(nodes, &apmNode{entry: entry, depth: depth, repo: repo, key: apmLockKey(entry, repo)}) } return nodes } @@ -125,11 +128,11 @@ func apmNodes(entries []apmLockEntry) []*apmNode { func apmParent(nodes []*apmNode, child *apmNode) (*apmNode, error) { ref := child.entry.ResolvedBy // APM writes bare repo_urls for v1 and v2; a fuller key is exact when one appears. - explicit := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref && n.entry.RepoURL != ref }) + explicit := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref && n.repo != ref }) if len(explicit) == 1 { return explicit[0], nil } - candidates := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref || n.entry.RepoURL == ref }) + candidates := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref || n.repo == ref }) switch len(candidates) { case 0: return nil, fmt.Errorf("%w: %s at depth %d has resolved_by %q", ErrAPMMissingParent, child.key, child.depth, ref) @@ -140,6 +143,7 @@ func apmParent(nodes []*apmNode, child *apmNode) (*apmNode, error) { } } +// apmCandidates returns the previous-depth entries that match. func apmCandidates(nodes []*apmNode, child *apmNode, match func(*apmNode) bool) []*apmNode { var candidates []*apmNode for _, n := range nodes { @@ -150,8 +154,29 @@ func apmCandidates(nodes []*apmNode, child *apmNode, match func(*apmNode) bool) return candidates } +// apmRepo returns repo_url case-folded as APM loads it. +func apmRepo(entry apmLockEntry) string { + if entry.Source == "local" { + return entry.RepoURL + } + if entry.Source == "registry" || entry.RegistryPrefix != "" { + return strings.ToLower(entry.RepoURL) + } + host := strings.ToLower(strings.TrimSpace(entry.Host)) + if (host == "" || host == "github.com" || strings.HasSuffix(host, ".ghe.com")) && !apmLocator(entry.RepoURL) { + return strings.ToLower(entry.RepoURL) + } + return entry.RepoURL +} + +// apmLocator reports a URL or host-qualified repo_url, which APM does not write. +func apmLocator(repo string) bool { + first, rest, _ := strings.Cut(repo, "/") + return strings.Contains(repo, ":") || (strings.Contains(first, ".") && strings.Contains(rest, "/")) +} + // apmLockKey mirrors APM's lockfile identity key. -func apmLockKey(entry apmLockEntry) string { +func apmLockKey(entry apmLockEntry, repo string) string { if entry.Source == "local" && entry.LocalPath != "" { if entry.AnchoredLocal != "" { return "local:" + entry.AnchoredLocal @@ -159,10 +184,7 @@ func apmLockKey(entry apmLockEntry) string { return entry.LocalPath } host := strings.ToLower(strings.TrimSpace(entry.Host)) - key := entry.RepoURL - if entry.Source == "registry" || entry.RegistryPrefix != "" || host == "" || host == "github.com" || strings.HasSuffix(host, ".ghe.com") { - key = strings.ToLower(key) - } + key := repo if entry.IsVirtual && entry.VirtualPath != "" { key += "/" + entry.VirtualPath } From 4347708d45542769dace3aa7cda8be700acf2f84 Mon Sep 17 00:00:00 2001 From: abhinavgautam01 Date: Thu, 12 Nov 2026 10:11:42 +0530 Subject: [PATCH 5/5] Fold resolved_by with each candidate's case rules when matching APM parents --- apm_test.go | 37 +++++++++++++++++++ parsers/apm.go | 99 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 105 insertions(+), 31 deletions(-) diff --git a/apm_test.go b/apm_test.go index 3b3b5bf..5223223 100644 --- a/apm_test.go +++ b/apm_test.go @@ -148,6 +148,43 @@ func TestAPMParentByVirtualKey(t *testing.T) { } } +// Lockfiles written before microsoft/apm#3370 keep manifest casing in repo_url and resolved_by. +func TestAPMMixedCaseLegacyParent(t *testing.T) { + result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: + - repo_url: ComposioHQ/awesome-claude-skills + resolved_commit: `+apmCommit+` + - repo_url: acme/shared + resolved_commit: `+apmCommit+` + depth: 2 + resolved_by: ComposioHQ/awesome-claude-skills +`)) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(result.Direct) != 1 || findDep(result.Direct[0].Deps, "github.com/acme/shared") == nil { + t.Errorf("direct = %+v, want acme/shared under awesome-claude-skills", result.Direct) + } +} + +func TestAPMMixedCaseVirtualKey(t *testing.T) { + lock := `dependencies: + - repo_url: Acme/Skills + virtual_path: Review + is_virtual: true + resolved_commit: ` + apmCommit + ` + - repo_url: acme/shared + resolved_commit: ` + apmCommit + ` + depth: 2 + resolved_by: ` + if _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", lock+"Acme/Skills/Review\n")); err != nil { + t.Errorf("mixed-case repository: %v", err) + } + _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", lock+"acme/skills/review\n")) + if !errors.Is(err, parsers.ErrAPMMissingParent) { + t.Errorf("lowercased virtual path: err = %v, want ErrAPMMissingParent", err) + } +} + func TestAPMAnchoredLocalPaths(t *testing.T) { result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies: - repo_url: _local/a diff --git a/parsers/apm.go b/parsers/apm.go index 23ce6f7..88f2ceb 100644 --- a/parsers/apm.go +++ b/parsers/apm.go @@ -41,11 +41,13 @@ type apmLockEntry struct { // apmNode is one lockfile entry with its identity and graph position. type apmNode struct { - entry apmLockEntry - depth int - repo string - key string - dep *resolve.Dep + entry apmLockEntry + depth int + folded bool // repository path is case-insensitive + repo string // repo_url as APM loads it + host string // host prefix of key, if any + key string + dep *resolve.Dep } // parseAPMDir builds the graph from manifests' APM identities plus depth and resolved_by. @@ -118,8 +120,7 @@ func apmNodes(entries []apmLockEntry) []*apmNode { if entry.RepoURL == "" || depth == 0 { continue } - repo := apmRepo(entry) - nodes = append(nodes, &apmNode{entry: entry, depth: depth, repo: repo, key: apmLockKey(entry, repo)}) + nodes = append(nodes, newAPMNode(entry, depth)) } return nodes } @@ -128,11 +129,11 @@ func apmNodes(entries []apmLockEntry) []*apmNode { func apmParent(nodes []*apmNode, child *apmNode) (*apmNode, error) { ref := child.entry.ResolvedBy // APM writes bare repo_urls for v1 and v2; a fuller key is exact when one appears. - explicit := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref && n.repo != ref }) + explicit := apmCandidates(nodes, child, func(n *apmNode) bool { return n.matchesKey(ref) && !n.matchesRepo(ref) }) if len(explicit) == 1 { return explicit[0], nil } - candidates := apmCandidates(nodes, child, func(n *apmNode) bool { return n.key == ref || n.repo == ref }) + candidates := apmCandidates(nodes, child, func(n *apmNode) bool { return n.matchesKey(ref) || n.matchesRepo(ref) }) switch len(candidates) { case 0: return nil, fmt.Errorf("%w: %s at depth %d has resolved_by %q", ErrAPMMissingParent, child.key, child.depth, ref) @@ -154,19 +155,39 @@ func apmCandidates(nodes []*apmNode, child *apmNode, match func(*apmNode) bool) return candidates } -// apmRepo returns repo_url case-folded as APM loads it. -func apmRepo(entry apmLockEntry) string { +// newAPMNode builds the node and its key as APM's build_dependency_unique_key does. +func newAPMNode(entry apmLockEntry, depth int) *apmNode { + n := &apmNode{entry: entry, depth: depth, folded: apmFolds(entry), repo: entry.RepoURL} + if n.folded { + n.repo = strings.ToLower(n.repo) + } + if entry.Source == "local" && entry.LocalPath != "" { + n.key = entry.LocalPath + if entry.AnchoredLocal != "" { + n.key = "local:" + entry.AnchoredLocal + } + return n + } + if host := strings.ToLower(strings.TrimSpace(entry.Host)); entry.RegistryPrefix == "" && host != "" && host != "github.com" { + n.host = host + } + n.key = n.repo + n.virtualSuffix() + if n.host != "" { + n.key = n.host + "/" + n.key + } + return n +} + +// apmFolds reports whether APM lowercases the entry's repo_url, as it does since microsoft/apm#3370. +func apmFolds(entry apmLockEntry) bool { if entry.Source == "local" { - return entry.RepoURL + return false } if entry.Source == "registry" || entry.RegistryPrefix != "" { - return strings.ToLower(entry.RepoURL) + return true } host := strings.ToLower(strings.TrimSpace(entry.Host)) - if (host == "" || host == "github.com" || strings.HasSuffix(host, ".ghe.com")) && !apmLocator(entry.RepoURL) { - return strings.ToLower(entry.RepoURL) - } - return entry.RepoURL + return (host == "" || host == "github.com" || strings.HasSuffix(host, ".ghe.com")) && !apmLocator(entry.RepoURL) } // apmLocator reports a URL or host-qualified repo_url, which APM does not write. @@ -175,23 +196,39 @@ func apmLocator(repo string) bool { return strings.Contains(repo, ":") || (strings.Contains(first, ".") && strings.Contains(rest, "/")) } -// apmLockKey mirrors APM's lockfile identity key. -func apmLockKey(entry apmLockEntry, repo string) string { - if entry.Source == "local" && entry.LocalPath != "" { - if entry.AnchoredLocal != "" { - return "local:" + entry.AnchoredLocal - } - return entry.LocalPath +// virtualSuffix returns "/virtual_path" for a virtual entry. +func (n *apmNode) virtualSuffix() string { + if n.entry.IsVirtual && n.entry.VirtualPath != "" { + return "/" + n.entry.VirtualPath } - host := strings.ToLower(strings.TrimSpace(entry.Host)) - key := repo - if entry.IsVirtual && entry.VirtualPath != "" { - key += "/" + entry.VirtualPath + return "" +} + +// matchesRepo compares ref to repo_url, folding ref by the node's case rules. +func (n *apmNode) matchesRepo(ref string) bool { + if n.folded { + ref = strings.ToLower(ref) } - if entry.RegistryPrefix == "" && host != "" && host != "github.com" { - key = host + "/" + key + return ref == n.repo +} + +// matchesKey compares ref to the full key, keeping the virtual path case-sensitive. +func (n *apmNode) matchesKey(ref string) bool { + if n.entry.Source == "local" && n.entry.LocalPath != "" { + return ref == n.key + } + rest, ok := strings.CutSuffix(ref, n.virtualSuffix()) + if !ok { + return false + } + if n.host != "" { + hostPart, path, ok := strings.Cut(rest, "/") + if !ok || !strings.EqualFold(hostPart, n.host) { + return false + } + rest = path } - return key + return n.matchesRepo(rest) } func init() {