Compare commits

...
Author SHA1 Message Date
AbbanMustafa bd48592bff [worker] Install package managers with Corepack 2026-08-14 18:37:28 -04:00
2 changed files with 116 additions and 8 deletions

No files matched your search

@@ -148,6 +148,75 @@ describe('prepareRuntimeEnvironment', () => {
);
});
it('uses Corepack to install package managers after installing custom Node.js', async () => {
const testCtx = { ...ctx, env: { ...ctx.env } };
mockRuntimeEnvironmentVersions();
await prepareRuntimeEnvironment(
testCtx,
{
node: '22.20.0',
corepack: true,
pnpm: '9.15.5',
yarn: '1.22.22',
},
false
);
expect(spawn).toHaveBeenCalledWith('corepack', ['enable'], expect.anything());
expect(spawn).toHaveBeenCalledWith(
'corepack',
['prepare', '[email protected]', '--activate'],
expect.anything()
);
expect(spawn).toHaveBeenCalledWith(
'corepack',
['prepare', '[email protected]', '--activate'],
expect.anything()
);
expect(spawn).not.toHaveBeenCalledWith(
'npm',
['-g', 'install', '[email protected]'],
expect.anything()
);
expect(spawn).not.toHaveBeenCalledWith(
'npm',
['-g', 'install', '[email protected]'],
expect.anything()
);
});
it('uses npm to install package managers when Corepack is disabled', async () => {
const testCtx = { ...ctx, env: { ...ctx.env } };
mockRuntimeEnvironmentVersions();
await prepareRuntimeEnvironment(
testCtx,
{
node: '22.20.0',
pnpm: '9.15.5',
yarn: '1.22.22',
},
false
);
expect(spawn).toHaveBeenCalledWith(
'npm',
['-g', 'install', '[email protected]'],
expect.anything()
);
expect(spawn).toHaveBeenCalledWith(
'npm',
['-g', 'install', '[email protected]'],
expect.anything()
);
expect(spawn).not.toHaveBeenCalledWith(
'corepack',
expect.arrayContaining(['prepare']),
expect.anything()
);
});
it('installs Bun when a specified version is different from installed version', async () => {
let isFirstTimeCheckingBunVersion = true;
jest.mocked(spawn).mockImplementation((cmd, _args, _opts) => {
@@ -269,6 +338,26 @@ function mockProcessPlatform(platform: NodeJS.Platform): void {
});
}
function mockRuntimeEnvironmentVersions(): void {
jest.mocked(spawn).mockImplementation((cmd, args) => {
if (cmd === 'bash' && args[1]?.includes('nvm install')) {
return Promise.resolve({
...spawnResult,
stdout: 'Now using node v22.20.0 (npm v10.9.3)\n',
});
} else if (cmd === 'pnpm') {
return Promise.resolve({ ...spawnResult, stdout: '9.15.5\n' });
} else if (cmd === 'yarn') {
return Promise.resolve({ ...spawnResult, stdout: '1.22.22\n' });
} else if (cmd === 'bun') {
return Promise.resolve({ ...spawnResult, stdout: '1.2.23\n' });
} else if (cmd === 'sharp') {
return Promise.resolve({ ...spawnResult, stdout: '5.2.0\n' });
}
return Promise.resolve(spawnResult);
});
}
function restoreEnv(key: string, value: string | undefined): void {
if (value === undefined) {
delete process.env[key];
+27 -8
View File
@@ -99,6 +99,7 @@ export async function prepareRuntimeEnvironment(
ctx,
userSpecifiedVersion: builderConfig.pnpm,
currentVersion: currentPnpmVersion,
useCorepack: Boolean(builderConfig.corepack),
});
}
if (
@@ -109,6 +110,7 @@ export async function prepareRuntimeEnvironment(
ctx,
userSpecifiedVersion: builderConfig.yarn,
currentVersion: currentYarnVersion,
useCorepack: Boolean(builderConfig.corepack),
});
}
if (builderConfig.bun && currentBunVersion !== builderConfig.bun) {
@@ -235,19 +237,18 @@ async function installPnpm({
ctx,
userSpecifiedVersion,
currentVersion,
useCorepack,
}: {
ctx: PreDownloadBuildContext;
userSpecifiedVersion: string | undefined;
currentVersion: string;
useCorepack: boolean;
}): Promise<void> {
const versionToInstall = userSpecifiedVersion ?? currentVersion;
try {
ctx.logger.info(`Installing pnpm@${versionToInstall}`);
await spawn('npm', ['-g', 'install', `pnpm@${versionToInstall}`], {
logger: ctx.logger,
env: ctx.env,
});
await installPackageManager(ctx, 'pnpm', versionToInstall, useCorepack);
const currentPnpmVersion = (
await spawn('pnpm', ['--version'], { stdio: 'pipe', env: ctx.env, cwd: os.tmpdir() })
).stdout.trim();
@@ -273,18 +274,17 @@ async function installYarn({
ctx,
userSpecifiedVersion,
currentVersion,
useCorepack,
}: {
ctx: PreDownloadBuildContext;
userSpecifiedVersion: string | undefined;
currentVersion: string;
useCorepack: boolean;
}): Promise<void> {
const versionToInstall = userSpecifiedVersion ?? currentVersion;
try {
ctx.logger.info(`Installing yarn@${versionToInstall}`);
await spawn('npm', ['-g', 'install', `yarn@${versionToInstall}`], {
logger: ctx.logger,
env: ctx.env,
});
await installPackageManager(ctx, 'yarn', versionToInstall, useCorepack);
const currentYarnVersion = (
await spawn('yarn', ['--version'], { stdio: 'pipe', env: ctx.env, cwd: os.tmpdir() })
).stdout.trim();
@@ -306,6 +306,25 @@ async function installYarn({
}
}
async function installPackageManager(
ctx: PreDownloadBuildContext,
packageManager: 'pnpm' | 'yarn',
version: string,
useCorepack: boolean
): Promise<void> {
if (useCorepack) {
await spawn('corepack', ['prepare', `${packageManager}@${version}`, '--activate'], {
logger: ctx.logger,
env: ctx.env,
});
} else {
await spawn('npm', ['-g', 'install', `${packageManager}@${version}`], {
logger: ctx.logger,
env: ctx.env,
});
}
}
async function installSharpCli(ctx: PreDownloadBuildContext, nodeVersion: string): Promise<void> {
const sharpCliVersion = (await spawn('sharp', ['--version'], { stdio: 'pipe' })).stdout.trim();
try {