Skip to content

Commit

Permalink
fix(console): preflight logs don't appear in the logs panel (#6908)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
skyrpex authored Jul 15, 2024
1 parent 2664089 commit 64b0fd6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
2 changes: 2 additions & 0 deletions apps/wing-console/console/app/demo/main.w
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,5 @@ class ApiUsersService {
}

new ApiUsersService();

log("hello from inflight");
6 changes: 3 additions & 3 deletions apps/wing-console/console/app/scripts/dev.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {},
Expand Down
3 changes: 3 additions & 0 deletions apps/wing-console/console/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ export const createConsoleServer = async ({
testing: false,
stateDir,
watchGlobs,
preflightLog(data) {
consoleLogger.log(data, "compiler");
},
});
let isStarting = false;
let isStopping = false;
Expand Down
3 changes: 3 additions & 0 deletions apps/wing-console/console/server/src/utils/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface CreateCompilerProps {
testing?: boolean;
stateDir?: string;
watchGlobs?: string[];
preflightLog?: (message: string) => void;
}

export const createCompiler = ({
Expand All @@ -37,6 +38,7 @@ export const createCompiler = ({
testing = false,
stateDir,
watchGlobs,
preflightLog,
}: CreateCompilerProps): Compiler => {
const dirname = path.dirname(wingfile);
const events = new Emittery<CompilerEvents>();
Expand All @@ -58,6 +60,7 @@ export const createCompiler = ({
simfile = await wing.compile(wingfile, {
platform,
testing,
preflightLog,
});
await events.emit("compiled", { simfile });
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -37,6 +38,7 @@ const longDateTimeFormat = new Intl.DateTimeFormat(undefined, {
minute: "2-digit",
second: "2-digit",
fractionalSecondDigits: 3,
hour12: false,
});

interface LogEntryProps {
Expand Down Expand Up @@ -127,7 +129,7 @@ const LogEntryRow = memo(
{/*eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions*/}
<div
className={classNames(
"group w-full flex",
"group w-full",
"flex min-w-0",
"justify-between",
theme.border4,
Expand Down Expand Up @@ -173,7 +175,7 @@ const LogEntryRow = memo(
<div
className={classNames("min-w-0 text-left grow", {
truncate: !expanded,
"ml-2": log.timestamp && !log.ctx?.hideTimestamp,
"ml-4": log.timestamp && !log.ctx?.hideTimestamp,
})}
>
{canBeExpanded && (
Expand Down Expand Up @@ -208,6 +210,11 @@ const LogEntryRow = memo(
)}
ref={expandableRef}
>
{log.source === "compiler" && (
<span className="bg-slate-450 text-white dark:bg-slate-550 rounded inline px-1 select-none pointer-events-none mr-2">
Compiler
</span>
)}
<Linkify
options={{
className: "text-sky-500 underline hover:text-sky-800",
Expand Down
13 changes: 10 additions & 3 deletions libs/wingcompiler/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export interface CompileOptions {
*/
readonly testing?: boolean;
readonly log?: (...args: any[]) => void;
readonly preflightLog?: (...args: any[]) => void;

/// Enable/disable color output for the compiler (subject to terminal detection)
readonly color?: boolean;
Expand Down Expand Up @@ -133,6 +134,7 @@ export function determineTargetFromPlatforms(platforms: string[]): string {
*/
export async function compile(entrypoint: string, options: CompileOptions): Promise<string> {
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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -332,16 +336,19 @@ function defaultValuesFile() {

async function runPreflightCodeInWorkerThread(
entrypoint: string,
env: Record<string, string | undefined>
env: Record<string, string | undefined>,
onStdout: (data: Buffer) => void
): Promise<void> {
try {
env.WING_PREFLIGHT_ENTRYPOINT = JSON.stringify(entrypoint);

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) => {
Expand Down

0 comments on commit 64b0fd6

Please sign in to comment.