Skip to content

Commit 635ed55

Browse files
authored
fix(core): use the current CLI to prepare external Yarn classic projects (#4682)
* fix(core): use the current CLI to prepare external Yarn classic projects * fix(core): revert changes made to the Manifest by `set version` * fix: add `--yarn-path`
1 parent 5505ce5 commit 635ed55

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

.yarn/versions/8fe58483.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/core": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-exec"
11+
- "@yarnpkg/plugin-file"
12+
- "@yarnpkg/plugin-git"
13+
- "@yarnpkg/plugin-github"
14+
- "@yarnpkg/plugin-http"
15+
- "@yarnpkg/plugin-init"
16+
- "@yarnpkg/plugin-interactive-tools"
17+
- "@yarnpkg/plugin-link"
18+
- "@yarnpkg/plugin-nm"
19+
- "@yarnpkg/plugin-npm"
20+
- "@yarnpkg/plugin-npm-cli"
21+
- "@yarnpkg/plugin-pack"
22+
- "@yarnpkg/plugin-patch"
23+
- "@yarnpkg/plugin-pnp"
24+
- "@yarnpkg/plugin-pnpm"
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/extensions"
32+
- "@yarnpkg/nm"
33+
- "@yarnpkg/pnpify"
34+
- "@yarnpkg/sdks"

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ The following changes only affect people writing Yarn plugins:
5757
### Bugfixes
5858

5959
- `yarn dlx` will no longer report false-positive `UNUSED_PACKAGE_EXTENSION` warnings
60+
- When Corepack is enabled Yarn will now use the current CLI to prepare external Yarn classic projects, matching the behaviour of when Corepack is disabled.
6061

6162
## 3.2.2
6263

packages/acceptance-tests/pkg-tests-specs/sources/protocols/git.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const {
44
} = require(`pkg-tests-core`);
55
const {parseSyml} = require(`@yarnpkg/parsers`);
66
const {execUtils, semverUtils} = require(`@yarnpkg/core`);
7+
const {npath} = require(`@yarnpkg/fslib`);
78

89
const TESTED_URLS = {
910
// We've picked util-deprecate because it doesn't have any dependency, and
@@ -221,5 +222,45 @@ describe(`Protocols`, () => {
221222
},
222223
),
223224
);
225+
226+
test(
227+
`it should not use Corepack to fetch Yarn Classic`,
228+
makeTemporaryEnv(
229+
{
230+
dependencies: {
231+
[`yarn-1-project`]: startPackageServer().then(url => `${url}/repositories/yarn-1-project.git`),
232+
},
233+
},
234+
async ({path, run, source}) => {
235+
// This checks that the `set version classic` part of `scriptUtils.prepareExternalProject` doesn't use Corepack.
236+
// The rest of the install will fail though.
237+
await expect(run(`install`, {
238+
env: {
239+
COREPACK_ROOT: npath.join(npath.fromPortablePath(path), `404`),
240+
YARN_ENABLE_INLINE_BUILDS: `true`,
241+
},
242+
})).rejects.toMatchObject({
243+
code: 1,
244+
stdout: expect.stringContaining(`Saving the new release`),
245+
});
246+
},
247+
),
248+
);
249+
250+
test(
251+
`it should not add a 'packageManager' field to a Yarn classic project`,
252+
makeTemporaryEnv(
253+
{
254+
dependencies: {
255+
[`yarn-1-project`]: startPackageServer().then(url => `${url}/repositories/yarn-1-project.git`),
256+
},
257+
},
258+
async ({path, run, source}) => {
259+
await expect(run(`install`)).resolves.toBeTruthy();
260+
261+
await expect(source(`require('yarn-1-project/package.json').packageManager`)).resolves.toBeUndefined();
262+
},
263+
),
264+
);
224265
});
225266
});

packages/yarnpkg-core/sources/scriptUtils.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,20 @@ export async function prepareExternalProject(cwd: PortablePath, outputPath: Port
250250
? [`workspace`, workspace]
251251
: [];
252252

253+
// `set version` will update the Manifest to contain a `packageManager` field with the latest
254+
// Yarn version which causes the results to change depending on when this command was run,
255+
// therefore we revert any change made to it.
256+
const manifestPath = ppath.join(cwd, Filename.manifest);
257+
const manifestBuffer = await xfs.readFilePromise(manifestPath);
258+
253259
// Makes sure that we'll be using Yarn 1.x
254-
const version = await execUtils.pipevp(`yarn`, [`set`, `version`, `classic`, `--only-if-needed`], {cwd, env, stdin, stdout, stderr, end: execUtils.EndStrategy.ErrorCode});
260+
const version = await execUtils.pipevp(process.execPath, [process.argv[1], `set`, `version`, `classic`, `--only-if-needed`, `--yarn-path`], {cwd, env, stdin, stdout, stderr, end: execUtils.EndStrategy.ErrorCode});
255261
if (version.code !== 0)
256262
return version.code;
257263

264+
// Revert any changes made to the Manifest by `set version`.
265+
await xfs.writeFilePromise(manifestPath, manifestBuffer);
266+
258267
// Otherwise Yarn 1 will pack the .yarn directory :(
259268
await xfs.appendFilePromise(ppath.join(cwd, `.npmignore` as PortablePath), `/.yarn\n`);
260269

0 commit comments

Comments
 (0)