diff --git a/.chronus/changes/fix-list-files-across-instances-2026-0-9-17-57-50.md b/.chronus/changes/fix-list-files-across-instances-2026-0-9-17-57-50.md new file mode 100644 index 00000000000..5a6c705d35f --- /dev/null +++ b/.chronus/changes/fix-list-files-across-instances-2026-0-9-17-57-50.md @@ -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 diff --git a/packages/compiler/src/core/emitter-utils.ts b/packages/compiler/src/core/emitter-utils.ts index 41e26d3a774..3f9fbfd5882 100644 --- a/packages/compiler/src/core/emitter-utils.ts +++ b/packages/compiler/src/core/emitter-utils.ts @@ -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(); } + +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 @@ -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); } diff --git a/packages/compiler/src/core/program.ts b/packages/compiler/src/core/program.ts index d5038ab0343..b9548c358f6 100644 --- a/packages/compiler/src/core/program.ts +++ b/packages/compiler/src/core/program.ts @@ -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"; @@ -196,7 +196,7 @@ export async function compile( const { duration } = await emit(emitter, program); emitStats.emitters[emitter.metadata.name ?? ""] = duration; if (options.listFiles) { - logEmittedFilesPath(host.logSink); + logEmittedFilesPath(program); } } emitStats.total = timer.end(); @@ -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) {