Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dev): better error message when remix-serve is not found #6477

Merged
merged 1 commit into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rude-geese-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

better error message when `remix-serve` is not found
22 changes: 1 addition & 21 deletions packages/remix-dev/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { TaskError } from "../codemod/utils/task";
import { transpile as convertFileToJS } from "./useJavascript";
import { warnOnce } from "../warnOnce";
import type { Options } from "../compiler/options";
import { getAppDependencies } from "../dependencies";

export async function create({
appTemplate,
Expand Down Expand Up @@ -502,7 +501,7 @@ let resolveDevOrigin = async (
};

type DevServeFlags = DevOrigin & {
command: string;
command?: string;
restart: boolean;
};
let resolveDevServe = async (
Expand All @@ -518,25 +517,6 @@ let resolveDevServe = async (
let command =
flags.command ??
(dev === true ? undefined : dev.command)
if (!command) {
command = `remix-serve ${path.relative(
process.cwd(),
config.serverBuildPath
)}`;

let usingRemixAppServer =
getAppDependencies(config, true)["@remix-run/serve"] !== undefined;
if (!usingRemixAppServer) {
console.error(
[
`Remix dev server command defaulted to '${command}', but @remix-run/serve is not installed.`,
"If you are using another server, specify how to run it with `-c` or `--command` flag.",
"For example, `remix dev -c 'node ./server.js'`",
].join("\n")
);
process.exit(1);
}
}

let restart =
flags.restart ?? (dev === true ? undefined : dev.restart) ?? true;
Expand Down
62 changes: 44 additions & 18 deletions packages/remix-dev/devServer_unstable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { detectPackageManager } from "../cli/detectPackageManager";
import * as HDR from "./hdr";
import type { Result } from "../result";
import { err, ok } from "../result";
import invariant from "../invariant";

type Origin = {
scheme: string;
Expand All @@ -41,7 +42,7 @@ let detectBin = async (): Promise<string> => {
export let serve = async (
initialConfig: RemixConfig,
options: {
command: string;
command?: string;
scheme: string;
host: string;
port: number;
Expand Down Expand Up @@ -83,19 +84,47 @@ export let serve = async (
};

let bin = await detectBin();
let startAppServer = (command: string) => {
console.log(`> ${command}`);
let newAppServer = execa.command(command, {
stdio: "pipe",
env: {
NODE_ENV: "development",
PATH:
bin + (process.platform === "win32" ? ";" : ":") + process.env.PATH,
REMIX_DEV_HTTP_ORIGIN: stringifyOrigin(origin),
},
// https://github.com/sindresorhus/execa/issues/433
windowsHide: false,
});
let startAppServer = (command?: string) => {
let cmd =
command ??
`remix-serve ${path.relative(
process.cwd(),
initialConfig.serverBuildPath
)}`;
console.log(`> ${cmd}`);
let newAppServer = execa
.command(cmd, {
stdio: "pipe",
env: {
NODE_ENV: "development",
PATH:
bin + (process.platform === "win32" ? ";" : ":") + process.env.PATH,
REMIX_DEV_HTTP_ORIGIN: stringifyOrigin(origin),
},
// https://github.com/sindresorhus/execa/issues/433
windowsHide: false,
})
.on("error", (e) => {
// patch execa error types
invariant("errno" in e && typeof e.errno === "number", "errno missing");
invariant("code" in e && typeof e.code === "string", "code missing");
invariant("path" in e && typeof e.path === "string", "path missing");

if (command === undefined) {
console.error(
[
"",
`┏ [error] command not found: ${e.path}`,
`┃ \`remix dev\` did not receive \`--command\` nor \`-c\`, defaulting to \`${cmd}\`.`,
"┃ You probably meant to use `-c` for your app server command.",
"┗ For example: `remix dev -c 'node ./server.js'`",
"",
].join("\n")
);
process.exit(1);
}
throw e;
});

if (newAppServer.stdin)
process.stdin.pipe(newAppServer.stdin, { end: true });
Expand Down Expand Up @@ -164,10 +193,7 @@ export let serve = async (
try {
console.log(`Waiting for app server (${state.manifest?.version})`);
let start = Date.now();
if (
options.command &&
(state.appServer === undefined || options.restart)
) {
if (state.appServer === undefined || options.restart) {
await kill(state.appServer);
state.appServer = startAppServer(options.command);
}
Expand Down