Skip to content

feat(install): add lazy dependencies - #491

Merged
arcanis merged 5 commits into
mainfrom
claude/lazy-dependencies
Nov 30, 2026
Merged

arcanis merged 5 commits into
mainfrom
claude/lazy-dependencies

Conversation

@arcanis

@arcanis arcanis commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Adds lazy dependencies: packages that are only downloaded once one of their binaries is called. The typical case is @yarnpkg/node (100+ MB): with it lazy, fresh clones whose scripts never run JavaScript don't download it.

Declaring them

{
  "dependenciesMeta": {
    "@yarnpkg/node": {"lazy": true}
  }
}
  • dependenciesMeta.<name>.lazy in the root manifest, next to built and unplugged. It accepts the same keys (globs, name@range).
  • Variants of a lazy package (the per-platform builds of @yarnpkg/node) are lazy too.

What installs do

  • Lazy packages are resolved and locked like any other package.
  • They're downloaded when they're added to the project, so the lockfile records their checksum. A later download therefore never changes yarn.lock.
  • They're also installed normally when they're already in the cache.
  • Otherwise the install skips them. It reuses the existing path for packages built for other platforms (MissingZip), which the linkers already skip.
  • yarn install --fetch-lazy downloads all of them, for example when preparing a CI image or before working offline.

Binaries in the lockfile

Resolution gets a bin field (name → path in the package), filled at resolution time:

  • npm packages: the registry metadata (bin was already kept in the metadata cache);
  • @yarnpkg/node: its known binary (node);
  • folders, tarballs, git, etc.: their manifest.

To support this, bin moved from Manifest to RemoteManifest, which Manifest flattens, so manifests serialize the same.

This changes lockfiles: entries of packages with binaries gain a bin field when they're next resolved. Entries reused from an existing lockfile are left alone. A lazy package whose resolution doesn't list binaries (an old lockfile entry, or a PyPI package, whose entry points are only known from the wheel) is downloaded normally.

Downloading on first use

  • Until the package is downloaded, its binaries are exposed as Binary::Lazy. Their PATH wrapper calls a new internal command, yarn debug run-lazy-binary <project> <locator> <name> …args.
  • That command downloads the package, checks it against the lockfile checksum when there is one, and extracts it to a fixed location. It then execs the binary in the caller's folder and environment, through node for JS binaries.
  • The location is the one the linker reserved for the package during the last install, recorded in the install state like any other package location: its unplugged folder with PnP (the PnP linker forces lazy packages to be unplugged), its hoisted folder in node_modules with node-modules, and its store folder with pnpm. Each linker keeps whatever gets extracted there instead of deleting it, so there's no second copy anywhere, and the package can be required as soon as it's downloaded.
  • No install runs during this, so a build script can call a lazy binary while an install is running it. That's tested with a postinstall that runs node.
  • The folder may already hold the packages nested inside it. The extraction goes through a temporary folder, moves the entries that aren't there yet, and writes a marker last.
  • Concurrent calls download the package once. The download holds an exclusive lock on .yarn/ignore/lazy-locks/<package>.lock (File::lock). The other processes wait for it, then find the package ready.
  • A progress bar is drawn on stderr while this happens, only when stderr is a terminal and enableProgressBars is on, since the binary's stdout may be read by another program. It shows the bytes received (the HTTP client adds them to a task-local counter that the download sets up), then the conversion and the extraction. A spinner replaces it when the package comes from a mirror, and a waiting message shows when another process is downloading the package.
  • Once extracted, the binaries point straight at the package, so the wrapper is no longer involved.
  • The up-to-date check notices lazy packages that have been downloaded since the last install, so the next install links them normally (binaries in node_modules/.bin, build scripts, ...).
  • yarn bin <name> downloads the package before printing the path. yarn bin --json prints where the binary will be without downloading it.

Tests

features/lazyDependencies.test.ts (11 tests):

  • a lazy package is downloaded when added, and not on a fresh clone until called;
  • the lockfile is unchanged after the download;
  • packages found in the cache are installed normally;
  • --fetch-lazy downloads them;
  • a build script triggers the download during an install;
  • yarn bin triggers the download;
  • lazy npm packages with PnP, node-modules and pnpm, including a binary that requires a dependency of its package, and requiring the package right after its download;
  • downloaded lazy packages are kept by the following installs;
  • three parallel calls download the package once. The test registry now counts Node.js downloads; without the lock, the count is 3 every time.

Unit tests cover the progress lines. The bar itself was checked by hand in a pseudo-terminal, with a real Node.js download and two parallel calls.

Not covered yet

  • Lazy packages can only be used through their binaries until they're downloaded. require() before that fails.
  • Build scripts of a lazy package don't run when it's downloaded on first use, only at the next install that links it.
  • Making the devEngines packages of feat(engines): support .node-version and .python-version #490 lazy by default comes once both PRs are merged.

Generated by Claude Code

Packages marked with dependenciesMeta.<name>.lazy are resolved and
locked like any other, and downloaded when they're added to the project
so the lockfile records their checksum. Installs that don't find them in
the cache skip them; their binaries, now listed in the lockfile
resolutions, download them on their first call and extract them to the
location the linker reserved for them, without running an install (so
build scripts can call them while an install runs). yarn install
--fetch-lazy downloads them all.
@arcanis
arcanis force-pushed the claude/lazy-dependencies branch from 9fe276b to bb9d227 Compare November 30, 2026 16:47
@arcanis
arcanis changed the base branch from claude/happy-hawking-hzclty to main November 30, 2026 16:47
The node-modules and pnpm linkers now record and keep the location of
the lazy packages they skip, like the PnP linker does with their
unplugged folder, and downloading a lazy package extracts it there
(merging with the packages nested inside it). This removes the separate
.yarn/ignore/lazy copy, which nothing cleaned up, and makes the package
requirable as soon as it's downloaded.
@netlify

netlify Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for yarn-v6 ready!

Name Link
🔨 Latest commit 878117e
🔍 Latest deploy log https://app.netlify.com/projects/yarn-v6/deploys/6b0dd9d78103bb60f5608cb2
😎 Deploy Preview https://deploy-preview-491--yarn-v6.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

…kages

Downloading a lazy package now takes an exclusive lock on a file of
.yarn/ignore/lazy-locks, so that concurrent calls to its binaries only
download it once (the others wait, then find it ready). While it runs, a
progress bar is drawn on the standard error when it's a terminal: the
bytes received (reported by the HTTP client to the task that asked for
them), then the conversion and the extraction, or a note that another
process is downloading the package.
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

⏱️ Benchmark Results

gatsby install-full-cold

Metric Base Head Difference
Mean 5.052s 5.031s -0.41% ✅
Median 5.058s 5.031s -0.55% ✅
Min 4.891s 4.869s
Max 5.314s 5.356s
Std Dev 0.094s 0.102s
📊 Raw benchmark data (gatsby install-full-cold)

Base times: 5.070s, 5.111s, 5.104s, 4.973s, 4.898s, 5.113s, 5.165s, 5.076s, 4.994s, 4.895s, 5.083s, 4.921s, 5.069s, 4.969s, 5.024s, 5.037s, 4.891s, 5.314s, 5.079s, 5.061s, 5.045s, 5.020s, 4.952s, 5.080s, 5.047s, 5.088s, 5.056s, 5.127s, 5.045s, 5.239s

Head times: 5.039s, 4.942s, 5.041s, 5.023s, 5.073s, 5.356s, 5.093s, 4.945s, 5.065s, 5.036s, 4.936s, 5.003s, 4.993s, 4.924s, 5.110s, 5.103s, 5.034s, 5.050s, 4.957s, 5.017s, 5.030s, 4.869s, 4.941s, 4.955s, 5.107s, 5.032s, 4.977s, 4.926s, 5.307s, 5.045s


gatsby install-cache-only

Metric Base Head Difference
Mean 1.803s 1.793s -0.58% ✅
Median 1.804s 1.784s -1.08% ✅
Min 1.757s 1.758s
Max 1.927s 1.982s
Std Dev 0.029s 0.039s
📊 Raw benchmark data (gatsby install-cache-only)

Base times: 1.787s, 1.789s, 1.775s, 1.803s, 1.810s, 1.808s, 1.808s, 1.800s, 1.809s, 1.803s, 1.802s, 1.840s, 1.811s, 1.805s, 1.815s, 1.776s, 1.808s, 1.812s, 1.766s, 1.795s, 1.927s, 1.803s, 1.781s, 1.804s, 1.777s, 1.757s, 1.789s, 1.810s, 1.811s, 1.813s

Head times: 1.796s, 1.791s, 1.793s, 1.773s, 1.768s, 1.780s, 1.793s, 1.804s, 1.790s, 1.763s, 1.782s, 1.775s, 1.982s, 1.817s, 1.784s, 1.826s, 1.795s, 1.781s, 1.785s, 1.806s, 1.794s, 1.775s, 1.768s, 1.761s, 1.758s, 1.779s, 1.769s, 1.798s, 1.783s, 1.812s


gatsby install-cache-and-lock (warm, with lockfile)

Metric Base Head Difference
Mean 0.455s 0.474s +4.15% ⚠️
Median 0.454s 0.464s +2.18% ⚠️
Min 0.443s 0.440s
Max 0.477s 0.566s
Std Dev 0.009s 0.032s
📊 Raw benchmark data (gatsby install-cache-and-lock (warm, with lockfile))

Base times: 0.447s, 0.463s, 0.445s, 0.446s, 0.447s, 0.457s, 0.449s, 0.447s, 0.443s, 0.447s, 0.446s, 0.454s, 0.445s, 0.456s, 0.454s, 0.450s, 0.455s, 0.452s, 0.460s, 0.476s, 0.465s, 0.460s, 0.454s, 0.462s, 0.468s, 0.461s, 0.477s, 0.456s, 0.457s, 0.449s

Head times: 0.522s, 0.511s, 0.467s, 0.467s, 0.455s, 0.468s, 0.543s, 0.566s, 0.463s, 0.467s, 0.511s, 0.497s, 0.468s, 0.510s, 0.465s, 0.460s, 0.440s, 0.509s, 0.463s, 0.453s, 0.448s, 0.444s, 0.448s, 0.452s, 0.473s, 0.450s, 0.450s, 0.449s, 0.449s, 0.443s

The lockfile now lists the binaries of the packages, so a build without
that change rewrites it, and CI (which installs the repository with the
build of the pull request, immutably and in hardened mode) refuses the
lockfile written by the previous one. packageManager now points to the
build of this branch, and the lockfile is regenerated with it.

AGENTS.md documents how to handle such changes.
yarn.lock is main's, regenerated with this branch's build
(--refresh-lockfile) as described in AGENTS.md.
@arcanis
arcanis merged commit ac47087 into main Nov 30, 2026
25 checks passed
@arcanis
arcanis deleted the claude/lazy-dependencies branch November 30, 2026 21:08
arcanis pushed a commit that referenced this pull request Nov 30, 2026
The binaries of the engines handle the lazy packages of #491.
arcanis added a commit that referenced this pull request Dec 1, 2026
Adds support for declaring the project's Node.js and Python through
`.node-version` and `.python-version` files at its root. These are the
same files nvm, fnm, pyenv and uv read.

```bash
echo 24 > .node-version
echo 3.13 > .python-version
```

This started as `devEngines.node` / `devEngines.python` support. Per the
spec, `devEngines` only has a single `runtime` field, which doesn't fit
projects with both Node.js and Python, so the engines now come from the
version files and `devEngines` is no longer read.

## What the engines do

- **Root dependency:** each version file adds the matching builtin
package (`@yarnpkg/node@builtin:<version>`,
`@yarnpkg/python@builtin:<version>`) to the dependencies of the root
workspace. It's treated like a dev dependency, so production installs
skip it. A root workspace that declares the package itself (or gets it
from a profile) keeps its own declaration.
- **PATH injection:** the engine binaries (`node`; `python` and
`python3`) are visible from every package: root scripts, scripts of
other workspaces, and dependency build scripts. Those packages don't get
them as dependencies. A package whose own dependencies include
`@yarnpkg/node` uses its own version.
- **Lazy by default:** a fresh clone only downloads an engine the first
time one of its binaries is called (#491).
`dependenciesMeta["@yarnpkg/node"].lazy: false` turns this off. The
Python engine stays eager when venv islands use it as their interpreter.
- **Platform-specific lazy packages** (like the Node.js builds) are no
longer downloaded when added to the project. The lockfile doesn't record
their checksums, so there was nothing to download them for. This also
spares `yarn init` (and the playground) a Node.js download.
- **Islands:** the Python engine is the Python version of islands that
don't set their own `pythonVersion`. Their PyPI packages are resolved
for it and their venvs are created from it. Islands can also have
version files of their own (see below).

## The version files

- Yarn reads the first non-comment line as a semver range: `24` means
the latest 24.x, `24.11.1` that exact release. The `v24` and
`cpython-3.13` prefixes are accepted.
- Content Yarn can't install (`lts/*`, `system`, `pypy3.10`, …) is
ignored with a warning, since other tools may understand it.
- The engines are stored in the lockfile's `project` section
(`"engines": {"node": "24"}`), because they change the root workspace's
dependencies without its manifest saying so. #488's consistency check
requires that, and `--since` compares them between revisions like the
other project rules.
- They're also part of the install configuration hash, so editing a
version file triggers a reinstall on the next command.

## Island version files

An island can have its own `.node-version` / `.python-version`, in the
folder of one of its workspaces. For that island they override the files
at the project root; the other workspaces keep the project's engines.

- The island's engines become dev dependencies of its workspaces, so the
island solver resolves them and the lockfile records them under
`islands`. Their binaries take precedence over the project engines in
the island's scripts. They're downloaded with the rest of the island,
not lazily.
- An island has a single version of each engine. If two of its
workspaces have different files, the install fails with an error.
- Python version precedence for an island is: its `pythonVersion`
setting, then its own `.python-version`, then the project's
`.python-version`, then the global `pythonVersion`. The island's own
`.python-version` is resolved before the island so its PyPI packages are
solved for that version. Its locked build comes from the island's part
of the lockfile.
- Venv islands no longer put builtin packages into `site-packages`.
They're extracted once to `.yarn/ignore/island-builtins/<slug>` and
shared by the island's venvs. An island with its own `.python-version`
creates its venvs from that build.

## `@yarnpkg/python`

This is a new builtin package providing the python-build-standalone
CPython builds, with one variant per platform. Builds are downloaded
from `pythonDownloadMirror` and checked against the release's
`SHA256SUMS`.

- The same Python version is rebuilt in several python-build-standalone
releases. To keep the lockfile deterministic, the release tag is stored
as the prerelease part of the locator
(`@yarnpkg/python@builtin:3.13.18-20261124`), while the package version
stays `3.13.18`.
- Ranges resolve against the latest release. To pin a build from an
older release, use a version like `3.12.1-20240101`.

## `yarn init`

- At the project root, `yarn init` writes `.node-version` with the
latest Node.js major, unless the file exists.
- `--node-version=<version>` writes something else; `latest` is the
default. `--no-node-version` writes no file. This relies on
arcanis/clipanion-rs#12 (merged).

## Playground

- Projects now start from the full manifest `yarn init` generates, with
the tutorial's fields on top, and get the `.node-version` it writes.
- Tutorial 15 now teaches the version file: Node.js downloaded on first
use, who depends on it, and pinning another release for one workspace.
The remarks in tutorials 3 and 6 about the sandbox having no Node.js
were updated.

## Tests

- `features/engines.test.ts` (29 tests, 7 of them for island version
files), `commands/init.test.ts`, and updated `lazyDependencies` tests.
- Unit tests cover the version-file parsing.
- The website's tutorial loader accepts all 20 tutorials.


---
_Generated by [Claude Code](https://claude.ai/code)_

---------

Co-authored-by: Claude <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
test-reports — 878117e6 Deployed Nov 30, 2026 by arcanis via Reporting test results #1812
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.

2 participants