Skip to content

Commit 5eadc50

Browse files
authored
feat: when strict checking is off, treat like transparent (#197)
1 parent 55005a9 commit 5eadc50

9 files changed

+46
-2
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,17 @@ This command will retrieve the given package manager from the specified archive
134134
manager versions that will be required for the projects you'll run, using
135135
`corepack hydrate`).
136136

137-
- `COREPACK_ENABLE_STRICT` can be set to `0` to prevent Corepack from checking
137+
- `COREPACK_ENABLE_STRICT` can be set to `0` to prevent Corepack from throwing error
138+
if the package manager does not correspond to the one defined for the current project.
139+
This means that if a user is using the package manager specified in the current project,
140+
it will use the version specified by the project's `packageManager` field.
141+
But if the user is using other package manager different from the one specified
142+
for the current project, it will use the system-wide package manager version.
143+
144+
- `COREPACK_ENABLE_PROJECT_SPEC` can be set to `0` to prevent Corepack from checking
138145
if the package manager corresponds to the one defined for the current project.
146+
This means that it will always use the system-wide package manager regardless of
147+
what is being specified in the project's `packageManager` field.
139148

140149
- `COREPACK_HOME` can be set in order to define where Corepack should install
141150
the package managers. By default it is set to `%LOCALAPPDATA%\node\corepack`

sources/specUtils.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export async function findProjectSpec(initialCwd: string, locator: Locator, {tra
4444
// A locator is a valid descriptor (but not the other way around)
4545
const fallbackLocator = {name: locator.name, range: locator.reference};
4646

47-
if (process.env.COREPACK_ENABLE_STRICT === `0`) return fallbackLocator;
47+
if (process.env.COREPACK_ENABLE_PROJECT_SPEC === `0`)
48+
return fallbackLocator;
49+
50+
if (process.env.COREPACK_ENABLE_STRICT === `0`)
51+
transparent = true;
4852

4953
while (true) {
5054
const result = await loadSpec(initialCwd);

tests/main.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,14 @@ it(`should refuse to run a different package manager within a configured project
270270
process.env.COREPACK_ENABLE_STRICT = `0`;
271271

272272
try {
273+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
274+
stdout: `1.0.0\n`,
275+
stderr: ``,
276+
exitCode: 0,
277+
});
273278
await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
274279
stdout: `${config.definitions.pnpm.default.split(`+`, 1)[0]}\n`,
280+
stderr: ``,
275281
exitCode: 0,
276282
});
277283
} finally {
@@ -281,6 +287,31 @@ it(`should refuse to run a different package manager within a configured project
281287
});
282288
});
283289

290+
291+
it(`should always use fallback version when project spec env is disabled`, async () => {
292+
await xfs.mktempPromise(async cwd => {
293+
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
294+
packageManager: `yarn@1.0.0`,
295+
});
296+
process.env.COREPACK_ENABLE_PROJECT_SPEC = `0`;
297+
298+
try {
299+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
300+
stdout: `${config.definitions.yarn.default.split(`+`, 1)[0]}\n`,
301+
stderr: ``,
302+
exitCode: 0,
303+
});
304+
await expect(runCli(cwd, [`pnpm`, `--version`])).resolves.toMatchObject({
305+
stdout: `${config.definitions.pnpm.default.split(`+`, 1)[0]}\n`,
306+
stderr: ``,
307+
exitCode: 0,
308+
});
309+
} finally {
310+
delete process.env.COREPACK_ENABLE_PROJECT_SPEC;
311+
}
312+
});
313+
});
314+
284315
it(`should allow to call "prepare" with --all to prepare all package managers`, async () => {
285316
await xfs.mktempPromise(async cwd => {
286317
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
22.1 KB
Binary file not shown.
3.44 MB
Binary file not shown.
-3.98 MB
Binary file not shown.
5.59 MB
Binary file not shown.
2.37 MB
Binary file not shown.
5.59 MB
Binary file not shown.

0 commit comments

Comments
 (0)