Skip to content

Lenient package.json parsing, migrate to version_spec, and protocol fixes - #32

Merged
milesj merged 5 commits into
masterfrom
lenient-package-json
Nov 17, 2026
Merged

milesj merged 5 commits into
masterfrom
lenient-package-json

Conversation

@milesj

@milesj milesj commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Summary

Makes package.json parsing lenient and replaces the semver crate with version_spec. It also fixes several protocol parsing and package graph bugs found in an audit. All of these change the public API, so nodejs_package_json and nodejs_package_graph need a minor version bump.

Changes

Lenient parsing

  • A field with an unexpected value or shape is skipped (None) instead of failing the whole package.json.
  • A dependency that can't be parsed becomes VersionProtocol::Unknown, including values that aren't strings, so one bad dependency no longer drops its whole map.
  • serde_json's preserve_order is now enabled. Lenient parsing goes through serde_json::Value, which sorted map keys when the crate was built on its own (the graph crate's dependencies enabled the feature, so its tests never noticed). That reordered exports conditions, which Node matches in order, along with scripts and engines.

semver → version_spec

  • Version is now version_spec::Version. A single requirement is Requirement, and anything compound (>=1 <2, 1.2.3 - 2.0.0, ^1 || ^2) is VersionProtocol::Range(Range), which mirrors UnresolvedVersionSpec.
  • version_spec parses npm spacing, wildcards, space-separated requirements, hyphen ranges and || itself, so clean_version is removed. The remaining npm differences are handled in a new protocols/range.rs:
    • A full version without an operator inside a range is an exact match (1.2.3 || 2.0.0 no longer matches 1.2.9).
    • Hyphen ranges with partial bounds, like 1.2 - 2, are converted for each || clause.
  • Ranges print with npm's syntax (>=1 <2), not version_spec's &&.
  • version_spec is optional and enabled by the protocols feature, as is its re-export.

Protocol fixes

  • Local paths (../foo, ./x.tgz, ~/x, /x, C:\x) parse as a new VersionProtocol::Path instead of being cut down to GitHub shorthand, like ../foo/bar → ../foo.
  • GitHub refs are kept whole (user/repo#semver:^5.0). Git URLs in git@host:repo form and the hosted shorthands (github:, gitlab:, bitbucket:, gist:) parse as Git.
  • x and X are wildcards instead of tags.
  • Workspace protocol:
    • Aliases follow pnpm's rule, so paths containing @ aren't split.
    • WorkspaceProtocol::Version keeps its alias.
    • Ranges are supported (WorkspaceProtocol::Range).
    • Non-ASCII values no longer panic.

Package graph fixes

  • dependents_of returned the queried package instead of its dependents.
  • workspace:^1.0.0 and workspace:~1.0.0 are matched as ranges instead of needing an exact version.
  • Range dependencies and bare local paths now create edges.
  • Build metadata is ignored when matching exact versions.
  • More than 255 workspace packages no longer overflows (Package.index is a usize).
  • bun.lock is detected, as well as bun.lockb.
  • Duplicate package names return PackageGraphError::DuplicatePackageName instead of silently overwriting each other.

Breaking changes

  • VersionReq → Requirement / Range. VersionProtocol::Range holds a Range, and compound requirements are now Range instead of Requirement.
  • New variants:
    • VersionProtocol::Path
    • WorkspaceProtocol::Range
    • PackageGraphError::DuplicatePackageName
  • WorkspaceProtocol::Version is now Version { alias, version }.
  • Renamed and removed error variants:
    • Semver → Spec
    • RangeMissingStartVersion and RangeMissingStopVersion are removed (they were unreachable).
  • Re-exports are only available with the protocols feature.
  • How values print has changed: 1.2.x → 1.2.*, a bare 1 → ~1, and >=1, <2 → >=1 <2.
  • Turning on preserve_order also turns it on for downstream crates.
  • Graph generation now fails on duplicate package names.

Testing

  • cargo test --workspace --all-features: 128 tests pass, with regression tests for each fix.
  • cargo clippy --workspace --all-targets --all-features -- -D warnings is clean.
  • nodejs_package_json builds with no features, protocols, serialize and miette,protocols.
  • The map-order test fails without preserve_order when run with -p nodejs_package_json.

🤖 Generated with Claude Code

milesj and others added 5 commits November 17, 2026 14:24
- Fix `dependents_of` returning the package itself instead of its dependents.
- Parse local paths (`../foo`, `~/foo`, `/foo`, `C:\foo`) as `VersionProtocol::Path`
  instead of GitHub shorthand, and SCP-like git URLs as `Git`.
- Keep the full GitHub reference, like `#semver:^5.0`.
- Match full versions without an operator in ranges exactly, like npm.
- Match `workspace:^1.0.0` and `workspace:~1.0.0` as ranges in the graph.
- Keep the alias for exact workspace versions (`WorkspaceProtocol::Version { alias, version }`).
- Don't split workspace paths containing `@`, and don't panic on non-ASCII values.
- Support more than 255 workspace packages.
- Detect Bun's `bun.lock` lockfile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Preserve the order of maps, like `exports` conditions, when parsing leniently,
  by enabling `serde_json`'s `preserve_order`.
- Convert partial hyphen ranges per `||` clause, like `1 - 2 || 3 - 4`.
- Deserialize dependencies that aren't strings as `Unknown`, instead of
  dropping the whole map.
- Error for duplicate package names in the graph.
- Parse `x` and `X` as wildcards.
- Parse hosted git shorthands, like `github:user/repo`, as `Git`.
- Ignore build metadata when linking exact versions in the graph.
- Support ranges in the workspace protocol (`WorkspaceProtocol::Range`).
- Remove the unreachable `RangeMissingStartVersion` and `RangeMissingStopVersion` errors.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@milesj
milesj merged commit 02d2a24 into master Nov 17, 2026
7 checks passed
@milesj
milesj deleted the lenient-package-json branch November 17, 2026 23:42
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