Skip to content

Commit 0d4f86c

Browse files
Giancarlo Anemonearcanis
andauthored
fix(core) resolve cwd using realpath when running yarn commands in a workspace (#2659)
* fix(core) resolve cwd using realpath when running yarn commands in a workspace * Update scriptUtils.ts Co-authored-by: Maël Nison <nison.mael@gmail.com>
1 parent fb82bd7 commit 0d4f86c

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

.yarn/versions/b469be37.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
releases:
2+
"@yarnpkg/core": patch
3+
"@yarnpkg/cli": patch
4+
5+
6+
declined:
7+
- "@yarnpkg/plugin-compat"
8+
- "@yarnpkg/plugin-constraints"
9+
- "@yarnpkg/plugin-dlx"
10+
- "@yarnpkg/plugin-essentials"
11+
- "@yarnpkg/plugin-exec"
12+
- "@yarnpkg/plugin-file"
13+
- "@yarnpkg/plugin-git"
14+
- "@yarnpkg/plugin-github"
15+
- "@yarnpkg/plugin-http"
16+
- "@yarnpkg/plugin-init"
17+
- "@yarnpkg/plugin-interactive-tools"
18+
- "@yarnpkg/plugin-link"
19+
- "@yarnpkg/plugin-node-modules"
20+
- "@yarnpkg/plugin-npm"
21+
- "@yarnpkg/plugin-npm-cli"
22+
- "@yarnpkg/plugin-pack"
23+
- "@yarnpkg/plugin-patch"
24+
- "@yarnpkg/plugin-pnp"
25+
- "@yarnpkg/plugin-stage"
26+
- "@yarnpkg/plugin-typescript"
27+
- "@yarnpkg/plugin-version"
28+
- "@yarnpkg/plugin-workspace-tools"
29+
- "@yarnpkg/builder"
30+
- "@yarnpkg/doctor"
31+
- "@yarnpkg/pnpify"

packages/yarnpkg-core/sources/scriptUtils.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,29 @@ async function initializeWorkspaceEnvironment(workspace: Workspace, {binFolder,
376376
)
377377
);
378378

379-
return {manifest: workspace.manifest, binFolder, env, cwd: cwd ?? workspace.cwd};
379+
// When operating under PnP, `initializePackageEnvironment`
380+
// yields package location to the linker, which goes into
381+
// the PnP hook, which resolves paths relative to dirname,
382+
// which is realpath'd (because of Node). The realpath that
383+
// follows ensures that workspaces are realpath'd in a
384+
// similar way.
385+
//
386+
// I'm not entirely comfortable with this, especially because
387+
// there are no tests pertaining to this behaviour and the use
388+
// case is still a bit fuzzy to me (something about Flow not
389+
// handling well the case where a project was 1:1 symlinked
390+
// into another place, I think?). I also don't like the idea
391+
// of realpathing thing in general, since it means losing
392+
// information...
393+
//
394+
// It's fine for now because it preserves a behaviour in 3.x
395+
// that was already there in 2.x, but it should be considered
396+
// for removal or standardization if it ever becomes a problem.
397+
//
398+
if (typeof cwd === `undefined`)
399+
cwd = ppath.dirname(await xfs.realpathPromise(ppath.join(workspace.cwd, `package.json` as Filename)));
400+
401+
return {manifest: workspace.manifest, binFolder, env, cwd};
380402
}
381403

382404
async function initializePackageEnvironment(locator: Locator, {project, binFolder, cwd, lifecycleScript}: {project: Project, binFolder: PortablePath, cwd?: PortablePath | undefined, lifecycleScript?: string}) {

0 commit comments

Comments
 (0)