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: No colors when using --watch #2886

Merged
merged 8 commits into from
Feb 9, 2024
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
8 changes: 8 additions & 0 deletions .chronus/changes/watch-color-2024-1-8-23-15-57.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix: `tsp compile --watch` was missing coloring and error previews
9 changes: 5 additions & 4 deletions packages/compiler/src/core/cli/actions/compile/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ async function compileOnce(
}

function compileWatch(
host: CompilerHost,
cliHost: CliCompilerHost,
path: string,
compilerOptions: CompilerOptions
): Promise<void> {
const watchHost: WatchHost = createWatchHost(cliHost);

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

Expand All @@ -88,7 +90,7 @@ function compileWatch(
console.clear();

watchHost?.forceJSReload();
currentCompilePromise = compileProgram(host, resolve(path), compilerOptions)
currentCompilePromise = compileProgram(watchHost, resolve(path), compilerOptions)
.then(onCompileFinished)
.catch(handleInternalCompilerError);
} else {
Expand All @@ -103,11 +105,10 @@ function compileWatch(
const watcher: ProjectWatcher = createWatcher((_name: string) => {
scheduleCompile();
});
const watchHost: WatchHost = (host = createWatchHost());

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

currentCompilePromise = undefined;
if (compileRequested) {
Expand Down
6 changes: 3 additions & 3 deletions packages/compiler/src/core/cli/actions/compile/watch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { FSWatcher, WatchEventType, watch } from "fs";
import { pathToFileURL } from "url";
import { NodeHost } from "../../../node-host.js";
import { CompilerHost } from "../../../types.js";
import { CliCompilerHost } from "../../types.js";

export interface ProjectWatcher {
/** Set the files to watch. */
Expand Down Expand Up @@ -54,10 +54,10 @@ export function createWatcher(
}
}

export function createWatchHost(): WatchHost {
export function createWatchHost(host: CliCompilerHost): WatchHost {
let count = 0;
return {
...NodeHost,
...host,
forceJSReload,
getJsImport: (path: string) => import(pathToFileURL(path).href + `?=${count}`),
};
Expand Down
Loading