Skip to content

fix: honor --prefix when resolving workspace dir#11549

Merged
zkochan merged 2 commits into
mainfrom
fix/11535
May 8, 2026
Merged

fix: honor --prefix when resolving workspace dir#11549
zkochan merged 2 commits into
mainfrom
fix/11535

Conversation

@zkochan

@zkochan zkochan commented May 8, 2026

Copy link
Copy Markdown
Member

Summary

Fixes #11535. Running pnpm --prefix=<dir> install from a parent directory was overwriting <dir>/pnpm-workspace.yaml, replacing user-set allowBuilds values (e.g. true) with set this to true or false placeholders.

The CLI's --prefix → dir rename was applied after getWorkspaceDir(options) had already run, so workspace detection fell back to process.cwd() and missed the manifest at the prefix dir. With no workspace manifest loaded, config.allowBuilds ended up as {}, and the ignored-builds auto-population step (handleIgnoredBuilds) wrote placeholders over the user's existing settings.

The fix moves the rename loop ahead of getWorkspaceDir on the main parse path, and threads renamedOptions into getWorkspaceDir so the --help/--version short-circuit paths also resolve --prefix correctly.

Test plan

  • Added regression test in cli/parse-cli-args/test/index.ts — verified it fails on main and passes with the fix
  • All 46 existing tests in @pnpm/cli.parse-cli-args pass
  • Lint passes

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

Summary by CodeRabbit

  • Bug Fixes

    • Fixed pnpm --prefix=<dir> install overwriting a workspace manifest with placeholder values; --prefix now correctly affects workspace root detection to prevent unintended file changes.
  • Tests

    • Added regression tests covering workspace detection with --prefix (and interactions with --dir), including short-circuit paths like --help/--version.

The `--prefix` option (renamed to `dir`) was applied to options after
`getWorkspaceDir` had already run, so workspace detection fell back to
`process.cwd()` and missed the manifest at the prefix dir. As a result,
`pnpm --prefix=<dir> install` from a parent directory would overwrite
`<dir>/pnpm-workspace.yaml` `allowBuilds` entries with placeholders
because the existing settings were never loaded into config.

Closes #11535
Copilot AI review requested due to automatic review settings May 8, 2026 14:51
@coderabbitai

coderabbitai Bot commented May 8, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

This PR fixes a bug where pnpm --prefix=<dir> install overwrites pnpm-workspace.yaml with placeholder values by applying renamed option mappings (e.g., --prefix → dir) before workspace root detection and updating the helper to use those mappings across parse paths.

Changes

Prefix Workspace Resolution

Layer / File(s) Summary
Problem Statement
.changeset/prefix-workspace-resolution.md
Changeset patch documents fix for pnpm --prefix=<dir> install incorrectly overwriting pnpm-workspace.yaml due to workspace root detection ignoring the renamed --prefix option.
Core Logic - Workspace Directory Resolution
cli/parse-cli-args/src/index.ts
Renamed-options remapping is applied to parsed options before calling getWorkspaceDir(); getWorkspaceDir now accepts an optional renamedOptions parameter and derives dir from either parsedOpts.dir or a source option mapped to dir.
Early Return Paths
cli/parse-cli-args/src/index.ts
--help and --version short-circuit returns are updated to pass opts.renamedOptions into getWorkspaceDir() so exploratory parsing resolves the workspace consistently.
Regression Test
cli/parse-cli-args/test/index.ts
Adds fs/path imports, a temp parent/child workspace harness, and tests ensuring workspaceDir resolves to the prefix target for install, --help, --version, and that --dir wins when both --prefix and --dir are supplied.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

  • pnpm/pnpm#11489: Both PRs modify cli/parse-cli-args/src/index.ts to ensure renamedOptions (e.g., --prefix → dir) are applied during short-circuit/exploratory parsing so workspace detection and universal options are honored consistently.

Suggested labels

area: cli/dlx

Poem

🐰 Hopping through args, I fix the trace,
Prefix now points to the proper place.
No more placeholders, no more fright,
Workspace found — everything's right.
Tiny paws, big fix, happy night!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: honor --prefix when resolving workspace dir' directly and clearly describes the main bug fix: ensuring the --prefix flag is properly honored during workspace directory resolution.
Linked Issues check ✅ Passed All code changes directly address issue #11535: the fix ensures parseCliArgs honors --prefix during workspace detection by applying renamedOptions before getWorkspaceDir, preventing pnpm-workspace.yaml from being overwritten.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the --prefix workspace resolution issue: the changeset documents the fix, parseCliArgs applies renamedOptions before workspace detection, and tests verify the regression is resolved.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/11535

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes workspace root detection when --prefix is used (and renamed to dir) so pnpm --prefix=<dir> install resolves the workspace from the prefixed directory instead of process.cwd(), preventing unintended overwrites of pnpm-workspace.yaml settings (e.g. allowBuilds) as reported in #11535.

Changes:

  • Apply renamedOptions (notably prefix → dir) before workspace detection in the main parse path.
  • Teach --help / --version short-circuit paths to resolve workspaceDir while honoring renamedOptions.
  • Add a regression test and a changeset documenting the fix.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
cli/parse-cli-args/src/index.ts Moves option renaming ahead of workspace detection and updates getWorkspaceDir to account for renamed options on short-circuit paths.
cli/parse-cli-args/test/index.ts Adds a regression test ensuring workspaceDir resolves from --prefix (via prefix → dir rename).
.changeset/prefix-workspace-resolution.md Adds a patch changeset for pnpm and @pnpm/cli.parse-cli-args describing the fix for #11535.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cli/parse-cli-args/src/index.ts Outdated
Comment thread cli/parse-cli-args/src/index.ts
… help/version regression tests

Address review feedback on #11549:
- Skip the rename when the target key is already set (e.g. `--prefix=foo
  --dir=bar` keeps `dir=bar`), so the alias never silently clobbers an
  explicitly-passed canonical option. The alias is always dropped.
- Add regression tests asserting `workspaceDir` resolves from `--prefix`
  on the `--help` and `--version` short-circuit paths.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
cli/parse-cli-args/test/index.ts (1)

408-415: ⚡ Quick win

Move the helper declaration below its call sites to match project style.

setupParentWithChildWorkspace is declared before usage; this file’s guideline expects hoisted function declarations after use. Please move this helper to the end of the file (no behavior change needed).

As per coding guidelines, "Declare functions after they are used, relying on hoisting".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cli/parse-cli-args/test/index.ts` around lines 408 - 415, The helper function
setupParentWithChildWorkspace is declared before its call sites; move its entire
declaration block so it appears after the tests that call it (i.e., place
setupParentWithChildWorkspace at the end of this test file) to follow the
project's "declare after use" style, ensuring you do not change the function
body or references (keep fs.mkdirSync, fs.writeFileSync, path.join and
process.chdir usage intact).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@cli/parse-cli-args/test/index.ts`:
- Around line 408-415: The helper function setupParentWithChildWorkspace is
declared before its call sites; move its entire declaration block so it appears
after the tests that call it (i.e., place setupParentWithChildWorkspace at the
end of this test file) to follow the project's "declare after use" style,
ensuring you do not change the function body or references (keep fs.mkdirSync,
fs.writeFileSync, path.join and process.chdir usage intact).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 1d2ca5bc-9cda-4caf-a09e-19d681395b3d

📥 Commits

Reviewing files that changed from the base of the PR and between 3a43db6 and d1742b0.

📒 Files selected for processing (2)
  • cli/parse-cli-args/src/index.ts
  • cli/parse-cli-args/test/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • cli/parse-cli-args/src/index.ts
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Compile & Lint
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{js,ts,tsx,jsx}

📄 CodeRabbit inference engine (AGENTS.md)

**/*.{js,ts,tsx,jsx}: Use trailing commas in code following Standard Style with modifications
Prefer functions over classes
Declare functions after they are used, relying on hoisting
Functions should have no more than two or three arguments; use a single options object for functions needing more parameters
Organize imports in order: standard libraries, external dependencies (sorted alphabetically), then relative imports

Files:

  • cli/parse-cli-args/test/index.ts
🔇 Additional comments (1)
cli/parse-cli-args/test/index.ts (1)

417-456: Strong regression coverage for the --prefix/--dir workspace resolution fix.

Nice job covering the normal path, --help/--version short-circuits, and canonical-option precedence in one focused block.

@zkochan zkochan merged commit 164221c into main May 8, 2026
13 checks passed
@zkochan zkochan deleted the fix/11535 branch May 8, 2026 18:12
zkochan added a commit that referenced this pull request May 12, 2026
The `--prefix` option (renamed to `dir`) was applied to options after
`getWorkspaceDir` had already run, so workspace detection fell back to
`process.cwd()` and missed the manifest at the prefix dir. As a result,
running pnpm from outside the project (e.g. `pnpm --prefix=child install`
from the parent dir) silently skipped settings declared in
`child/pnpm-workspace.yaml`.

Backport of #11549 to release/10. Closes #11535.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pnpm install overwrites pnpm-workspace.yaml when --prefix is present

2 participants