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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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

Expand Down
303 changes: 303 additions & 0 deletions apm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
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 || 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")
if style == nil || style.PURL != "pkg:git/gitlab.com/team/style-guide@0f1e2d3c4b5a69788796a5b4c3d2e1f0a9b8c7d6" {
t.Errorf("style-guide = %+v", style)
}

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 TestAPMParentOnOtherHost(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: 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)
}
}

// 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
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)
}
}

// 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
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 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 {
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},
{"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},
{"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},
{"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)
}
}
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
14 changes: 12 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Loading
Loading