Skip to content
Open
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
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 `--list-files` not working when multiple instance of compiler are loaded
18 changes: 13 additions & 5 deletions packages/compiler/src/core/emitter-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ export interface EmitFileOptions {
newLine?: NewLine;
}

const emittedFilesPaths: string[] = [];
export function flushEmittedFilesPaths(): string[] {
return emittedFilesPaths.splice(0, emittedFilesPaths.length);
const emittedFilesPerProgramKey = Symbol.for("TYPESPEC_EMITTED_FILES_PATHS");
if ((globalThis as any)[emittedFilesPerProgramKey] === undefined) {
(globalThis as any)[emittedFilesPerProgramKey] = new WeakMap<Program, string[]>();
}

export function getEmittedFilesForProgram(program: Program): string[] {
const existing = (globalThis as any)[emittedFilesPerProgramKey].get(program);
if (existing) return existing;
const val: string[] = [];
(globalThis as any)[emittedFilesPerProgramKey].set(program, val);
return val;
}

/**
* Helper to emit a file.
* @param program TypeSpec Program
Expand All @@ -25,8 +34,7 @@ export async function emitFile(program: Program, options: EmitFileOptions): Prom
options.newLine && options.newLine === "crlf"
? options.content.replace(/(\r\n|\n|\r)/gm, "\r\n")
: options.content;

emittedFilesPaths.push(options.path);
getEmittedFilesForProgram(program).push(options.path);

return await program.host.writeFile(options.path, content);
}
10 changes: 5 additions & 5 deletions packages/compiler/src/core/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { createBinder } from "./binder.js";
import { Checker, createChecker } from "./checker.js";
import { createSuppressCodeFix } from "./compiler-code-fixes/suppress.codefix.js";
import { compilerAssert } from "./diagnostics.js";
import { flushEmittedFilesPaths } from "./emitter-utils.js";
import { getEmittedFilesForProgram } from "./emitter-utils.js";
import { resolveTypeSpecEntrypoint } from "./entrypoint-resolution.js";
import { ExternalError } from "./external-error.js";
import { getLibraryUrlsLoaded } from "./library.js";
Expand Down Expand Up @@ -196,7 +196,7 @@ export async function compile(
const { duration } = await emit(emitter, program);
emitStats.emitters[emitter.metadata.name ?? "<unnamed>"] = duration;
if (options.listFiles) {
logEmittedFilesPath(host.logSink);
logEmittedFilesPath(program);
}
}
emitStats.total = timer.end();
Expand Down Expand Up @@ -1014,10 +1014,10 @@ async function runEmitter(emitter: EmitterRef, program: Program) {
}
}

function logEmittedFilesPath(logSink: LogSink) {
flushEmittedFilesPaths().forEach((filePath) => {
function logEmittedFilesPath(program: Program) {
getEmittedFilesForProgram(program).forEach((filePath) => {
// eslint-disable-next-line no-console
console.log(` ${pc.dim(transformPathForSink(logSink, filePath))}`);
console.log(` ${pc.dim(transformPathForSink(program.host.logSink, filePath))}`);
});
}
function transformPathForSink(logSink: LogSink, path: string) {
Expand Down
Loading