[remix] cd to the project directory during readConfig() (#9518)

Sometimes the `cwd` needs to be properly set for some Remix configurations. This is problematic in monorepos because the default `cwd` is the root of the monorepo, and not the project directory.

So `cd` to the project directory when reading the Remix config file to account for that.

Fixes https://github.com/kiliman/remix-flat-routes/issues/42.
Fixes https://github.com/vercel/vercel/discussions/9478.
This commit is contained in:
Nathan Rajlich
2023-02-23 11:18:40 -08:00
committed by GitHub
parent 73d6f0d0fa
commit 231714c71b

View File

@@ -28,6 +28,7 @@ import type {
PackageJson,
BuildResultV2Typical,
} from '@vercel/build-utils';
import type { RemixConfig } from '@remix-run/dev/dist/config';
import type { ConfigRoute } from '@remix-run/dev/dist/config/routes';
import {
findConfig,
@@ -94,7 +95,7 @@ export const build: BuildV2 = async ({
// Make `remix build` output production mode
spawnOpts.env.NODE_ENV = 'production';
let remixConfig = await readConfig(entrypointFsDirname);
let remixConfig = await chdirAndReadConfig(entrypointFsDirname);
const { serverEntryPoint } = remixConfig;
// We need to patch the `remix.config.js` file to force some values necessary
@@ -166,7 +167,7 @@ module.exports = config;`;
});
}
}
remixConfig = await readConfig(entrypointFsDirname);
remixConfig = await chdirAndReadConfig(entrypointFsDirname);
} finally {
// Clean up our patched `remix.config.js` to be polite
if (remixConfigPath && renamedRemixConfigPath) {
@@ -482,3 +483,15 @@ async function ensureResolvable(start: string, base: string, pkgName: string) {
function isEdgeRuntime(runtime: string): boolean {
return runtime === 'edge' || runtime === 'experimental-edge';
}
async function chdirAndReadConfig(dir: string) {
const originalCwd = process.cwd();
let remixConfig: RemixConfig;
try {
process.chdir(dir);
remixConfig = await readConfig(dir);
} finally {
process.chdir(originalCwd);
}
return remixConfig;
}