Skip to content

Commit 7112cc1

Browse files
authored
Merge pull request #17 from abhinavgautam01/apm-lockfile-graph
Read APM dependency graphs from the lockfile
2 parents 130c870 + 4347708 commit 7112cc1

6 files changed

Lines changed: 624 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ result, err := resolve.Parse("npm", output)
2626
// result.Direct == []*Dep{ {PURL: "pkg:npm/express@4.18.2", Name: "express", Version: "4.18.2", Deps: [...]}, ... }
2727
```
2828

29-
`Parse` is the only entry point. It dispatches to the correct parser based on the manager name and returns `ErrUnsupportedManager` for unknown managers.
29+
`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.
3030

3131
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.
3232

@@ -58,6 +58,13 @@ Each `Dep` includes the ecosystem-native package name, resolved version, a PURL
5858
| conan | conan | Custom |
5959
| deno | deno | JSON flat |
6060
| helm | helm | Tabular |
61+
| apm | apm | Lockfile graph |
62+
63+
## Directory parsers
64+
65+
Some managers record their resolved graph in a lockfile rather than in command output. `ParseDir(manager, dir)` reads those files from a project directory.
66+
67+
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.
6168

6269
## License
6370

‎apm_test.go‎

Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
1+
package resolve_test
2+
3+
import (
4+
"errors"
5+
"os"
6+
"path/filepath"
7+
"reflect"
8+
"testing"
9+
10+
"github.com/git-pkgs/resolve"
11+
"github.com/git-pkgs/resolve/parsers"
12+
)
13+
14+
const (
15+
apmSkillsCommit = "7f3c9a4d2e1b8c7f0a9e6d5c4b3a2918f7e6d5c4"
16+
apmCommit = "0123456789abcdef0123456789abcdef01234567"
17+
)
18+
19+
func writeAPMLockfile(t *testing.T, name, content string) string {
20+
t.Helper()
21+
dir := t.TempDir()
22+
if err := os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644); err != nil {
23+
t.Fatal(err)
24+
}
25+
return dir
26+
}
27+
28+
func TestAPM(t *testing.T) {
29+
result, err := resolve.ParseDir("apm", filepath.Join("testdata", "apm"))
30+
if err != nil {
31+
t.Fatalf("unexpected error: %v", err)
32+
}
33+
if result.Manager != "apm" || result.Ecosystem != "apm" {
34+
t.Errorf("result = %s/%s, want apm/apm", result.Manager, result.Ecosystem)
35+
}
36+
37+
type node struct{ name, version, purl string }
38+
var roots []node
39+
for _, dep := range result.Direct {
40+
roots = append(roots, node{dep.Name, dep.Version, dep.PURL})
41+
}
42+
want := []node{
43+
{"github.com/acme-corp/security-baseline", "a1b2c3d4e5f6789012345678901234567890abcd",
44+
"pkg:git/github.com/acme-corp/security-baseline@a1b2c3d4e5f6789012345678901234567890abcd"},
45+
{"github.com/composiohq/awesome-claude-skills/brand-guidelines", apmSkillsCommit,
46+
"pkg:git/github.com/composiohq/awesome-claude-skills@" + apmSkillsCommit + "#brand-guidelines"},
47+
{"github.com/composiohq/awesome-claude-skills/canvas-design", apmSkillsCommit,
48+
"pkg:git/github.com/composiohq/awesome-claude-skills@" + apmSkillsCommit + "#canvas-design"},
49+
// Registry and local sources have no package coordinates.
50+
{"acme/toolkit", "2.3.1", ""},
51+
{"./packages/shared-skills", "0.1.0", ""},
52+
{"github.com/owner/test-helpers", apmCommit, "pkg:git/github.com/owner/test-helpers@" + apmCommit},
53+
}
54+
if !reflect.DeepEqual(roots, want) {
55+
t.Fatalf("direct =\n%+v\nwant\n%+v", roots, want)
56+
}
57+
58+
// Transitive chain resolved by repo_url, across hosts.
59+
baseline := findDep(result.Direct, "github.com/acme-corp/security-baseline")
60+
prompts := findDep(baseline.Deps, "github.com/acme-corp/common-prompts")
61+
if prompts == nil || findDep(baseline.Deps, "github.com/composiohq/design-tokens") == nil || len(baseline.Deps) != 2 {
62+
t.Fatalf("security-baseline deps = %+v", baseline.Deps)
63+
}
64+
style := findDep(prompts.Deps, "gitlab.com/team/style-guide")
65+
if style == nil || style.PURL != "pkg:git/gitlab.com/team/style-guide@0f1e2d3c4b5a69788796a5b4c3d2e1f0a9b8c7d6" {
66+
t.Errorf("style-guide = %+v", style)
67+
}
68+
69+
for _, dep := range result.Direct {
70+
if dep.Deps == nil {
71+
t.Errorf("%s Deps is nil, want an empty slice for a tree manager", dep.Name)
72+
}
73+
}
74+
}
75+
76+
func TestAPMLegacyLockfile(t *testing.T) {
77+
content, err := os.ReadFile(filepath.Join("testdata", "apm", "apm.lock.yaml"))
78+
if err != nil {
79+
t.Fatal(err)
80+
}
81+
current, err := resolve.ParseDir("apm", filepath.Join("testdata", "apm"))
82+
if err != nil {
83+
t.Fatal(err)
84+
}
85+
legacy, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock", string(content)))
86+
if err != nil {
87+
t.Fatalf("legacy apm.lock: %v", err)
88+
}
89+
if !reflect.DeepEqual(legacy.Direct, current.Direct) {
90+
t.Error("legacy apm.lock graph differs from apm.lock.yaml")
91+
}
92+
}
93+
94+
func TestAPMPrefersCurrentLockfile(t *testing.T) {
95+
dir := writeAPMLockfile(t, "apm.lock.yaml", "dependencies:\n - repo_url: owner/current\n resolved_commit: "+apmCommit+"\n")
96+
if err := os.WriteFile(filepath.Join(dir, "apm.lock"), []byte("dependencies:\n - repo_url: owner/legacy\n"), 0o644); err != nil {
97+
t.Fatal(err)
98+
}
99+
result, err := resolve.ParseDir("apm", dir)
100+
if err != nil {
101+
t.Fatal(err)
102+
}
103+
if len(result.Direct) != 1 || result.Direct[0].Name != "github.com/owner/current" {
104+
t.Errorf("direct = %+v, want only github.com/owner/current", result.Direct)
105+
}
106+
}
107+
108+
func TestAPMParentOnOtherHost(t *testing.T) {
109+
result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies:
110+
- repo_url: team/rules
111+
host: gitlab.example.com
112+
resolved_commit: `+apmCommit+`
113+
- repo_url: team/shared
114+
host: gitlab.example.com
115+
resolved_commit: `+apmCommit+`
116+
depth: 2
117+
resolved_by: team/rules
118+
`))
119+
if err != nil {
120+
t.Fatal(err)
121+
}
122+
if len(result.Direct) != 1 || findDep(result.Direct[0].Deps, "gitlab.example.com/team/shared") == nil {
123+
t.Errorf("direct = %+v, want team/shared under team/rules", result.Direct)
124+
}
125+
}
126+
127+
// Full keys are what replacement lockfiles write; v1 and v2 use bare repo_urls.
128+
func TestAPMParentByVirtualKey(t *testing.T) {
129+
result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies:
130+
- repo_url: acme/skills
131+
resolved_commit: `+apmCommit+`
132+
- repo_url: acme/skills
133+
virtual_path: review
134+
is_virtual: true
135+
resolved_commit: `+apmCommit+`
136+
- repo_url: acme/shared
137+
resolved_commit: `+apmCommit+`
138+
depth: 2
139+
resolved_by: acme/skills/review
140+
`))
141+
if err != nil {
142+
t.Fatal(err)
143+
}
144+
review := findDep(result.Direct, "github.com/acme/skills/review")
145+
whole := findDep(result.Direct, "github.com/acme/skills")
146+
if review == nil || findDep(review.Deps, "github.com/acme/shared") == nil || len(whole.Deps) != 0 {
147+
t.Errorf("direct = %+v, want acme/shared under the review subpackage only", result.Direct)
148+
}
149+
}
150+
151+
// Lockfiles written before microsoft/apm#3370 keep manifest casing in repo_url and resolved_by.
152+
func TestAPMMixedCaseLegacyParent(t *testing.T) {
153+
result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies:
154+
- repo_url: ComposioHQ/awesome-claude-skills
155+
resolved_commit: `+apmCommit+`
156+
- repo_url: acme/shared
157+
resolved_commit: `+apmCommit+`
158+
depth: 2
159+
resolved_by: ComposioHQ/awesome-claude-skills
160+
`))
161+
if err != nil {
162+
t.Fatalf("unexpected error: %v", err)
163+
}
164+
if len(result.Direct) != 1 || findDep(result.Direct[0].Deps, "github.com/acme/shared") == nil {
165+
t.Errorf("direct = %+v, want acme/shared under awesome-claude-skills", result.Direct)
166+
}
167+
}
168+
169+
func TestAPMMixedCaseVirtualKey(t *testing.T) {
170+
lock := `dependencies:
171+
- repo_url: Acme/Skills
172+
virtual_path: Review
173+
is_virtual: true
174+
resolved_commit: ` + apmCommit + `
175+
- repo_url: acme/shared
176+
resolved_commit: ` + apmCommit + `
177+
depth: 2
178+
resolved_by: `
179+
if _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", lock+"Acme/Skills/Review\n")); err != nil {
180+
t.Errorf("mixed-case repository: %v", err)
181+
}
182+
_, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", lock+"acme/skills/review\n"))
183+
if !errors.Is(err, parsers.ErrAPMMissingParent) {
184+
t.Errorf("lowercased virtual path: err = %v, want ErrAPMMissingParent", err)
185+
}
186+
}
187+
188+
func TestAPMAnchoredLocalPaths(t *testing.T) {
189+
result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies:
190+
- repo_url: _local/a
191+
source: local
192+
local_path: ./packages/a
193+
- repo_url: _local/b
194+
source: local
195+
local_path: ./tools/b
196+
- repo_url: _local/sibling
197+
source: local
198+
local_path: ../sibling
199+
anchored_local_path: packages/sibling
200+
depth: 2
201+
resolved_by: _local/a
202+
- repo_url: _local/sibling
203+
source: local
204+
local_path: ../sibling
205+
anchored_local_path: tools/sibling
206+
depth: 2
207+
resolved_by: _local/b
208+
`))
209+
if err != nil {
210+
t.Fatalf("unexpected error: %v", err)
211+
}
212+
if len(result.Direct) != 2 {
213+
t.Fatalf("direct = %+v, want two local parents", result.Direct)
214+
}
215+
for _, parent := range result.Direct {
216+
if len(parent.Deps) != 1 || parent.Deps[0].Name != "../sibling" {
217+
t.Errorf("%s deps = %+v, want one ../sibling", parent.Name, parent.Deps)
218+
}
219+
}
220+
}
221+
222+
func TestAPMLocatorKeepsPathCase(t *testing.T) {
223+
result, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", `dependencies:
224+
- repo_url: https://gitlab.com/Team/Rules
225+
resolved_commit: `+apmCommit+`
226+
- repo_url: https://gitlab.com/team/rules
227+
resolved_commit: `+apmCommit+`
228+
`))
229+
if err != nil {
230+
t.Fatalf("unexpected error: %v", err)
231+
}
232+
if findDep(result.Direct, "gitlab.com/Team/Rules") == nil || findDep(result.Direct, "gitlab.com/team/rules") == nil {
233+
t.Errorf("direct = %+v, want both GitLab paths", result.Direct)
234+
}
235+
}
236+
237+
func TestAPMGraphErrors(t *testing.T) {
238+
commit := " resolved_commit: " + apmCommit + "\n"
239+
tests := []struct {
240+
name, content string
241+
want error
242+
}{
243+
{"ambiguous parent", "dependencies:\n" +
244+
" - repo_url: acme/skills\n virtual_path: review\n is_virtual: true\n" + commit +
245+
" - repo_url: acme/skills\n virtual_path: deploy\n is_virtual: true\n" + commit +
246+
" - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n",
247+
parsers.ErrAPMAmbiguousParent},
248+
{"repo_url shared by package and subpackage", "dependencies:\n" +
249+
" - repo_url: acme/skills\n" + commit +
250+
" - repo_url: acme/skills\n virtual_path: review\n is_virtual: true\n" + commit +
251+
" - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n",
252+
parsers.ErrAPMAmbiguousParent},
253+
{"case-folded repo_url on two hosts", "dependencies:\n" +
254+
" - repo_url: ACME/Skills\n" + commit +
255+
" - repo_url: acme/skills\n host: gitlab.com\n" + commit +
256+
" - repo_url: acme/shared\n" + commit + " depth: 2\n resolved_by: acme/skills\n",
257+
parsers.ErrAPMAmbiguousParent},
258+
{"missing parent", "dependencies:\n" +
259+
" - repo_url: acme/orphan\n" + commit + " depth: 2\n resolved_by: acme/gone\n",
260+
parsers.ErrAPMMissingParent},
261+
{"parent at wrong depth", "dependencies:\n" +
262+
" - repo_url: acme/root\n" + commit +
263+
" - repo_url: acme/deep\n" + commit + " depth: 3\n resolved_by: acme/root\n",
264+
parsers.ErrAPMMissingParent},
265+
{"cyclic parents", "dependencies:\n" +
266+
" - repo_url: acme/a\n" + commit + " depth: 2\n resolved_by: acme/b\n" +
267+
" - repo_url: acme/b\n" + commit + " depth: 2\n resolved_by: acme/a\n",
268+
parsers.ErrAPMMissingParent},
269+
{"duplicate entry", "dependencies:\n" +
270+
" - repo_url: acme/twice\n" + commit +
271+
" - repo_url: acme/twice\n" + commit,
272+
parsers.ErrAPMDuplicateEntry},
273+
}
274+
for _, tt := range tests {
275+
t.Run(tt.name, func(t *testing.T) {
276+
_, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", tt.content))
277+
if !errors.Is(err, tt.want) {
278+
t.Errorf("err = %v, want %v", err, tt.want)
279+
}
280+
})
281+
}
282+
}
283+
284+
func TestAPMInvalidLockfile(t *testing.T) {
285+
for name, content := range map[string]string{
286+
"malformed YAML": "dependencies: [unterminated\n",
287+
"missing repo_url": "dependencies:\n - resolved_commit: " + apmCommit + "\n",
288+
"unsupported version": "lockfile_version: \"3\"\nreplacements: []\ndependencies: []\n",
289+
} {
290+
t.Run(name, func(t *testing.T) {
291+
if _, err := resolve.ParseDir("apm", writeAPMLockfile(t, "apm.lock.yaml", content)); err == nil {
292+
t.Error("expected error")
293+
}
294+
})
295+
}
296+
}
297+
298+
func TestAPMMissingLockfile(t *testing.T) {
299+
_, err := resolve.ParseDir("apm", t.TempDir())
300+
if !errors.Is(err, parsers.ErrAPMLockfileMissing) {
301+
t.Errorf("err = %v, want ErrAPMLockfileMissing", err)
302+
}
303+
}

‎go.mod‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
module github.com/git-pkgs/resolve
22

3-
go 1.26.0
4-
5-
toolchain go1.26.7
6-
7-
require github.com/git-pkgs/purl v0.2.1
3+
go 1.27
84

95
require (
10-
github.com/package-url/packageurl-go v0.1.7 // indirect
11-
go.yaml.in/yaml/v3 v3.0.5 // indirect
6+
github.com/git-pkgs/managers v0.12.0
7+
github.com/git-pkgs/manifests v0.14.0
8+
github.com/git-pkgs/purl v0.2.1
9+
go.yaml.in/yaml/v3 v3.0.6
1210
)
1311

1412
require (
15-
github.com/git-pkgs/managers v0.12.0
13+
github.com/BurntSushi/toml v1.6.0 // indirect
14+
github.com/bazelbuild/buildtools v0.0.0-20260716142318-04cf7de1434f // indirect
15+
github.com/bmatcuk/doublestar/v4 v4.10.4 // indirect
16+
github.com/git-pkgs/pom v0.1.8 // indirect
1617
github.com/git-pkgs/vers v0.7.2 // indirect
18+
github.com/package-url/packageurl-go v0.1.7 // indirect
1719
)

‎go.sum‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1+
github.com/BurntSushi/toml v1.6.0 h1:dRaEfpa2VI55EwlIW72hMRHdWouJeRF7TPYhI+AUQjk=
2+
github.com/BurntSushi/toml v1.6.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
3+
github.com/bazelbuild/buildtools v0.0.0-20260716142318-04cf7de1434f h1:2mT6QcXmMFtvg7bezs1Fef7nnpJyeCmfWoWNjwtvNZ4=
4+
github.com/bazelbuild/buildtools v0.0.0-20260716142318-04cf7de1434f/go.mod h1:PLNUetjLa77TCCziPsz0EI8a6CUxgC+1jgmWv0H25tg=
5+
github.com/bmatcuk/doublestar/v4 v4.10.4 h1:oTKkRmRuOsKMqFe1AQazd9gGBFNEpOmgKp/xzB97dnA=
6+
github.com/bmatcuk/doublestar/v4 v4.10.4/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
17
github.com/git-pkgs/managers v0.12.0 h1:Ex0fZ7hOzoxzXYLmY5ghxqel6jAltaehuGOcPFeQz4U=
28
github.com/git-pkgs/managers v0.12.0/go.mod h1:gWk/V63R12skgcTmVsYgED/kxlk2MERS2iWMYKBrJeM=
9+
github.com/git-pkgs/manifests v0.14.0 h1:UE+kxktTLN77XiaklhlhzG0zD8GAd/g2lOjQepO6LYI=
10+
github.com/git-pkgs/manifests v0.14.0/go.mod h1:+yP/Ivk5j9MDfs7G70BlzxWnFB8q+kFOWtbLhXd+OT0=
11+
github.com/git-pkgs/pom v0.1.8 h1:mXCuWw6XkrKKZb6IurRZyIznYLPhV5AYg5ZO38Uj/l8=
12+
github.com/git-pkgs/pom v0.1.8/go.mod h1:sQBeYRzjCUn77fcABPn6I/a1aBHFxm55jTmkNxYtGtw=
313
github.com/git-pkgs/purl v0.2.1 h1:b46hRlOMbrG6aXjjFJdYixGi5WTh0TLlAnH9iNeuOS0=
414
github.com/git-pkgs/purl v0.2.1/go.mod h1:l8rCfvt5iXVrFIcoQ8+vCLOaHhrtEW2id0LENP+IzMw=
515
github.com/git-pkgs/vers v0.7.2 h1:Ctex1O+Ai/FONmyDaw37EolBXg0og6UdxbCjjYbeRHs=
616
github.com/git-pkgs/vers v0.7.2/go.mod h1:ofLiBpPNkQmC0LB1k0zmN1gCv7Hi3MiZx8WtmWZ4A9A=
717
github.com/package-url/packageurl-go v0.1.7 h1:iFWg6tzAjLA6F/qX3M5nZaiMHJgc+p2zxVyr/fY+sZY=
818
github.com/package-url/packageurl-go v0.1.7/go.mod h1:nKAWB8E6uk1MHqiS/lQb9pYBGH2+mdJ2PJc2s50dQY0=
9-
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
10-
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
19+
go.yaml.in/yaml/v3 v3.0.6 h1:zcDfvjXEqTQmpjniUBLc91xYJMB2c4QOFxEF/LCEckM=
20+
go.yaml.in/yaml/v3 v3.0.6/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=

0 commit comments

Comments
 (0)