From 64b0fd6a9d8822df8b83ed4c4a45f93fb0e72ac1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristian=20Pallar=C3=A9s?= Date: Mon, 15 Jul 2024 14:50:26 +0200 Subject: [PATCH] fix(console): preflight logs don't appear in the logs panel (#6908) Moves the preflight log entries (those emitted when compiling wing code) to the console logs panel. Previously, they were emitted through stdout to the terminal that launched the console. In order to achieve so, adds an optional `preflightLog` option to the `compile` function in the `@winglang/compiler` package. By default, preflight logs are emitted through `console.log`. Fixes #3698. --- apps/wing-console/console/app/demo/main.w | 2 ++ apps/wing-console/console/app/scripts/dev.mjs | 6 +++--- apps/wing-console/console/server/src/index.ts | 3 +++ .../console/server/src/utils/compiler.ts | 3 +++ .../ui/src/features/logs-pane/console-logs.tsx | 11 +++++++++-- libs/wingcompiler/src/compile.ts | 13 ++++++++++--- 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/apps/wing-console/console/app/demo/main.w b/apps/wing-console/console/app/demo/main.w index 0937d568c29..bde1ce69bfa 100644 --- a/apps/wing-console/console/app/demo/main.w +++ b/apps/wing-console/console/app/demo/main.w @@ -324,3 +324,5 @@ class ApiUsersService { } new ApiUsersService(); + +log("hello from inflight"); diff --git a/apps/wing-console/console/app/scripts/dev.mjs b/apps/wing-console/console/app/scripts/dev.mjs index 28c77a818f9..56c18828cdc 100644 --- a/apps/wing-console/console/app/scripts/dev.mjs +++ b/apps/wing-console/console/app/scripts/dev.mjs @@ -24,9 +24,9 @@ const options = parseArgs({ fileURLToPath(new URL("../demo/main.w", import.meta.url)), requestedPort: 1214, log: { - info: console.log, - error: console.error, - verbose: console.log, + info: () => {}, + error: (...arguments_) => console.error("[error]", ...arguments_), + verbose: () => {}, }, config: { addEventListener(event, listener) {}, diff --git a/apps/wing-console/console/server/src/index.ts b/apps/wing-console/console/server/src/index.ts index 2f3fbadeb4f..f3787650f6f 100644 --- a/apps/wing-console/console/server/src/index.ts +++ b/apps/wing-console/console/server/src/index.ts @@ -126,6 +126,9 @@ export const createConsoleServer = async ({ testing: false, stateDir, watchGlobs, + preflightLog(data) { + consoleLogger.log(data, "compiler"); + }, }); let isStarting = false; let isStopping = false; diff --git a/apps/wing-console/console/server/src/utils/compiler.ts b/apps/wing-console/console/server/src/utils/compiler.ts index f77018dc205..858e92205e8 100644 --- a/apps/wing-console/console/server/src/utils/compiler.ts +++ b/apps/wing-console/console/server/src/utils/compiler.ts @@ -29,6 +29,7 @@ export interface CreateCompilerProps { testing?: boolean; stateDir?: string; watchGlobs?: string[]; + preflightLog?: (message: string) => void; } export const createCompiler = ({ @@ -37,6 +38,7 @@ export const createCompiler = ({ testing = false, stateDir, watchGlobs, + preflightLog, }: CreateCompilerProps): Compiler => { const dirname = path.dirname(wingfile); const events = new Emittery(); @@ -58,6 +60,7 @@ export const createCompiler = ({ simfile = await wing.compile(wingfile, { platform, testing, + preflightLog, }); await events.emit("compiled", { simfile }); } catch (error) { diff --git a/apps/wing-console/console/ui/src/features/logs-pane/console-logs.tsx b/apps/wing-console/console/ui/src/features/logs-pane/console-logs.tsx index c101afbda81..6b4d3c8ec10 100644 --- a/apps/wing-console/console/ui/src/features/logs-pane/console-logs.tsx +++ b/apps/wing-console/console/ui/src/features/logs-pane/console-logs.tsx @@ -27,6 +27,7 @@ const shortDateTimeFormat = new Intl.DateTimeFormat(undefined, { minute: "2-digit", second: "2-digit", fractionalSecondDigits: 3, + hour12: false, }); const longDateTimeFormat = new Intl.DateTimeFormat(undefined, { @@ -37,6 +38,7 @@ const longDateTimeFormat = new Intl.DateTimeFormat(undefined, { minute: "2-digit", second: "2-digit", fractionalSecondDigits: 3, + hour12: false, }); interface LogEntryProps { @@ -127,7 +129,7 @@ const LogEntryRow = memo( {/*eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions*/}
{canBeExpanded && ( @@ -208,6 +210,11 @@ const LogEntryRow = memo( )} ref={expandableRef} > + {log.source === "compiler" && ( + + Compiler + + )} void; + readonly preflightLog?: (...args: any[]) => void; /// Enable/disable color output for the compiler (subject to terminal detection) readonly color?: boolean; @@ -133,6 +134,7 @@ export function determineTargetFromPlatforms(platforms: string[]): string { */ export async function compile(entrypoint: string, options: CompileOptions): Promise { const { log } = options; + const preflightLog = options.preflightLog ?? console.log; // create a unique temporary directory for the compilation const targetdir = options.targetDir ?? join(dirname(entrypoint), "target"); const entrypointFile = resolve(entrypoint); @@ -205,9 +207,11 @@ export async function compile(entrypoint: string, options: CompileOptions): Prom delete preflightEnv.Path; } } + await runPreflightCodeInWorkerThread( compileForPreflightResult.preflightEntrypoint, - preflightEnv + preflightEnv, + (data) => preflightLog?.(data.toString().trim()) ); } return synthDir; @@ -332,7 +336,8 @@ function defaultValuesFile() { async function runPreflightCodeInWorkerThread( entrypoint: string, - env: Record + env: Record, + onStdout: (data: Buffer) => void ): Promise { try { env.WING_PREFLIGHT_ENTRYPOINT = JSON.stringify(entrypoint); @@ -340,8 +345,10 @@ async function runPreflightCodeInWorkerThread( await new Promise((resolve, reject) => { const worker = fork(join(__dirname, "..", "preflight.shim.cjs"), { env, - stdio: "inherit", + stdio: "pipe", }); + worker.stdout?.on("data", onStdout); + worker.stderr?.on("data", onStdout); worker.on("message", reject); worker.on("error", reject); worker.on("exit", (code) => {