Files
integraci-dagger-modules/prebuild-check/lockfile.go
T

325 lines
9.3 KiB
Go

package main
// Lockfile present-and-in-sync check (ADR-0093 §10 item 2, WP-13 13b).
//
// Pure: it judges a map of repository-relative path -> file content, so it is
// tested without a Dagger engine. main.go reads the files from the mounted
// source and posts the result.
//
// Outcomes (the ADR-0093 §10 rule):
//
// not_applicable no package ecosystem in the repository (a static site): passes
// ran every declared ecosystem was checked; findings list what failed
//
// "No lockfile for a declared ecosystem" is a FINDING (lockfile_missing), never
// a pass and never could_not_run: the check ran and the lockfile is not there.
import (
"encoding/json"
"path"
"sort"
"strings"
)
// LockfileFinding is one lockfile problem. Kind is lockfile_missing,
// lockfile_out_of_sync or lockfile_changed_without_manifest.
type LockfileFinding struct {
Kind string `json:"kind"`
Ecosystem string `json:"ecosystem"`
Manifest string `json:"manifest"`
Lockfile string `json:"lockfile,omitempty"`
Detail string `json:"detail,omitempty"`
}
// LockfileReport is what the check posts (report_data of scan_type lockfile).
type LockfileReport struct {
Outcome string `json:"outcome"`
Ecosystems []string `json:"ecosystems"`
Findings []LockfileFinding `json:"lockfile_findings"`
}
// skippedDir reports whether a path lies in a vendored / generated tree whose
// manifests are not the repository's own.
func skippedDir(p string) bool {
for _, seg := range strings.Split(p, "/") {
switch seg {
case "node_modules", "vendor", ".git", "dist", "build", "target", ".venv", "venv", "__pycache__":
return true
}
}
return false
}
func exists(files map[string]string, p string) bool {
_, ok := files[p]
return ok
}
func firstExisting(files map[string]string, dir string, names ...string) string {
for _, n := range names {
p := path.Join(dir, n)
if exists(files, p) {
return p
}
}
return ""
}
// pythonLockfiles returns the lockfile a pyproject.toml's declared tool needs,
// or "" when it declares no locking tool (PEP 621 metadata alone).
func pythonTool(pyproject string) (tool, lock string) {
switch {
case strings.Contains(pyproject, "[tool.poetry"):
return "poetry", "poetry.lock"
case strings.Contains(pyproject, "[tool.uv"):
return "uv", "uv.lock"
case strings.Contains(pyproject, "[tool.pdm"):
return "pdm", "pdm.lock"
}
return "", ""
}
// npmManifestDeps returns the dependency names a package.json declares.
func npmManifestDeps(content string) (map[string]bool, bool) {
var pkg struct {
Dependencies map[string]any `json:"dependencies"`
DevDependencies map[string]any `json:"devDependencies"`
}
if err := json.Unmarshal([]byte(content), &pkg); err != nil {
return nil, false
}
out := map[string]bool{}
for k := range pkg.Dependencies {
out[k] = true
}
for k := range pkg.DevDependencies {
out[k] = true
}
return out, true
}
// npmLockRootDeps returns the root dependency names a package-lock.json v2/v3
// records (packages[""]), and false when the lock has no such section (v1).
func npmLockRootDeps(content string) (map[string]bool, bool) {
var lock struct {
Packages map[string]struct {
Dependencies map[string]any `json:"dependencies"`
DevDependencies map[string]any `json:"devDependencies"`
} `json:"packages"`
}
if err := json.Unmarshal([]byte(content), &lock); err != nil {
return nil, false
}
root, ok := lock.Packages[""]
if !ok {
return nil, false
}
out := map[string]bool{}
for k := range root.Dependencies {
out[k] = true
}
for k := range root.DevDependencies {
out[k] = true
}
return out, true
}
func setDiff(a, b map[string]bool) []string {
var out []string
for k := range a {
if !b[k] {
out = append(out, k)
}
}
sort.Strings(out)
return out
}
func goModRequires(content string) bool {
for _, line := range strings.Split(content, "\n") {
l := strings.TrimSpace(line)
if strings.HasPrefix(l, "require ") || l == "require (" || strings.HasPrefix(l, "require(") {
return true
}
}
return false
}
// CheckLockfiles judges every manifest under files. changed lists the
// repository-relative paths the change touched (the diff range); nil when the
// job does not know it, which skips only the changed-without-manifest rule.
func CheckLockfiles(files map[string]string, changed []string) LockfileReport {
ecos := map[string]bool{}
var findings []LockfileFinding
paths := make([]string, 0, len(files))
for p := range files {
if !skippedDir(p) {
paths = append(paths, p)
}
}
sort.Strings(paths)
// manifest -> lockfile actually used, for the changed-without-manifest rule.
pairs := map[string]string{}
missing := func(eco, manifest, lock string) {
findings = append(findings, LockfileFinding{
Kind: "lockfile_missing", Ecosystem: eco, Manifest: manifest, Lockfile: lock,
})
}
for _, p := range paths {
dir, base := path.Dir(p), path.Base(p)
switch base {
case "package.json":
ecos["npm"] = true
lock := firstExisting(files, dir, "package-lock.json", "npm-shrinkwrap.json", "yarn.lock", "pnpm-lock.yaml", "bun.lockb", "bun.lock")
if lock == "" {
// A workspace member is locked by the root's lockfile.
if root := firstExisting(files, ".", "package-lock.json", "yarn.lock", "pnpm-lock.yaml", "bun.lock"); root != "" && dir != "." {
pairs[p] = root
continue
}
missing("npm", p, path.Join(dir, "package-lock.json"))
continue
}
pairs[p] = lock
if path.Base(lock) == "package-lock.json" {
want, okM := npmManifestDeps(files[p])
have, okL := npmLockRootDeps(files[lock])
if okM && okL {
if add, drop := setDiff(want, have), setDiff(have, want); len(add)+len(drop) > 0 {
findings = append(findings, LockfileFinding{
Kind: "lockfile_out_of_sync", Ecosystem: "npm", Manifest: p, Lockfile: lock,
Detail: "not in lockfile: " + strings.Join(add, ",") + "; only in lockfile: " + strings.Join(drop, ","),
})
}
}
}
case "pyproject.toml":
tool, lockName := pythonTool(files[p])
if tool == "" {
continue
}
ecos["pypi"] = true
if lock := firstExisting(files, dir, lockName); lock != "" {
pairs[p] = lock
} else {
missing("pypi", p, path.Join(dir, lockName))
}
case "requirements.in":
ecos["pypi"] = true
if lock := firstExisting(files, dir, "requirements.txt"); lock != "" {
pairs[p] = lock
} else {
missing("pypi", p, path.Join(dir, "requirements.txt"))
}
case "Pipfile":
ecos["pypi"] = true
if lock := firstExisting(files, dir, "Pipfile.lock"); lock != "" {
pairs[p] = lock
} else {
missing("pypi", p, path.Join(dir, "Pipfile.lock"))
}
case "requirements.txt":
// Its own lock when every line is pinned; pinning is the pinning check's job.
ecos["pypi"] = true
case "go.mod":
ecos["go"] = true
if !goModRequires(files[p]) {
continue
}
if lock := firstExisting(files, dir, "go.sum"); lock != "" {
pairs[p] = lock
} else {
missing("go", p, path.Join(dir, "go.sum"))
}
case "Cargo.toml":
ecos["cargo"] = true
// A workspace member is locked at the workspace root.
if lock := firstExisting(files, dir, "Cargo.lock"); lock != "" {
pairs[p] = lock
} else if !exists(files, "Cargo.lock") {
missing("cargo", p, path.Join(dir, "Cargo.lock"))
}
case "composer.json":
ecos["composer"] = true
if lock := firstExisting(files, dir, "composer.lock"); lock != "" {
pairs[p] = lock
} else {
missing("composer", p, path.Join(dir, "composer.lock"))
}
case "Gemfile":
ecos["ruby"] = true
if lock := firstExisting(files, dir, "Gemfile.lock"); lock != "" {
pairs[p] = lock
} else {
missing("ruby", p, path.Join(dir, "Gemfile.lock"))
}
case "build.gradle", "build.gradle.kts":
// Gradle locking is opt-in ("where used"): only a build that turned
// dependency locking on needs gradle.lockfile.
if !strings.Contains(files[p], "dependencyLocking") {
continue
}
ecos["gradle"] = true
if lock := firstExisting(files, dir, "gradle.lockfile"); lock != "" {
pairs[p] = lock
} else {
missing("gradle", p, path.Join(dir, "gradle.lockfile"))
}
}
}
if changed != nil {
touched := map[string]bool{}
for _, c := range changed {
touched[strings.TrimPrefix(strings.TrimSpace(c), "./")] = true
}
manifests := make([]string, 0, len(pairs))
for m := range pairs {
manifests = append(manifests, m)
}
sort.Strings(manifests)
for _, m := range manifests {
lock := pairs[m]
if touched[lock] && !touched[m] {
findings = append(findings, LockfileFinding{
Kind: "lockfile_changed_without_manifest", Ecosystem: ecoOf(path.Base(m)),
Manifest: m, Lockfile: lock,
})
}
}
}
report := LockfileReport{Outcome: "ran", Findings: findings}
for e := range ecos {
report.Ecosystems = append(report.Ecosystems, e)
}
sort.Strings(report.Ecosystems)
if len(report.Ecosystems) == 0 {
report.Outcome = "not_applicable"
}
if report.Findings == nil {
report.Findings = []LockfileFinding{}
}
return report
}
func ecoOf(manifest string) string {
switch manifest {
case "package.json":
return "npm"
case "pyproject.toml", "requirements.in", "Pipfile":
return "pypi"
case "go.mod":
return "go"
case "Cargo.toml":
return "cargo"
case "composer.json":
return "composer"
case "Gemfile":
return "ruby"
}
return "gradle"
}