Khải bfa861fe04 refactor(pacquet/config): replace env mutations with DI pattern (#11718)
* refactor(pacquet/config): thread default_store_dir through the EnvVar DI seam

Replaces process-environment mutation in the `default_store_dir` tests with
the dependency-injection pattern established in pnpm/pacquet#339 and
consolidated for the in-tree pacquet subtree in #11708. Tracks
pnpm/pacquet#343 — the original issue still applied after pacquet was
merged into this repo because the env-mutating tests came with it.

The four affected unit tests (`test_default_store_dir_with_pnpm_home_env`
and `test_default_store_dir_with_xdg_env` in `defaults::tests`, plus
`should_use_pnpm_home_env_var` and `should_use_xdg_data_home_env_var` in
`lib::tests`) now drive each branch with per-test unit structs that
satisfy `EnvVar`. No `EnvGuard` snapshot, no `unsafe` block, no
process-environment write. The `home_dir` and `current_dir` closures call
`unreachable!` for branches the early `PNPM_HOME` / `XDG_DATA_HOME`
returns short-circuit before consulting them, documenting the
precondition the way the style guide's worked example does.

`default_store_dir` is now generic over `Sys: EnvVar` and takes the
`home_dir` and `current_dir` lookups as `FnOnce` closures, mirroring the
shape of `Config::current`. A thin args-less wrapper
`default_store_dir_host` wires the production `Host` provider together
with `home::home_dir` and `env::current_dir` so the SmartDefault
expression on `Config::store_dir` stays short. The `have_default_values`
wiring assertion now compares against
`default_store_dir::<Host>(home::home_dir, env::current_dir)` instead of
the old args-less helper.

`npmrc_auth::tests::ignores_non_auth_keys` no longer needs to hold the
EnvGuard global lock against the two `Config::new()` snapshots it
compares — nothing in the crate mutates `PNPM_HOME` or `XDG_DATA_HOME`
anymore, so the env-derived `store_dir` is observed identically by both
calls even under nextest's in-process parallelism.

`EnvGuard` itself stays in `pacquet-testing-utils` because the proxy
cascade and `NPM_CONFIG_WORKSPACE_DIR` tests in `lib::tests`, as well as
out-of-crate users in `git-fetcher` and `executor`, still rely on it.
Retiring it entirely is out of scope for this issue.

---
Written by an agent (Claude Code, claude-opus-4-7).

* fix(pacquet/config): add Debug bound on default_store_dir's Error parameter

The Windows branch of `default_store_dir` calls
`current_dir().expect("current directory is unavailable")`, which
requires `Error: Debug` on the `Result<PathBuf, Error>` returned by the
`CurrentDir` closure. The original `where` clause didn't declare that
bound, so the function compiled on Linux/macOS (where the Windows
branch is `#[cfg(windows)]`-gated out) but failed on
`Lint and Test (windows-latest)` once the cfg fired.

All current callers pass `env::current_dir` (Error = `io::Error`) or
pin `Error = std::io::Error` via turbofish on test fakes — both
already satisfy `Debug` — so this is a pure type-bound fix with no
behaviour or call-site change.

---
Written by an agent (Claude Code, claude-opus-4-7).

* docs(pacquet): add shared-process-state DI exception to the style guide

The "Dependency injection for tests" gating list in
`pacquet/CODE_STYLE_GUIDE.md` enumerated four reasons to reach past
real fixtures for the DI seam: filesystem error kinds, deterministic
time, external-service happy paths, and unreachable-by-design
preconditions. It did not cover the case this PR retired from
`default_store_dir`: tests that mutate a single per-process slot
(env vars, cwd, umask, signal handlers, …) and so race with every
other test in the same process.

The pre-existing `EnvGuard` workaround restored correctness only by
holding a binary-wide mutex around `unsafe { env::set_var(...) }`,
which forecloses parallelism inside the affected tests and leaves
`unsafe` in the test source. A capability-trait fake keeps the read
deterministic and the mutation contained to the test that needs it.

Adds that bullet to the style guide between "Deterministic time" and
"External-service happy paths," and updates rule 7 in
`pacquet/AGENTS.md` (and its `CLAUDE.md` / `GEMINI.md` symlink
targets) so the summary mirrors the longer list.

---
Written by an agent (Claude Code, claude-opus-4-7).

* docs(pacquet): drop unfounded set_current_dir example from DI guidance

The previous commit cited `set_current_dir` and "the umask" as
illustrative examples in both the style guide and the AGENTS rule 7
summary. Neither is actually used anywhere in the pacquet codebase
today — only `env::set_var` is. Speculative examples in a style
guide invite contributors to chase patterns that don't exist, so
narrow the wording to the one slot we have a concrete example for
while keeping the principle generalisable (the bullet still says
"any future analogue").

---
Written by an agent (Claude Code, claude-opus-4-7).

* docs(pacquet): revert example narrowing on the shared-process-state bullet

Reviewer feedback: keep the broader `set_current_dir` / umask /
signal-handler / global-allocator examples in the DI gating bullet
and rule 7 summary. They're not used in the codebase today, but the
analogues are useful as forward-looking illustrations so the next
contributor recognises the same shape when it comes up.

Restores the wording introduced in 96040d7; reverses the narrowing in
d930bed.

---
Written by an agent (Claude Code, claude-opus-4-7).

* docs(pacquet): document when library code may read process state

Adds a "Reading process state" section to CODE_STYLE_GUIDE.md, placed
right before "Dependency injection for tests" so the reading order
captures the two questions in dependency order: first ask whether you
should be reaching for `env::var` / `env::current_dir` at all, then
ask how to inject it if the answer is yes.

The section pins:

1. Library code should rarely call `std::env::var`, `std::env::var_os`,
   or `std::env::current_dir` directly. The default fix is a parameter
   (`&Path`, `&str`, an `Option<String>`, or a `FnOnce` closure) so the
   caller decides which value to read; `Config::current` and
   `workspace::find_workspace_dir_from_env_with` already follow this.
2. When the read genuinely needs to happen, route through the DI seam
   (`Sys: EnvVar`, `Sys::var(name)`).
3. The narrow legitimate direct-call case is computing a `Config`
   default that has no caller — `default_store_dir`,
   `default_modules_dir`, `default_virtual_store_dir`. This is the
   "begrudging" use that pnpm/pacquet#343 + pnpm/pnpm#11718 finally
   threaded through the DI seam.
4. Other accepted boundary reads are program-entry knobs
   (`RAYON_NUM_THREADS`, `TRACE`) and the lifecycle-script env snapshot
   in `crates/executor` / `crates/git-fetcher` that forwards the parent
   env to spawned children verbatim.

The section belongs in CODE_STYLE_GUIDE.md (code-level convention),
not CONTRIBUTING.md (PR workflow) or AGENTS.md (agent-specific
operating rules); rule 7 in AGENTS.md already cross-links the style
guide, so no change there.

---
Written by an agent (Claude Code, claude-opus-4-7).

* refactor(pacquet/config): inline default_store_dir's SmartDefault wiring

Reviewer pointed out that the args-less `default_store_dir_host`
wrapper could be inlined into the `#[default(_code = ...)]`
expression on `Config::store_dir` — the SmartDefault macro accepts
the turbofish form `default_store_dir::<Host, _, _, _>(home::home_dir,
env::current_dir)` verbatim, and once the wrapper is gone there's
nothing for the indirection to earn.

Dropping the wrapper also removes a small motivation for the
`Host` import inside `defaults.rs` — adjust the doc comment on
`default_store_dir` to cite `crate::Host` by full path instead.

The `#[cfg(windows)] Error: std::fmt::Debug` bound that the same
reviewer asked about is not feasible: attributes on where-clause
predicates are still unstable (rust-lang/rust#115590), so the bound
stays unconditional with a comment explaining why.

---
Written by an agent (Claude Code, claude-opus-4-7).

* docs(pacquet/config): purge stale default_store_dir_host references

Commit 49c690c inlined the SmartDefault expression for
`Config::store_dir` and dropped the `default_store_dir_host`
wrapper, but three comments still pointed at the wrapper or at the
old single-type-parameter signature of `default_store_dir`.
CodeRabbit and Copilot flagged all three on review:

- `crates/config/src/lib.rs`, `have_default_values`: the comment said
  the SmartDefault resolved "via the thin `default_store_dir_host`
  wrapper". Now reflects the direct
  `default_store_dir::<Host, _, _, _>(home::home_dir,
  env::current_dir)` call.
- `crates/config/src/npmrc_auth/tests.rs`, `ignores_non_auth_keys`:
  the comment cited `default_store_dir::<Host>` with no closure
  parameters. Now spells out the four-parameter turbofish that
  matches the current signature.
- `CODE_STYLE_GUIDE.md`, "Reading process state": the paragraph
  recommended an args-less `default_store_dir_host` wrapper as the
  pattern to follow. Now recommends inlining the production
  composition at the `SmartDefault` site directly.

Copilot also queried whether `pnpm/pnpm#11718` was the right number
in the DI section's `EnvGuard` retirement citation — confirmed it
is. #11708 introduced the DI seam consolidation; this PR is the
one that retires the env-mutation pattern from `default_store_dir`.

---
Written by an agent (Claude Code, claude-opus-4-7).

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-05-18 17:23:58 +02:00
2026-04-10 18:30:33 +02:00
2026-04-10 18:30:33 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-04-30 23:03:46 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-04-30 23:19:31 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-04-30 23:03:46 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 15:42:32 +02:00
2026-05-18 10:21:42 +02:00
2026-01-16 16:31:31 +01:00
2024-03-21 01:09:22 +01:00
2022-06-01 02:48:58 +03:00

简体中文 | 日本語 | 한국어 | Italiano | Português Brasileiro

pnpm

Fast, disk space efficient package manager:

  • Fast. Up to 2x faster than the alternatives (see benchmark).
  • Efficient. Files inside node_modules are linked from a single content-addressable storage.
  • Great for monorepos.
  • Strict. A package can access only dependencies that are specified in its package.json.
  • Deterministic. Has a lockfile called pnpm-lock.yaml.
  • Works as a Node.js version manager. See pnpm runtime.
  • Works everywhere. Supports Windows, Linux, and macOS.
  • Battle-tested. Used in production by teams of all sizes since 2016.
  • See the full feature comparison with npm and Yarn.

To quote the Rush team:

Microsoft uses pnpm in Rush repos with hundreds of projects and hundreds of PRs per day, and weve found it to be very fast and reliable.

npm version OpenCollective OpenCollective X Follow Stand With Ukraine

Platinum Sponsors

Bit

Gold Sponsors

Sanity Discord Vite
SerpApi CodeRabbit Stackblitz
Workleap Nx

Silver Sponsors

Replit Cybozu devowl.io
u|screen Leniolabs_ Depot
Cerbos ⏱️ Time.now

Support this project by becoming a sponsor.

Background

pnpm uses a content-addressable filesystem to store all files from all module directories on a disk. When using npm, if you have 100 projects using lodash, you will have 100 copies of lodash on disk. With pnpm, lodash will be stored in a content-addressable storage, so:

  1. If you depend on different versions of lodash, only the files that differ are added to the store. If lodash has 100 files, and a new version has a change only in one of those files, pnpm update will only add 1 new file to the storage.
  2. All the files are saved in a single place on the disk. When packages are installed, their files are linked from that single place consuming no additional disk space. Linking is performed using either hard-links or reflinks (copy-on-write).

As a result, you save gigabytes of space on your disk and you have a lot faster installations! If you'd like more details about the unique node_modules structure that pnpm creates and why it works fine with the Node.js ecosystem, read this small article: Flat node_modules is not the only way.

💖 Like this project? Let people know with a tweet

Getting Started

Benchmark

pnpm is up to 2x faster than npm and Yarn classic. See all benchmarks here.

Benchmarks on an app with lots of dependencies:

License

MIT

Description
No description provided
Readme MIT 296 MiB
Languages
Rust 61.2%
TypeScript 38.3%
JavaScript 0.4%