From 54fbeb511d1e43b62a6d4975f27e8ff63ae389c4 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 26 May 2021 15:31:26 -0700 Subject: [PATCH] Use correct state when getting it from redirected program (#44275) Eg. program can backup and restore state changing the state object and we want to release program on the correct one This ensure program is released correctly when there are declaration emit errors during tsc --build --- src/compiler/builder.ts | 15 ++++++++------- src/compiler/builderPublic.ts | 2 +- .../when-file-with-no-error-changes.js | 2 +- ...-fixing-errors-only-changed-file-is-emitted.js | 2 +- .../when-file-with-no-error-changes.js | 2 +- ...en-fixing-error-files-all-files-are-emitted.js | 2 +- 6 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/compiler/builder.ts b/src/compiler/builder.ts index 1b6bdf5eecb..68d3796fa34 100644 --- a/src/compiler/builder.ts +++ b/src/compiler/builder.ts @@ -976,8 +976,9 @@ namespace ts { oldProgram = undefined; oldState = undefined; - const builderProgram = createRedirectedBuilderProgram(state, configFileParsingDiagnostics); - builderProgram.getState = () => state; + const getState = () => state; + const builderProgram = createRedirectedBuilderProgram(getState, configFileParsingDiagnostics); + builderProgram.getState = getState; builderProgram.backupState = () => { Debug.assert(backupState === undefined); backupState = cloneBuilderProgramState(state); @@ -1306,15 +1307,15 @@ namespace ts { } } - export function createRedirectedBuilderProgram(state: { program: Program | undefined; compilerOptions: CompilerOptions; }, configFileParsingDiagnostics: readonly Diagnostic[]): BuilderProgram { + export function createRedirectedBuilderProgram(getState: () => { program: Program | undefined; compilerOptions: CompilerOptions; }, configFileParsingDiagnostics: readonly Diagnostic[]): BuilderProgram { return { getState: notImplemented, backupState: noop, restoreState: noop, getProgram, - getProgramOrUndefined: () => state.program, - releaseProgram: () => state.program = undefined, - getCompilerOptions: () => state.compilerOptions, + getProgramOrUndefined: () => getState().program, + releaseProgram: () => getState().program = undefined, + getCompilerOptions: () => getState().compilerOptions, getSourceFile: fileName => getProgram().getSourceFile(fileName), getSourceFiles: () => getProgram().getSourceFiles(), getOptionsDiagnostics: cancellationToken => getProgram().getOptionsDiagnostics(cancellationToken), @@ -1331,7 +1332,7 @@ namespace ts { }; function getProgram() { - return Debug.checkDefined(state.program); + return Debug.checkDefined(getState().program); } } } diff --git a/src/compiler/builderPublic.ts b/src/compiler/builderPublic.ts index c415ba02f5b..5d5d2ed25dd 100644 --- a/src/compiler/builderPublic.ts +++ b/src/compiler/builderPublic.ts @@ -164,6 +164,6 @@ namespace ts { export function createAbstractBuilder(rootNames: readonly string[] | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): BuilderProgram; export function createAbstractBuilder(newProgramOrRootNames: Program | readonly string[] | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | BuilderProgram, configFileParsingDiagnosticsOrOldProgram?: readonly Diagnostic[] | BuilderProgram, configFileParsingDiagnostics?: readonly Diagnostic[], projectReferences?: readonly ProjectReference[]): BuilderProgram { const { newProgram, configFileParsingDiagnostics: newConfigFileParsingDiagnostics } = getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences); - return createRedirectedBuilderProgram({ program: newProgram, compilerOptions: newProgram.getCompilerOptions() }, newConfigFileParsingDiagnostics); + return createRedirectedBuilderProgram(() => ({ program: newProgram, compilerOptions: newProgram.getCompilerOptions() }), newConfigFileParsingDiagnostics); } } diff --git a/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/introduceError/when-file-with-no-error-changes.js b/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/introduceError/when-file-with-no-error-changes.js index a3aa3a83ddd..e23630d1287 100644 --- a/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/introduceError/when-file-with-no-error-changes.js +++ b/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/introduceError/when-file-with-no-error-changes.js @@ -215,7 +215,7 @@ Output:: Program root files: ["/user/username/projects/solution/app/fileWithError.ts","/user/username/projects/solution/app/fileWithoutError.ts"] Program options: {"composite":true,"watch":true,"configFilePath":"/user/username/projects/solution/app/tsconfig.json"} -Program structureReused: Completely +Program structureReused: Not Program files:: /a/lib/lib.d.ts /user/username/projects/solution/app/fileWithError.ts diff --git a/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/introduceError/when-fixing-errors-only-changed-file-is-emitted.js b/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/introduceError/when-fixing-errors-only-changed-file-is-emitted.js index 897bc2b2b4e..eb1054c9de2 100644 --- a/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/introduceError/when-fixing-errors-only-changed-file-is-emitted.js +++ b/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/introduceError/when-fixing-errors-only-changed-file-is-emitted.js @@ -213,7 +213,7 @@ Output:: Program root files: ["/user/username/projects/solution/app/fileWithError.ts","/user/username/projects/solution/app/fileWithoutError.ts"] Program options: {"composite":true,"watch":true,"configFilePath":"/user/username/projects/solution/app/tsconfig.json"} -Program structureReused: Completely +Program structureReused: Not Program files:: /a/lib/lib.d.ts /user/username/projects/solution/app/fileWithError.ts diff --git a/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/when-file-with-no-error-changes.js b/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/when-file-with-no-error-changes.js index e6440cf9959..fd26df3a120 100644 --- a/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/when-file-with-no-error-changes.js +++ b/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/when-file-with-no-error-changes.js @@ -91,7 +91,7 @@ Output:: Program root files: ["/user/username/projects/solution/app/fileWithError.ts","/user/username/projects/solution/app/fileWithoutError.ts"] Program options: {"composite":true,"watch":true,"configFilePath":"/user/username/projects/solution/app/tsconfig.json"} -Program structureReused: Completely +Program structureReused: Not Program files:: /a/lib/lib.d.ts /user/username/projects/solution/app/fileWithError.ts diff --git a/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/when-fixing-error-files-all-files-are-emitted.js b/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/when-fixing-error-files-all-files-are-emitted.js index 37d6eed6beb..c22ec678f14 100644 --- a/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/when-fixing-error-files-all-files-are-emitted.js +++ b/tests/baselines/reference/tsbuild/watchMode/programUpdates/reportErrors/declarationEmitErrors/when-fixing-error-files-all-files-are-emitted.js @@ -89,7 +89,7 @@ Output:: Program root files: ["/user/username/projects/solution/app/fileWithError.ts","/user/username/projects/solution/app/fileWithoutError.ts"] Program options: {"composite":true,"watch":true,"configFilePath":"/user/username/projects/solution/app/tsconfig.json"} -Program structureReused: Completely +Program structureReused: Not Program files:: /a/lib/lib.d.ts /user/username/projects/solution/app/fileWithError.ts