# Yarn PnP integrity in Bazel

*Use the project's existing dependency state, verify that the pieces agree, then allow code to load.*

A build system needs to know more than which dependency names a project declared. It needs to know which files those names resolve to, whether the resolver and dependency graph agree, and whether the files available at execution match the inputs the project intended.

My rules\_js contribution explores that boundary for Yarn Plug'n'Play, which resolves packages without a conventional node\_modules tree. After maintainer feedback, I moved from an exporter design to a zero-install importer: Bazel consumes the project's own checked-in state rather than asking Yarn to recreate it during the build.

## Respect the state the project already owns.

The importer does not run Yarn and does not construct a node\_modules directory. Its caller supplies the lockfile, PnP graph, resolver, configuration, integrity manifest, and the cache or unpacked files those records reference. That makes the adoption boundary visible: these are the project's inputs, not a second dependency installation.

The resolver and lockfile tell related but different stories. One describes what a dependency request resolves to; the other records the intended package relationships. Cross-checking those relationships is important because individually valid files can still disagree with each other.

**Validity includes the relationship.**

1. Read the lockfile and graph
2. Cross-check their bindings
3. Verify the referenced files

Checking a file in isolation is different from checking that it belongs to the dependency state being executed.

## Bind more than the bytes.

The integrity manifest includes file type and executable mode as well as content hashes. Those details affect what a build can do. An executable becoming ordinary data, or a path becoming a symlink, changes the execution contract even if a filename looks familiar.

The verifier also checks canonical paths and exact tree membership before the resolver is loaded. This is a point where refusing an unexpected shape is more useful than guessing what the author meant. The implementation's tests exercise changed archives, mutated configuration, traversal, symlinks, and unpacked-file membership.

## Keep the usable boundary specific.

This is an experimental public implementation with deliberate limits. The contribution records Yarn 4 runtime execution, Yarn 3 import and verification, and a CommonJS PnP boundary. Rules that require a linked node\_modules layout are a different integration shape, not something this importer silently emulates.

The design lesson I take from it is broader than the particular package manager. When two systems meet, decide which one owns the state. Then make the relationship between the inputs explicit enough to verify. That is often clearer than adding another conversion layer and hoping the resulting files remain equivalent.

## What I take from it

Check that the resolver files match the declared dependencies and locations before allowing the build to load them.

## Code and further reading

- [rules\_js: the zero-install importer design and tests](https://github.com/aspect-build/rules_js/pull/2957)
- [The public integrity-bound PnP implementation](https://github.com/mh0pe/rules_js/tree/codex/yarn-lock-repo-cache-v2.3.7)

[Read the illustrated essay on mh0pe.github.io](https://mh0pe.github.io/blog/integrity-bound-yarn-pnp-for-bazel/)
