Skip to content

Commit

Permalink
Revert "[build-utils] Fix corepack packageManager detection on mono…
Browse files Browse the repository at this point in the history
…repos" (#12242)

Reverts #12219 because it caused an issue with corepack
versions being set to `latest`.

https://vercel.slack.com/archives/C07R9QVM5L1
  • Loading branch information
EndangeredMassa authored Oct 8, 2024
1 parent fd9a850 commit 226028a
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 197 deletions.
10 changes: 10 additions & 0 deletions .changeset/chatty-plums-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@vercel/build-utils": patch
"@vercel/hydrogen": patch
"@vercel/next": patch
"@vercel/redwood": patch
"@vercel/remix-builder": patch
"@vercel/static-build": patch
---

Revert "[build-utils] Fix corepack `packageManager` detection on monorepos"
91 changes: 30 additions & 61 deletions packages/build-utils/src/fs/run-user-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ export interface ScanParentDirsResult {
* or `undefined` if not found.
*/
lockfileVersion?: number;
/**
* The contents of the `packageManager` field from `package.json` if found.
* The value may come from a different `package.json` file than the one
* specified by `packageJsonPath`, in the case of a monorepo.
*/
packageJsonPackageManager?: string;
/**
* Whether Turborepo supports the `COREPACK_HOME` environment variable.
* `undefined` if not a Turborepo project.
Expand Down Expand Up @@ -354,19 +348,17 @@ export async function scanParentDirs(
readPackageJson && pkgJsonPath
? JSON.parse(await fs.readFile(pkgJsonPath, 'utf8'))
: undefined;
const {
paths: [yarnLockPath, npmLockPath, pnpmLockPath, bunLockPath],
packageJsonPackageManager,
} = await walkParentDirsMulti({
base,
start: destPath,
filenames: [
'yarn.lock',
'package-lock.json',
'pnpm-lock.yaml',
'bun.lockb',
],
});
const [yarnLockPath, npmLockPath, pnpmLockPath, bunLockPath] =
await walkParentDirsMulti({
base,
start: destPath,
filenames: [
'yarn.lock',
'package-lock.json',
'pnpm-lock.yaml',
'bun.lockb',
],
});
let lockfilePath: string | undefined;
let lockfileVersion: number | undefined;
let cliType: CliType;
Expand Down Expand Up @@ -421,17 +413,19 @@ export async function scanParentDirs(
// TODO: read "bun-lockfile-format-v0"
lockfileVersion = 0;
} else {
cliType = detectPackageManagerNameWithoutLockfile(
packageJsonPackageManager,
turboSupportsCorepackHome
);
cliType =
packageJson && rootProjectInfo
? detectPackageManagerNameWithoutLockfile(
packageJson,
turboSupportsCorepackHome
)
: 'npm';
}

const packageJsonPath = pkgJsonPath || undefined;
return {
cliType,
packageJson,
packageJsonPackageManager,
lockfilePath,
lockfileVersion,
packageJsonPath,
Expand Down Expand Up @@ -468,9 +462,10 @@ function turboRangeSupportsCorepack(turboVersionRange: string) {
}

function detectPackageManagerNameWithoutLockfile(
packageJsonPackageManager: string | undefined,
packageJson: PackageJson,
turboSupportsCorepackHome: boolean | undefined
) {
const packageJsonPackageManager = packageJson.packageManager;
if (
usingCorepack(
process.env,
Expand Down Expand Up @@ -542,35 +537,20 @@ async function walkParentDirsMulti({
base,
start,
filenames,
}: WalkParentDirsMultiProps): Promise<{
paths: (string | undefined)[];
packageJsonPackageManager?: string;
}> {
let packageManager: string | undefined;

}: WalkParentDirsMultiProps): Promise<(string | undefined)[]> {
for (const dir of traverseUpDirectories({ start, base })) {
const fullPaths = filenames.map(f => path.join(dir, f));
const existResults = await Promise.all(
fullPaths.map(f => fs.pathExists(f))
);
const foundOneOrMore = existResults.some(b => b);
const packageJsonPath = path.join(dir, 'package.json');
const packageJson: PackageJson | null = await fs
.readJSON(packageJsonPath)
.catch(() => null);
if (packageJson?.packageManager) {
packageManager = packageJson.packageManager;
}

if (foundOneOrMore) {
return {
paths: fullPaths.map((f, i) => (existResults[i] ? f : undefined)),
packageJsonPackageManager: packageManager,
};
return fullPaths.map((f, i) => (existResults[i] ? f : undefined));
}
}

return { paths: [], packageJsonPackageManager: packageManager };
return [];
}

function isSet<T>(v: any): v is Set<T> {
Expand Down Expand Up @@ -598,7 +578,6 @@ export async function runNpmInstall(
packageJsonPath,
packageJson,
lockfileVersion,
packageJsonPackageManager,
turboSupportsCorepackHome,
} = await scanParentDirs(destPath, true);

Expand Down Expand Up @@ -635,7 +614,7 @@ export async function runNpmInstall(
opts.env = getEnvForPackageManager({
cliType,
lockfileVersion,
packageJsonPackageManager,
packageJsonPackageManager: packageJson?.packageManager,
nodeVersion,
env,
packageJsonEngines: packageJson?.engines,
Expand Down Expand Up @@ -1175,17 +1154,12 @@ export async function runCustomInstallCommand({
spawnOpts?: SpawnOptions;
}) {
console.log(`Running "install" command: \`${installCommand}\`...`);
const {
cliType,
lockfileVersion,
packageJson,
packageJsonPackageManager,
turboSupportsCorepackHome,
} = await scanParentDirs(destPath, true);
const { cliType, lockfileVersion, packageJson, turboSupportsCorepackHome } =
await scanParentDirs(destPath, true);
const env = getEnvForPackageManager({
cliType,
lockfileVersion,
packageJsonPackageManager,
packageJsonPackageManager: packageJson?.packageManager,
nodeVersion,
env: spawnOpts?.env || {},
packageJsonEngines: packageJson?.engines,
Expand All @@ -1206,13 +1180,8 @@ export async function runPackageJsonScript(
) {
assert(path.isAbsolute(destPath));

const {
packageJson,
cliType,
lockfileVersion,
packageJsonPackageManager,
turboSupportsCorepackHome,
} = await scanParentDirs(destPath, true);
const { packageJson, cliType, lockfileVersion, turboSupportsCorepackHome } =
await scanParentDirs(destPath, true);
const scriptName = getScriptName(
packageJson,
typeof scriptNames === 'string' ? [scriptNames] : scriptNames
Expand All @@ -1228,7 +1197,7 @@ export async function runPackageJsonScript(
env: getEnvForPackageManager({
cliType,
lockfileVersion,
packageJsonPackageManager,
packageJsonPackageManager: packageJson?.packageManager,
nodeVersion: undefined,
env: cloneEnv(process.env, spawnOpts?.env),
packageJsonEngines: packageJson?.engines,
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

35 changes: 0 additions & 35 deletions packages/build-utils/test/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -836,41 +836,6 @@ it('should detect non-turborepo monorepo', async () => {
expect(result.turboSupportsCorepackHome).toEqual(undefined);
});

it('should detect `packageManager` in npm monorepo', async () => {
try {
process.env.ENABLE_EXPERIMENTAL_COREPACK = '1';

const base = path.join(__dirname, 'fixtures', '41-npm-workspaces-corepack');
const fixture = path.join(base, 'a');
const result = await scanParentDirs(fixture, false, base);
expect(result.cliType).toEqual('npm');
expect(result.packageJsonPackageManager).toEqual('[email protected]');
expect(result.lockfileVersion).toEqual(undefined);
expect(result.packageJsonPath).toEqual(path.join(fixture, 'package.json'));
} finally {
delete process.env.ENABLE_EXPERIMENTAL_COREPACK;
}
});

it('should detect `packageManager` in pnpm monorepo', async () => {
try {
process.env.ENABLE_EXPERIMENTAL_COREPACK = '1';
const base = path.join(
__dirname,
'fixtures',
'42-pnpm-workspaces-corepack'
);
const fixture = path.join(base, 'c');
const result = await scanParentDirs(fixture, false, base);
expect(result.cliType).toEqual('pnpm');
expect(result.packageJsonPackageManager).toEqual('[email protected]');
expect(result.lockfileVersion).toEqual(undefined);
expect(result.packageJsonPath).toEqual(path.join(fixture, 'package.json'));
} finally {
delete process.env.ENABLE_EXPERIMENTAL_COREPACK;
}
});

it('should retry npm install when peer deps invalid and npm@8 on node@16', async () => {
const nodeMajor = Number(process.versions.node.split('.')[0]);
if (nodeMajor !== 16) {
Expand Down
10 changes: 3 additions & 7 deletions packages/hydrogen/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ export const build: BuildV2 = async ({
);

const spawnOpts = getSpawnOptions(meta, nodeVersion);
const {
cliType,
lockfileVersion,
packageJsonPackageManager,
turboSupportsCorepackHome,
} = await scanParentDirs(entrypointDir, true);
const { cliType, lockfileVersion, packageJson, turboSupportsCorepackHome } =
await scanParentDirs(entrypointDir, true);

spawnOpts.env = getEnvForPackageManager({
cliType,
lockfileVersion,
packageJsonPackageManager,
packageJsonPackageManager: packageJson?.packageManager,
nodeVersion,
env: spawnOpts.env || {},
turboSupportsCorepackHome,
Expand Down
10 changes: 3 additions & 7 deletions packages/next/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,13 @@ export const build: BuildV2 = async buildOptions => {
const nextVersionRange = await getNextVersionRange(entryPath);
const nodeVersion = await getNodeVersion(entryPath, undefined, config, meta);
const spawnOpts = getSpawnOptions(meta, nodeVersion);
const {
cliType,
lockfileVersion,
packageJsonPackageManager,
turboSupportsCorepackHome,
} = await scanParentDirs(entryPath, true);
const { cliType, lockfileVersion, packageJson, turboSupportsCorepackHome } =
await scanParentDirs(entryPath, true);

spawnOpts.env = getEnvForPackageManager({
cliType,
lockfileVersion,
packageJsonPackageManager,
packageJsonPackageManager: packageJson?.packageManager,
nodeVersion,
env: spawnOpts.env || {},
turboSupportsCorepackHome,
Expand Down
10 changes: 3 additions & 7 deletions packages/redwood/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,13 @@ export const build: BuildV2 = async ({
if (!spawnOpts.env) {
spawnOpts.env = {};
}
const {
cliType,
lockfileVersion,
packageJsonPackageManager,
turboSupportsCorepackHome,
} = await scanParentDirs(entrypointFsDirname, true);
const { cliType, lockfileVersion, packageJson, turboSupportsCorepackHome } =
await scanParentDirs(entrypointFsDirname, true);

spawnOpts.env = getEnvForPackageManager({
cliType,
lockfileVersion,
packageJsonPackageManager,
packageJsonPackageManager: packageJson?.packageManager,
nodeVersion,
env: spawnOpts.env || {},
turboSupportsCorepackHome,
Expand Down
4 changes: 2 additions & 2 deletions packages/remix/src/build-legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ export const build: BuildV2 = async ({
const {
cliType,
packageJsonPath,
packageJson,
lockfileVersion,
lockfilePath,
packageJsonPackageManager,
turboSupportsCorepackHome,
} = await scanParentDirs(entrypointFsDirname, true);

Expand All @@ -131,7 +131,7 @@ export const build: BuildV2 = async ({
spawnOpts.env = getEnvForPackageManager({
cliType,
lockfileVersion,
packageJsonPackageManager,
packageJsonPackageManager: packageJson?.packageManager,
nodeVersion,
env: spawnOpts.env,
turboSupportsCorepackHome,
Expand Down
Loading

0 comments on commit 226028a

Please sign in to comment.