Skip to content

Commit 0294528

Browse files
authored
Prefer using exclusively Corepack when possible (#4254)
* Uses exclusively Corepack by default * Fixes tests * Implements --yarn-path * Updates documentation * Fixes types * Fixes types * Uses yarnPath by default if Corepack isnt enabled * Adds tests * Reverts parts of the changes * Fixes broken plugin tests * Fixes windows set version from file * Fixes merge conflicts
1 parent 952e88e commit 0294528

File tree

7 files changed

+278
-81
lines changed

7 files changed

+278
-81
lines changed

.yarn/versions/2847fa24.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
releases:
2+
"@yarnpkg/cli": major
3+
"@yarnpkg/plugin-essentials": major
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-init"
10+
- "@yarnpkg/plugin-interactive-tools"
11+
- "@yarnpkg/plugin-nm"
12+
- "@yarnpkg/plugin-npm-cli"
13+
- "@yarnpkg/plugin-pack"
14+
- "@yarnpkg/plugin-patch"
15+
- "@yarnpkg/plugin-pnp"
16+
- "@yarnpkg/plugin-pnpm"
17+
- "@yarnpkg/plugin-stage"
18+
- "@yarnpkg/plugin-typescript"
19+
- "@yarnpkg/plugin-version"
20+
- "@yarnpkg/plugin-workspace-tools"
21+
- "@yarnpkg/builder"
22+
- "@yarnpkg/core"
23+
- "@yarnpkg/doctor"

packages/acceptance-tests/pkg-tests-core/sources/utils/tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface RunDriverOptions extends Record<string, any> {
2727
cwd?: PortablePath;
2828
projectFolder?: PortablePath;
2929
registryUrl: string;
30-
env?: Record<string, string>;
30+
env?: Record<string, string | undefined>;
3131
}
3232

3333
export type PackageRunDriver = (
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import {xfs, ppath, PortablePath, Filename} from '@yarnpkg/fslib';
2+
3+
const yarnrcRegexp = /^yarnPath:/;
4+
5+
describe(`Commands`, () => {
6+
describe(`set version`, () => {
7+
test(
8+
`it shouldn't set yarnPath if corepack is enabled and the version is semver`,
9+
makeTemporaryEnv({}, {
10+
env: {COREPACK_ROOT: `/path/to/corepack`},
11+
}, async ({path, run, source}) => {
12+
await run(`set`, `version`, `3.0.0`);
13+
await check(path, {corepackVersion: `3.0.0`, usePath: false});
14+
}),
15+
);
16+
17+
test(
18+
`it should set yarnPath if corepack is disabled, even when the version is semver`,
19+
makeTemporaryEnv({}, {
20+
env: {COREPACK_ROOT: undefined},
21+
}, async ({path, run, source}) => {
22+
await run(`set`, `version`, `3.0.0`);
23+
await check(path, {corepackVersion: `3.0.0`, usePath: true});
24+
}),
25+
);
26+
27+
test(
28+
`it should always set yarnPath if one already exists`,
29+
makeTemporaryEnv({}, {
30+
env: {COREPACK_ROOT: `/path/to/corepack`},
31+
}, async ({path, run, source}) => {
32+
// To force yarnPath to be set; followed by a sanity check
33+
await run(`set`, `version`, `3.0.0`, {env: {COREPACK_ROOT: undefined}});
34+
await check(path, {corepackVersion: `3.0.0`, usePath: true});
35+
36+
await run(`set`, `version`, `3.0.0`);
37+
await check(path, {corepackVersion: `3.0.0`, usePath: true});
38+
}),
39+
);
40+
41+
test(
42+
`it should always set yarnPath if --yarn-path is set`,
43+
makeTemporaryEnv({}, {
44+
env: {COREPACK_ROOT: `/path/to/corepack`},
45+
}, async ({path, run, source}) => {
46+
await run(`set`, `version`, `3.0.0`, `--yarn-path`);
47+
await check(path, {corepackVersion: `3.0.0`, usePath: true});
48+
}),
49+
);
50+
51+
test(
52+
`it should never set yarnPath if --no-yarn-path is set`,
53+
makeTemporaryEnv({}, {
54+
env: {COREPACK_ROOT: undefined},
55+
}, async ({path, run, source}) => {
56+
await run(`set`, `version`, `3.0.0`, `--no-yarn-path`);
57+
await check(path, {corepackVersion: `3.0.0`, usePath: false});
58+
}),
59+
);
60+
61+
test(
62+
`it should prevent using --no-yarn-path with arbitrary files`,
63+
makeTemporaryEnv({}, {
64+
env: {COREPACK_ROOT: undefined},
65+
}, async ({path, run, source}) => {
66+
const yarnIndirection = ppath.join(path, `custom-yarn.cjs` as Filename);
67+
await xfs.writeFilePromise(yarnIndirection, ``);
68+
69+
await expect(run(`set`, `version`, yarnIndirection, `--no-yarn-path`)).rejects.toThrow();
70+
}),
71+
);
72+
73+
test(
74+
`it should set yarnPath if the version is an arbitrary file`,
75+
makeTemporaryEnv({}, {
76+
env: {COREPACK_ROOT: undefined},
77+
}, async ({path, run, source}) => {
78+
const yarnIndirection = ppath.join(path, `custom-yarn.cjs` as Filename);
79+
await xfs.writeFilePromise(yarnIndirection, ``);
80+
81+
await run(`set`, `version`, yarnIndirection);
82+
await check(path, {corepackVersion: /[0-9]+\./, usePath: true});
83+
}),
84+
);
85+
});
86+
});
87+
88+
async function check(path: PortablePath, checks: {corepackVersion: string | RegExp, usePath: boolean}) {
89+
const releasesPath = ppath.join(path, `.yarn/releases` as PortablePath);
90+
const yarnrcPath = ppath.join(path, Filename.rc);
91+
const manifestPath = ppath.join(path, Filename.manifest);
92+
93+
let releases: Array<string> | null;
94+
try {
95+
releases = await xfs.readdirPromise(releasesPath);
96+
} catch (err) {
97+
if (err.code === `ENOENT`) {
98+
releases = null;
99+
} else {
100+
throw err;
101+
}
102+
}
103+
104+
let yarnrcFile;
105+
try {
106+
yarnrcFile = await xfs.readFilePromise(yarnrcPath, `utf8`);
107+
} catch (err) {
108+
if (err.code === `ENOENT`) {
109+
yarnrcFile = ``;
110+
} else {
111+
throw err;
112+
}
113+
}
114+
115+
if (checks.usePath)
116+
expect(releases).toHaveLength(1);
117+
else
118+
expect(releases).toEqual(null);
119+
120+
if (checks.usePath)
121+
expect(yarnrcFile).toMatch(yarnrcRegexp);
122+
else
123+
expect(yarnrcFile).not.toMatch(yarnrcRegexp);
124+
125+
await expect(xfs.readJsonPromise(manifestPath)).resolves.toMatchObject({
126+
packageManager: checks.corepackVersion instanceof RegExp
127+
? expect.stringMatching(`yarn@${checks.corepackVersion.source}`)
128+
: `yarn@${checks.corepackVersion}`,
129+
});
130+
}

0 commit comments

Comments
 (0)