Skip to content

Commit 8e90f41

Browse files
1 parent 7349ce2 commit 8e90f41

File tree

16 files changed

+69
-175
lines changed

16 files changed

+69
-175
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 "Revert "Revert "Fix corepack `packageManager` detection on monorepos"""

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

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ export interface ScanParentDirsResult {
5959
* or `undefined` if not found.
6060
*/
6161
lockfileVersion?: number;
62-
/**
63-
* The contents of the `packageManager` field from `package.json` if found.
64-
* The value may come from a different `package.json` file than the one
65-
* specified by `packageJsonPath`, in the case of a monorepo.
66-
*/
67-
packageJsonPackageManager?: string;
6862
}
6963

7064
export interface TraverseUpDirectoriesProps {
@@ -322,19 +316,17 @@ export async function scanParentDirs(
322316
readPackageJson && pkgJsonPath
323317
? JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'))
324318
: undefined;
325-
const {
326-
paths: [yarnLockPath, npmLockPath, pnpmLockPath, bunLockPath],
327-
packageJsonPackageManager,
328-
} = await walkParentDirsMulti({
329-
base,
330-
start: destPath,
331-
filenames: [
332-
'yarn.lock',
333-
'package-lock.json',
334-
'pnpm-lock.yaml',
335-
'bun.lockb',
336-
],
337-
});
319+
const [yarnLockPath, npmLockPath, pnpmLockPath, bunLockPath] =
320+
await walkParentDirsMulti({
321+
base,
322+
start: destPath,
323+
filenames: [
324+
'yarn.lock',
325+
'package-lock.json',
326+
'pnpm-lock.yaml',
327+
'bun.lockb',
328+
],
329+
});
338330
let lockfilePath: string | undefined;
339331
let lockfileVersion: number | undefined;
340332
let cliType: CliType;
@@ -374,25 +366,23 @@ export async function scanParentDirs(
374366
// TODO: read "bun-lockfile-format-v0"
375367
lockfileVersion = 0;
376368
} else {
377-
cliType = detectPackageManagerNameWithoutLockfile(
378-
packageJsonPackageManager
379-
);
369+
cliType = packageJson
370+
? detectPackageManagerNameWithoutLockfile(packageJson)
371+
: 'npm';
380372
}
381373

382374
const packageJsonPath = pkgJsonPath || undefined;
383375
return {
384376
cliType,
385377
packageJson,
386-
packageJsonPackageManager,
387378
lockfilePath,
388379
lockfileVersion,
389380
packageJsonPath,
390381
};
391382
}
392383

393-
function detectPackageManagerNameWithoutLockfile(
394-
packageJsonPackageManager?: string
395-
) {
384+
function detectPackageManagerNameWithoutLockfile(packageJson: PackageJson) {
385+
const packageJsonPackageManager = packageJson.packageManager;
396386
if (usingCorepack(process.env, packageJsonPackageManager)) {
397387
const corepackPackageManager = validateVersionSpecifier(
398388
packageJsonPackageManager
@@ -446,35 +436,20 @@ async function walkParentDirsMulti({
446436
base,
447437
start,
448438
filenames,
449-
}: WalkParentDirsMultiProps): Promise<{
450-
paths: (string | undefined)[];
451-
packageJsonPackageManager?: string;
452-
}> {
453-
let packageManager: string | undefined;
454-
439+
}: WalkParentDirsMultiProps): Promise<(string | undefined)[]> {
455440
for (const dir of traverseUpDirectories({ start, base })) {
456441
const fullPaths = filenames.map(f => path.join(dir, f));
457442
const existResults = await Promise.all(
458443
fullPaths.map(f => fs.pathExists(f))
459444
);
460445
const foundOneOrMore = existResults.some(b => b);
461-
const packageJsonPath = path.join(dir, 'package.json');
462-
const packageJson: PackageJson | null = await fs
463-
.readJSON(packageJsonPath)
464-
.catch(() => null);
465-
if (packageJson?.packageManager) {
466-
packageManager = packageJson.packageManager;
467-
}
468446

469447
if (foundOneOrMore) {
470-
return {
471-
paths: fullPaths.map((f, i) => (existResults[i] ? f : undefined)),
472-
packageJsonPackageManager: packageManager,
473-
};
448+
return fullPaths.map((f, i) => (existResults[i] ? f : undefined));
474449
}
475450
}
476451

477-
return { paths: [], packageJsonPackageManager: packageManager };
452+
return [];
478453
}
479454

480455
function isSet<T>(v: any): v is Set<T> {
@@ -497,13 +472,8 @@ export async function runNpmInstall(
497472

498473
try {
499474
await runNpmInstallSema.acquire();
500-
const {
501-
cliType,
502-
packageJsonPath,
503-
lockfileVersion,
504-
packageJsonPackageManager,
505-
packageJson,
506-
} = await scanParentDirs(destPath);
475+
const { cliType, packageJsonPath, packageJson, lockfileVersion } =
476+
await scanParentDirs(destPath, true);
507477

508478
if (!packageJsonPath) {
509479
debug(
@@ -538,7 +508,7 @@ export async function runNpmInstall(
538508
opts.env = getEnvForPackageManager({
539509
cliType,
540510
lockfileVersion,
541-
packageJsonPackageManager,
511+
packageJsonPackageManager: packageJson?.packageManager,
542512
nodeVersion,
543513
env,
544514
packageJsonEngines: packageJson?.engines,
@@ -1071,12 +1041,14 @@ export async function runCustomInstallCommand({
10711041
spawnOpts?: SpawnOptions;
10721042
}) {
10731043
console.log(`Running "install" command: \`${installCommand}\`...`);
1074-
const { cliType, lockfileVersion, packageJsonPackageManager, packageJson } =
1075-
await scanParentDirs(destPath);
1044+
const { cliType, lockfileVersion, packageJson } = await scanParentDirs(
1045+
destPath,
1046+
true
1047+
);
10761048
const env = getEnvForPackageManager({
10771049
cliType,
10781050
lockfileVersion,
1079-
packageJsonPackageManager,
1051+
packageJsonPackageManager: packageJson?.packageManager,
10801052
nodeVersion,
10811053
env: spawnOpts?.env || {},
10821054
packageJsonEngines: packageJson?.engines,
@@ -1096,8 +1068,10 @@ export async function runPackageJsonScript(
10961068
) {
10971069
assert(path.isAbsolute(destPath));
10981070

1099-
const { packageJson, cliType, lockfileVersion, packageJsonPackageManager } =
1100-
await scanParentDirs(destPath, true);
1071+
const { packageJson, cliType, lockfileVersion } = await scanParentDirs(
1072+
destPath,
1073+
true
1074+
);
11011075
const scriptName = getScriptName(
11021076
packageJson,
11031077
typeof scriptNames === 'string' ? [scriptNames] : scriptNames
@@ -1113,7 +1087,7 @@ export async function runPackageJsonScript(
11131087
env: getEnvForPackageManager({
11141088
cliType,
11151089
lockfileVersion,
1116-
packageJsonPackageManager,
1090+
packageJsonPackageManager: packageJson?.packageManager,
11171091
nodeVersion: undefined,
11181092
env: cloneEnv(process.env, spawnOpts?.env),
11191093
packageJsonEngines: packageJson?.engines,

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)