Skip to content

Commit ae2bdab

Browse files
authored
Revert "Fix corepack packageManager detection on monorepos" (#11865)
Reverts #11811
1 parent 6bfead7 commit ae2bdab

File tree

16 files changed

+69
-174
lines changed

16 files changed

+69
-174
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@vercel/build-utils": patch
3+
"@vercel/hydrogen": patch
4+
"@vercel/next": patch
5+
"@vercel/redwood": patch
6+
"@vercel/remix-builder": patch
7+
"@vercel/static-build": patch
8+
---
9+
10+
Revert "Fix corepack `packageManager` detection on monorepos"

packages/build-utils/src/fs/run-user-scripts.ts

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@ export interface ScanParentDirsResult {
5252
* or `undefined` if not found.
5353
*/
5454
lockfileVersion?: number;
55-
/**
56-
* The contents of the `packageManager` field from `package.json` if found.
57-
* The value may come from a different `package.json` file than the one
58-
* specified by `packageJsonPath`, in the case of a monorepo.
59-
*/
60-
packageJsonPackageManager?: string;
6155
}
6256

6357
export interface TraverseUpDirectoriesProps {
@@ -315,19 +309,17 @@ export async function scanParentDirs(
315309
readPackageJson && pkgJsonPath
316310
? JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'))
317311
: undefined;
318-
const {
319-
paths: [yarnLockPath, npmLockPath, pnpmLockPath, bunLockPath],
320-
packageJsonPackageManager,
321-
} = await walkParentDirsMulti({
322-
base,
323-
start: destPath,
324-
filenames: [
325-
'yarn.lock',
326-
'package-lock.json',
327-
'pnpm-lock.yaml',
328-
'bun.lockb',
329-
],
330-
});
312+
const [yarnLockPath, npmLockPath, pnpmLockPath, bunLockPath] =
313+
await walkParentDirsMulti({
314+
base,
315+
start: destPath,
316+
filenames: [
317+
'yarn.lock',
318+
'package-lock.json',
319+
'pnpm-lock.yaml',
320+
'bun.lockb',
321+
],
322+
});
331323
let lockfilePath: string | undefined;
332324
let lockfileVersion: number | undefined;
333325
let cliType: CliType;
@@ -367,25 +359,23 @@ export async function scanParentDirs(
367359
// TODO: read "bun-lockfile-format-v0"
368360
lockfileVersion = 0;
369361
} else {
370-
cliType = detectPackageManagerNameWithoutLockfile(
371-
packageJsonPackageManager
372-
);
362+
cliType = packageJson
363+
? detectPackageManagerNameWithoutLockfile(packageJson)
364+
: 'npm';
373365
}
374366

375367
const packageJsonPath = pkgJsonPath || undefined;
376368
return {
377369
cliType,
378370
packageJson,
379-
packageJsonPackageManager,
380371
lockfilePath,
381372
lockfileVersion,
382373
packageJsonPath,
383374
};
384375
}
385376

386-
function detectPackageManagerNameWithoutLockfile(
387-
packageJsonPackageManager?: string
388-
) {
377+
function detectPackageManagerNameWithoutLockfile(packageJson: PackageJson) {
378+
const packageJsonPackageManager = packageJson.packageManager;
389379
if (usingCorepack(process.env, packageJsonPackageManager)) {
390380
const corepackPackageManager = validateVersionSpecifier(
391381
packageJsonPackageManager
@@ -439,35 +429,20 @@ async function walkParentDirsMulti({
439429
base,
440430
start,
441431
filenames,
442-
}: WalkParentDirsMultiProps): Promise<{
443-
paths: (string | undefined)[];
444-
packageJsonPackageManager?: string;
445-
}> {
446-
let packageManager: string | undefined;
447-
432+
}: WalkParentDirsMultiProps): Promise<(string | undefined)[]> {
448433
for (const dir of traverseUpDirectories({ start, base })) {
449434
const fullPaths = filenames.map(f => path.join(dir, f));
450435
const existResults = await Promise.all(
451436
fullPaths.map(f => fs.pathExists(f))
452437
);
453438
const foundOneOrMore = existResults.some(b => b);
454-
const packageJsonPath = path.join(dir, 'package.json');
455-
const packageJson: PackageJson | null = await fs
456-
.readJSON(packageJsonPath)
457-
.catch(() => null);
458-
if (packageJson?.packageManager) {
459-
packageManager = packageJson.packageManager;
460-
}
461439

462440
if (foundOneOrMore) {
463-
return {
464-
paths: fullPaths.map((f, i) => (existResults[i] ? f : undefined)),
465-
packageJsonPackageManager: packageManager,
466-
};
441+
return fullPaths.map((f, i) => (existResults[i] ? f : undefined));
467442
}
468443
}
469444

470-
return { paths: [], packageJsonPackageManager: packageManager };
445+
return [];
471446
}
472447

473448
function isSet<T>(v: any): v is Set<T> {
@@ -490,12 +465,8 @@ export async function runNpmInstall(
490465

491466
try {
492467
await runNpmInstallSema.acquire();
493-
const {
494-
cliType,
495-
packageJsonPath,
496-
lockfileVersion,
497-
packageJsonPackageManager,
498-
} = await scanParentDirs(destPath);
468+
const { cliType, packageJsonPath, packageJson, lockfileVersion } =
469+
await scanParentDirs(destPath, true);
499470

500471
if (!packageJsonPath) {
501472
debug(
@@ -530,7 +501,7 @@ export async function runNpmInstall(
530501
opts.env = getEnvForPackageManager({
531502
cliType,
532503
lockfileVersion,
533-
packageJsonPackageManager,
504+
packageJsonPackageManager: packageJson?.packageManager,
534505
nodeVersion,
535506
env,
536507
});
@@ -1024,12 +995,14 @@ export async function runCustomInstallCommand({
1024995
spawnOpts?: SpawnOptions;
1025996
}) {
1026997
console.log(`Running "install" command: \`${installCommand}\`...`);
1027-
const { cliType, lockfileVersion, packageJsonPackageManager } =
1028-
await scanParentDirs(destPath);
998+
const { cliType, lockfileVersion, packageJson } = await scanParentDirs(
999+
destPath,
1000+
true
1001+
);
10291002
const env = getEnvForPackageManager({
10301003
cliType,
10311004
lockfileVersion,
1032-
packageJsonPackageManager,
1005+
packageJsonPackageManager: packageJson?.packageManager,
10331006
nodeVersion,
10341007
env: spawnOpts?.env || {},
10351008
});
@@ -1048,8 +1021,10 @@ export async function runPackageJsonScript(
10481021
) {
10491022
assert(path.isAbsolute(destPath));
10501023

1051-
const { packageJson, cliType, lockfileVersion, packageJsonPackageManager } =
1052-
await scanParentDirs(destPath, true);
1024+
const { packageJson, cliType, lockfileVersion } = await scanParentDirs(
1025+
destPath,
1026+
true
1027+
);
10531028
const scriptName = getScriptName(
10541029
packageJson,
10551030
typeof scriptNames === 'string' ? [scriptNames] : scriptNames
@@ -1065,7 +1040,7 @@ export async function runPackageJsonScript(
10651040
env: getEnvForPackageManager({
10661041
cliType,
10671042
lockfileVersion,
1068-
packageJsonPackageManager,
1043+
packageJsonPackageManager: packageJson?.packageManager,
10691044
nodeVersion: undefined,
10701045
env: cloneEnv(process.env, spawnOpts?.env),
10711046
}),

packages/build-utils/test/fixtures/41-npm-workspaces-corepack/a/package.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/build-utils/test/fixtures/41-npm-workspaces-corepack/b/package.json

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/build-utils/test/fixtures/41-npm-workspaces-corepack/package.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/build-utils/test/fixtures/42-pnpm-workspaces-corepack/c/package.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/build-utils/test/fixtures/42-pnpm-workspaces-corepack/d/package.json

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/build-utils/test/fixtures/42-pnpm-workspaces-corepack/package.json

Lines changed: 0 additions & 7 deletions
This file was deleted.

packages/build-utils/test/fixtures/42-pnpm-workspaces-corepack/pnpm-workspace.yaml

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/build-utils/test/unit.test.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -713,43 +713,6 @@ it('should detect package.json in nested frontend', async () => {
713713
expect(result.packageJsonPath).toEqual(path.join(fixture, 'package.json'));
714714
});
715715

716-
it('should detect `packageManager` in npm monorepo', async () => {
717-
try {
718-
process.env.ENABLE_EXPERIMENTAL_COREPACK = '1';
719-
720-
const base = path.join(__dirname, 'fixtures', '41-npm-workspaces-corepack');
721-
const fixture = path.join(base, 'a');
722-
const result = await scanParentDirs(fixture, false, base);
723-
console.log(result);
724-
expect(result.cliType).toEqual('npm');
725-
expect(result.packageJsonPackageManager).toEqual('[email protected]');
726-
expect(result.lockfileVersion).toEqual(undefined);
727-
expect(result.packageJsonPath).toEqual(path.join(fixture, 'package.json'));
728-
} finally {
729-
delete process.env.ENABLE_EXPERIMENTAL_COREPACK;
730-
}
731-
});
732-
733-
it('should detect `packageManager` in pnpm monorepo', async () => {
734-
try {
735-
process.env.ENABLE_EXPERIMENTAL_COREPACK = '1';
736-
const base = path.join(
737-
__dirname,
738-
'fixtures',
739-
'42-pnpm-workspaces-corepack'
740-
);
741-
const fixture = path.join(base, 'c');
742-
const result = await scanParentDirs(fixture, false, base);
743-
console.log(result);
744-
expect(result.cliType).toEqual('pnpm');
745-
expect(result.packageJsonPackageManager).toEqual('[email protected]');
746-
expect(result.lockfileVersion).toEqual(undefined);
747-
expect(result.packageJsonPath).toEqual(path.join(fixture, 'package.json'));
748-
} finally {
749-
delete process.env.ENABLE_EXPERIMENTAL_COREPACK;
750-
}
751-
});
752-
753716
it('should retry npm install when peer deps invalid and npm@8 on node@16', async () => {
754717
const nodeMajor = Number(process.versions.node.split('.')[0]);
755718
if (nodeMajor !== 16) {

0 commit comments

Comments
 (0)