Skip to content

Commit

Permalink
Keep watch going when crash happens
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin committed Jul 25, 2024
1 parent 2fdb218 commit fed1a64
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
24 changes: 16 additions & 8 deletions packages/compiler/src/core/cli/actions/compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { resolvePath } from "../../../path-utils.js";
import { Program, compile as compileProgram } from "../../../program.js";
import { CompilerHost, Diagnostic } from "../../../types.js";
import { CliCompilerHost } from "../../types.js";
import { handleInternalCompilerError, logDiagnosticCount } from "../../utils.js";
import {
handleInternalCompilerError,
logDiagnosticCount,
logInternalCompilerError,
} from "../../utils.js";
import { CompileCliArgs, getCompilerOptions } from "./args.js";
import { ProjectWatcher, WatchHost, createWatchHost, createWatcher } from "./watch.js";

Expand Down Expand Up @@ -77,10 +81,11 @@ function compileWatch(
path: string,
compilerOptions: CompilerOptions
): Promise<void> {
const entrypoint = resolve(path);
const watchHost: WatchHost = createWatchHost(cliHost);

let compileRequested: boolean = false;
let currentCompilePromise: Promise<Program> | undefined = undefined;
let currentCompilePromise: Promise<Program | void> | undefined = undefined;

const runCompilePromise = () => {
// Don't run the compiler if it's already running
Expand All @@ -90,9 +95,9 @@ function compileWatch(
console.clear();

watchHost?.forceJSReload();
currentCompilePromise = compileProgram(watchHost, resolve(path), compilerOptions)
.then(onCompileFinished)
.catch(handleInternalCompilerError);
currentCompilePromise = compileProgram(watchHost, entrypoint, compilerOptions)
.catch(logInternalCompilerError)
.then(onCompileFinished);
} else {
compileRequested = true;
}
Expand All @@ -105,10 +110,13 @@ function compileWatch(
const watcher: ProjectWatcher = createWatcher((_name: string) => {
scheduleCompile();
});
watcher?.updateWatchedFiles([entrypoint]);

const onCompileFinished = (program: Program) => {
watcher?.updateWatchedFiles([...program.sourceFiles.keys(), ...program.jsSourceFiles.keys()]);
logProgramResult(watchHost, program, { showTimestamp: true });
const onCompileFinished = (program?: Program | void) => {
if (program !== undefined) {
watcher?.updateWatchedFiles([...program.sourceFiles.keys(), ...program.jsSourceFiles.keys()]);
logProgramResult(watchHost, program, { showTimestamp: true });
}

currentCompilePromise = undefined;
if (compileRequested) {
Expand Down
26 changes: 14 additions & 12 deletions packages/compiler/src/core/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,7 @@ export function logDiagnosticCount(diagnostics: readonly Diagnostic[]) {
console.log(`\nFound ${[errorText, warningText].filter((x) => x !== undefined).join(", ")}.`);
}

/**
* Handle an internal compiler error.
*
* NOTE: An expected error, like one thrown for bad input, shouldn't reach
* here, but be handled somewhere else. If we reach here, it should be
* considered a bug and therefore we should not suppress the stack trace as
* that risks losing it in the case of a bug that does not repro easily.
*
* @param error error thrown
*/
export function handleInternalCompilerError(error: unknown): never {
export function logInternalCompilerError(error: unknown) {
/* eslint-disable no-console */
if (error instanceof ExternalError) {
// ExternalError should already have all the relevant information needed when thrown.
Expand All @@ -164,7 +154,19 @@ export function handleInternalCompilerError(error: unknown): never {
console.error(error);
}
/* eslint-enable no-console */

}
/**
* Handle an internal compiler error.
*
* NOTE: An expected error, like one thrown for bad input, shouldn't reach
* here, but be handled somewhere else. If we reach here, it should be
* considered a bug and therefore we should not suppress the stack trace as
* that risks losing it in the case of a bug that does not repro easily.
*
* @param error error thrown
*/
export function handleInternalCompilerError(error: unknown) {
logInternalCompilerError(error);
process.exit(1);
}

Expand Down

0 comments on commit fed1a64

Please sign in to comment.