Skip to content

fix: preserve root path in tryWorkspaceByCwd when cwd is /#7203

Open
quinise wants to merge 4 commits into
yarnpkg:masterfrom
quinise:fix/tryWorkspaceByCwd-root-path
Open

fix: preserve root path in tryWorkspaceByCwd when cwd is /#7203
quinise wants to merge 4 commits into
yarnpkg:masterfrom
quinise:fix/tryWorkspaceByCwd-root-path

Conversation

@quinise

@quinise quinise commented Jun 26, 2026

Copy link
Copy Markdown

What's the problem this PR addresses?

In Project.tryWorkspaceByCwd, the workspaceCwd is normalized and trailing slashes are stripped. However, when cwd is the filesystem root (/), stripping the trailing slash produces an empty string "" instead of "/", causing workspace lookup to fail.

Closes issue #5380
[Bug?]: The nearest package directory (/) doesn't seem to be part of the project declared in /. (docker)

...

How did you fix it?

After stripping trailing slashes, fall back to PortablePath.root if the result is an empty string:

// Before
workspaceCwd = ppath.normalize(workspaceCwd).replace(/\/+$/, ``) as PortablePath;

// After
workspaceCwd = (ppath.normalize(workspaceCwd).replace(/\/+$/, ``) || PortablePath.root) as PortablePath;

corrected RegEx Search passing test

...

Checklist

  • I have set the packages that need to be released for my changes to be effective.
  • [] I will check that all automated PR checks pass before the PR gets reviewed.

Copilot AI review requested due to automatic review settings June 26, 2026 19:42

Copilot AI 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.

Pull request overview

Fixes a workspace lookup edge case in Project.tryWorkspaceByCwd where normalizing / and stripping trailing slashes could produce an empty string, causing workspace resolution to fail when the current working directory is the filesystem root (notably in Docker scenarios).

Changes:

  • Preserve the filesystem root by falling back to PortablePath.root when trailing-slash stripping would yield ''.
  • Add a regression test ensuring tryWorkspaceByCwd('/') can resolve a workspace registered at /.
  • Add a versions file to release the fix as patch updates across relevant Yarn packages/plugins.

Reviewed changes

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

File Description
packages/yarnpkg-core/sources/Project.ts Ensures normalized root path (/) isn’t converted into an empty string during workspace lookup.
packages/yarnpkg-core/tests/Project.test.ts Adds a regression test covering workspace lookup when cwd is /.
.yarn/versions/da7e1fc4.yml Declares patch releases for packages affected by the core fix.

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

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