[remix] Reinstall dependencies during prepareCache() (#10922)

https://github.com/vercel/vercel/pull/10819 introduced a bug causing the `prepareCache()` function to fail (due to `@remix-run/dev` no longer being require-able). Even if it were not failing, the deps installed are not a valid representation of the user's `package.json` / lockfile. So to have more truthful cache contents, run `npm install` once again during `prepareCache()` to fix both issues at the same time.
This commit is contained in:
Nathan Rajlich
2023-12-11 16:41:51 -08:00
committed by GitHub
parent ab34a26205
commit d09dd1794b
3 changed files with 56 additions and 11 deletions

View File

@@ -104,15 +104,17 @@ export const build: BuildV2 = async ({
meta
);
const { cliType, packageJsonPath, lockfileVersion } = await scanParentDirs(
entrypointFsDirname
);
const { cliType, packageJsonPath, lockfileVersion, lockfilePath } =
await scanParentDirs(entrypointFsDirname);
if (!packageJsonPath) {
throw new Error('Failed to locate `package.json` file in your project');
}
const pkgRaw = await fs.readFile(packageJsonPath, 'utf8');
const [lockfileRaw, pkgRaw] = await Promise.all([
lockfilePath ? fs.readFile(lockfilePath) : null,
fs.readFile(packageJsonPath, 'utf8'),
]);
const pkg = JSON.parse(pkgRaw);
const spawnOpts = getSpawnOptions(meta, nodeVersion);
@@ -423,9 +425,7 @@ module.exports = config;`;
cleanupOps.push(
fs
.rename(renamedRemixConfigPath, remixConfigPath)
.then(() =>
debug(`Restored original "${basename(remixConfigPath)}" file`)
)
.then(() => debug(`Restored original "${remixConfigPath}" file`))
);
}
// Restore original server entrypoint if it was modified (for Hydrogen v2)
@@ -433,11 +433,24 @@ module.exports = config;`;
cleanupOps.push(
fs
.writeFile(serverEntryPointAbs, originalServerEntryPoint)
.then(() =>
debug(`Restored original "${basename(serverEntryPointAbs!)}" file`)
)
.then(() => debug(`Restored original "${serverEntryPointAbs}" file`))
);
}
// Restore original `package.json` file and lockfile
if (depsModified) {
cleanupOps.push(
fs
.writeFile(packageJsonPath, pkgRaw)
.then(() => debug(`Restored original "${packageJsonPath}" file`))
);
if (lockfilePath && lockfileRaw) {
cleanupOps.push(
fs
.writeFile(lockfilePath, lockfileRaw)
.then(() => debug(`Restored original "${lockfilePath}" file`))
);
}
}
await Promise.all(cleanupOps);
}

View File

@@ -1,4 +1,9 @@
import { glob } from '@vercel/build-utils';
import {
getNodeVersion,
getSpawnOptions,
glob,
runNpmInstall,
} from '@vercel/build-utils';
import { dirname, join, relative } from 'path';
import { require_, chdirAndReadConfig } from './utils';
import type { PrepareCache } from '@vercel/build-utils';
@@ -7,10 +12,32 @@ export const prepareCache: PrepareCache = async ({
entrypoint,
repoRootPath,
workPath,
config,
}) => {
const root = repoRootPath || workPath;
const mountpoint = dirname(entrypoint);
const entrypointFsDirname = join(workPath, mountpoint);
// Because the `node_modules` directory was modified to install
// the forked Remix compiler, re-install to the "fresh" dependencies
// state before the cache gets created.
const nodeVersion = await getNodeVersion(
entrypointFsDirname,
undefined,
config
);
const spawnOpts = getSpawnOptions({}, nodeVersion);
await runNpmInstall(
entrypointFsDirname,
[],
{
...spawnOpts,
stdio: 'ignore',
},
undefined,
nodeVersion
);
const packageJsonPath = join(entrypointFsDirname, 'package.json');
const remixRunDevPath = dirname(
require_.resolve('@remix-run/dev/package.json', {