Skip to content

Cargo: lockfile-first analysis (parse Cargo.lock directly, fall back to cargo metadata) - #1855

Draft
csasarak wants to merge 21 commits into
masterfrom
read-cargo-lock
Draft

csasarak wants to merge 21 commits into
masterfrom
read-cargo-lock

Conversation

@csasarak

Copy link
Copy Markdown
Contributor

Summary

fossa analyze for Rust workspaces now prefers parsing Cargo.lock directly over shelling out to cargo metadata, falling back to the metadata path when the lockfile is missing or unparseable. This removes a Rust-toolchain dependency from the common case and makes results reflect exactly what is pinned.

How it works

  • Source decision (Strategy.Cargo.Lock): for each workspace member manifest found via the shared walk, decide between the lockfile and cargo metadata — lockfile when present and parseable, metadata otherwise (missing/unparseable lock, or analysis needs the toolchain). One warning surfaces the fallback reason.
  • Lockfile conversion: parseCargoLock (v3/v4) → member enumeration → analyzeManifests (kinds from each member's [dependencies]/[build-dependencies]/[dev-dependencies], incl. workspace = true inheritance) → lockfileToMetadata, which produces the same CargoMetadata shape the existing graph builder consumes.
  • Dependency kinds per resolved version: a member declaring the same crate at two semver-incompatible versions with different kinds (e.g. 0.12 prod + 0.11 build) now labels each lockfile entry by the requirement that matches the resolved version (Cargo caret/tilde/comparator semantics), falling back to the union when no requirement matches (drifted lockfile).
  • Unresolvable refs: lockfile dependencies entries that reference a package absent from the lockfile are omitted with a warning instead of producing a phantom vertex.
  • Git locators: the lockfile path honors useGitBackedCargoLocators from StrategyConfig on both dynamic and static-only paths.
  • analyze/buildGraph and the metadata path are untouched; Strategy.Cargo re-exports keep the old API surface.

Known gaps (documented, follow-ups)

  • Workspace exclude and out-of-tree member paths are not honored by the lockfile walk.
  • Non-member (non-workspace-root) crates default to production kind.

Testing

  • Unit: lockfile parser (v3/v4), workspace member enumeration (root-in-members dedup, globs), conversion, source decision, per-version kind tests (disambig, compat_unify, drifted, versionless fixtures), static-only wiring.
  • Integration: static lockfile analysis suites, metadata-fallback (lockfile removed), and unparseable-lock fallback suites (need the nix rust toolchain env to run).
  • cabal build clean; fourmolu/hlint clean.

…helpers

Parses Cargo.lock v3/v4 (TOML), enumerates workspace members from Cargo.toml, and derives per-member dependency kinds, feeding the lockfile-vs-metadata source decision.
Single crate, virtual workspace, and members+default-members enumeration over fixtures, registered in the unit-tests suite.
The primed variant is needed by the Cargo strategy's lockfile-first read path.
Prefer a present, parsable Cargo.lock over cargo metadata for full analysis, and require it for static analysis (with explicit fatals), keeping cargo metadata as the dynamic fallback.
Complete LockfileSpec (dependency-kind analysis, source decision, golden conversions) plus dynamic-mode analysis wiring specs, with their fixtures and test-suite registration.
analyzeProjectStaticOnly produces real results from a parsable Cargo.lock and fatals with distinct diagnostics for missing/unparseable lockfiles.
…llback suite

Integration fixtures run against local workspaces; the new suite covers the cargo-metadata fallback when Cargo.lock is absent.
…naryAnalysis field

origin/master added a 4th field (enableGoBinaryAnalysis) to StrategyConfig
after this feature branched, leaving the static-analysis test harness using
the stale 3-arg positional constructor. Add the field (False: Go-binary
analysis is irrelevant to Cargo static analysis and opt-in) so unit-tests
compiles. Part of the Cargo.lock-first feature.
Conflict: Strategy.Cargo export list — keep the branch's submodule
re-export structure, add master's discoverVisitor. Also drop an unused
Data.Bifunctor import and an unshadowed 'min' binding in
Strategy.Cargo.Lock surfaced by the rebuild.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant