[remix] Clean up dev symlink upon config error (#9595)

Move the try/catch block to immediately after the symlink call, so that if reading the `remix.config.js` file throws an error or if there's an error parsing the `export const config` in a route, the catch block will clean up after itself properly.

I recommend reviewing with [whitespace changes hidden](https://github.com/vercel/vercel/pull/9595/files?w=1).
This commit is contained in:
Nathan Rajlich
2023-03-03 12:02:27 -08:00
committed by GitHub
parent 7f89aca52c
commit 15eb018f84

View File

@@ -30,7 +30,7 @@ import type {
BuildResultV2Typical, BuildResultV2Typical,
} from '@vercel/build-utils'; } from '@vercel/build-utils';
import type { BaseFunctionConfig } from '@vercel/static-config'; import type { BaseFunctionConfig } from '@vercel/static-config';
import type { RemixConfig } from '@remix-run/dev/dist/config'; import type { AppConfig, RemixConfig } from '@remix-run/dev/dist/config';
import type { ConfigRoute } from '@remix-run/dev/dist/config/routes'; import type { ConfigRoute } from '@remix-run/dev/dist/config/routes';
import { import {
calculateRouteConfigHash, calculateRouteConfigHash,
@@ -111,14 +111,6 @@ export const build: BuildV2 = async ({
await runNpmInstall(entrypointFsDirname, [], spawnOpts, meta, nodeVersion); await runNpmInstall(entrypointFsDirname, [], spawnOpts, meta, nodeVersion);
} }
const remixDevPackageJsonPath = _require.resolve(
'@remix-run/dev/package.json',
{ paths: [entrypointFsDirname] }
);
const remixVersion = JSON.parse(
await fs.readFile(remixDevPackageJsonPath, 'utf8')
).version;
// Make our version of `remix` CLI available to the project's build // Make our version of `remix` CLI available to the project's build
// command by creating a symlink to the copy in our node modules, // command by creating a symlink to the copy in our node modules,
// so that `serverBundles` works: https://github.com/remix-run/remix/pull/5479 // so that `serverBundles` works: https://github.com/remix-run/remix/pull/5479
@@ -127,15 +119,34 @@ export const build: BuildV2 = async ({
repoRootPath, repoRootPath,
'@remix-run/dev' '@remix-run/dev'
); );
const remixVersion = JSON.parse(
await fs.readFile(join(remixRunDevPath, 'package.json'), 'utf8')
).version;
let remixConfigWrapped = false;
const remixConfigPath = findConfig(entrypointFsDirname, 'remix.config');
const renamedRemixConfigPath = remixConfigPath
? `${remixConfigPath}.original${extname(remixConfigPath)}`
: undefined;
const backupRemixRunDevPath = `${remixRunDevPath}.__vercel_backup`; const backupRemixRunDevPath = `${remixRunDevPath}.__vercel_backup`;
await fs.rename(remixRunDevPath, backupRemixRunDevPath); await fs.rename(remixRunDevPath, backupRemixRunDevPath);
await fs.symlink(REMIX_RUN_DEV_PATH, remixRunDevPath); await fs.symlink(REMIX_RUN_DEV_PATH, remixRunDevPath);
// These get populated inside the try/catch below
let serverBundles: AppConfig['serverBundles'];
let remixConfig: RemixConfig;
let remixRoutes: ConfigRoute[];
const serverBundlesMap = new Map<string, ConfigRoute[]>();
const resolvedConfigsMap = new Map<ConfigRoute, ResolvedRouteConfig>();
try {
// Make `remix build` output production mode // Make `remix build` output production mode
spawnOpts.env.NODE_ENV = 'production'; spawnOpts.env.NODE_ENV = 'production';
const remixConfig = await chdirAndReadConfig(entrypointFsDirname); remixConfig = await chdirAndReadConfig(entrypointFsDirname);
const remixRoutes = Object.values(remixConfig.routes); remixRoutes = Object.values(remixConfig.routes);
// Read the `export const config` (if any) for each route // Read the `export const config` (if any) for each route
const project = new Project(); const project = new Project();
@@ -146,7 +157,6 @@ export const build: BuildV2 = async ({
staticConfigsMap.set(route, staticConfig); staticConfigsMap.set(route, staticConfig);
} }
const resolvedConfigsMap = new Map<ConfigRoute, ResolvedRouteConfig>();
for (const route of remixRoutes) { for (const route of remixRoutes) {
const config = getResolvedRouteConfig( const config = getResolvedRouteConfig(
route, route,
@@ -158,7 +168,6 @@ export const build: BuildV2 = async ({
// Figure out which routes belong to which server bundles // Figure out which routes belong to which server bundles
// based on having common static config properties // based on having common static config properties
const serverBundlesMap = new Map<string, ConfigRoute[]>();
for (const route of remixRoutes) { for (const route of remixRoutes) {
if (isLayoutRoute(route.id, remixRoutes)) continue; if (isLayoutRoute(route.id, remixRoutes)) continue;
@@ -177,7 +186,7 @@ export const build: BuildV2 = async ({
routesForHash.push(route); routesForHash.push(route);
} }
const serverBundles = Array.from(serverBundlesMap.entries()).map( serverBundles = Array.from(serverBundlesMap.entries()).map(
([hash, routes]) => { ([hash, routes]) => {
const runtime = resolvedConfigsMap.get(routes[0])?.runtime ?? 'nodejs'; const runtime = resolvedConfigsMap.get(routes[0])?.runtime ?? 'nodejs';
return { return {
@@ -189,10 +198,6 @@ export const build: BuildV2 = async ({
// We need to patch the `remix.config.js` file to force some values necessary // We need to patch the `remix.config.js` file to force some values necessary
// for a build that works on either Node.js or the Edge runtime // for a build that works on either Node.js or the Edge runtime
const remixConfigPath = findConfig(entrypointFsDirname, 'remix.config');
const renamedRemixConfigPath = remixConfigPath
? `${remixConfigPath}.original${extname(remixConfigPath)}`
: undefined;
if (remixConfigPath && renamedRemixConfigPath) { if (remixConfigPath && renamedRemixConfigPath) {
await fs.rename(remixConfigPath, renamedRemixConfigPath); await fs.rename(remixConfigPath, renamedRemixConfigPath);
@@ -227,10 +232,10 @@ config.serverBundles = ${JSON.stringify(serverBundles)};
module.exports = config;`; module.exports = config;`;
} }
await fs.writeFile(remixConfigPath, patchedConfig); await fs.writeFile(remixConfigPath, patchedConfig);
remixConfigWrapped = true;
} }
// Run "Build Command" // Run "Build Command"
try {
if (buildCommand) { if (buildCommand) {
debug(`Executing build command "${buildCommand}"`); debug(`Executing build command "${buildCommand}"`);
await execCommand(buildCommand, { await execCommand(buildCommand, {
@@ -260,7 +265,7 @@ module.exports = config;`;
} }
} finally { } finally {
// Clean up our patched `remix.config.js` to be polite // Clean up our patched `remix.config.js` to be polite
if (remixConfigPath && renamedRemixConfigPath) { if (remixConfigWrapped && remixConfigPath && renamedRemixConfigPath) {
await fs.rename(renamedRemixConfigPath, remixConfigPath); await fs.rename(renamedRemixConfigPath, remixConfigPath);
} }