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): prevent rebuilds from hanging #6295

Merged
merged 2 commits into from
May 3, 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/twenty-toys-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/dev": patch
---

cancel previous build when rebuild is kicked off to prevent rebuilds from hanging
7 changes: 7 additions & 0 deletions packages/remix-dev/compiler/cancel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const CANCEL_PREFIX = "remix-compile-cancel";

export class Cancel extends Error {
constructor(message: string) {
super(`${CANCEL_PREFIX}: ${message}`);
}
}
3 changes: 2 additions & 1 deletion packages/remix-dev/compiler/js/plugins/cssBundleUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Plugin } from "esbuild";
import { readFile } from "fs-extra";

import type * as Channel from "../../../channel";
import { Cancel } from "../../cancel";

const pluginName = "css-bundle-update-plugin";
const namespace = `${pluginName}-ns`;
Expand Down Expand Up @@ -53,7 +54,7 @@ export function cssBundleUpdatePlugin(channels: {

build.onLoad({ filter: /.*/, namespace }, async (args) => {
let cssBundleHref = await channels.cssBundleHref.result;
if (!cssBundleHref.ok) throw Error("canceled");
if (!cssBundleHref.ok) throw new Cancel("js");
let contents = await readFile(args.path, "utf8");

if (cssBundleHref.value) {
Expand Down
3 changes: 2 additions & 1 deletion packages/remix-dev/compiler/server/plugins/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import jsesc from "jsesc";
import type * as Channel from "../../../channel";
import { type Manifest } from "../../../manifest";
import { assetsManifestVirtualModule } from "../virtualModules";
import { Cancel } from "../../cancel";

/**
* Creates a virtual module called `@remix-run/dev/assets-manifest` that exports
Expand All @@ -27,7 +28,7 @@ export function serverAssetsManifestPlugin(channels: {

build.onLoad({ filter }, async () => {
let manifest = await channels.manifest.result;
if (!manifest.ok) throw Error("canceled");
if (!manifest.ok) throw new Cancel("server");
return {
contents: `export default ${jsesc(manifest.value, {
es6: true,
Expand Down
14 changes: 10 additions & 4 deletions packages/remix-dev/compiler/utils/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import esbuild from "esbuild";

import { CANCEL_PREFIX } from "../cancel";

let toError = (thrown: unknown): Error => {
if (thrown instanceof Error) return thrown;
try {
Expand All @@ -21,10 +23,14 @@ let logEsbuildError = (error: esbuild.BuildFailure) => {
color: true,
});
warnings.forEach((w) => console.warn(w));
let errors = esbuild.formatMessagesSync(error.errors, {
kind: "error",
color: true,
});
let errors = esbuild.formatMessagesSync(
// Filter out cancelation errors
error.errors.filter((e) => !e.text.startsWith(CANCEL_PREFIX)),
{
kind: "error",
color: true,
}
);
errors.forEach((e) => console.error(e));
};

Expand Down
1 change: 1 addition & 0 deletions packages/remix-dev/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export async function watch(
}, 500);

let rebuild = debounce(async () => {
await compiler.cancel();
onBuildStart?.(ctx);
let start = Date.now();
let manifest = await compile();
Expand Down