From 8192d550496d884263e292488e325ae96893dc78 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Tue, 27 Sep 2022 21:34:39 -0700 Subject: [PATCH] Pick correct compilerOptions when checking if we can share emitSignatures (#50910) * Pick correct compilerOptions when checking if we can share emitSignatures Fixes #50902 * Add a note * Rewording --- src/compiler/builder.ts | 8 +- src/testRunner/unittests/tsc/composite.ts | 21 +++ .../tsc/composite/converting-to-modules.js | 126 ++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/tsc/composite/converting-to-modules.js diff --git a/src/compiler/builder.ts b/src/compiler/builder.ts index 6177713582859..84578817961e1 100644 --- a/src/compiler/builder.ts +++ b/src/compiler/builder.ts @@ -173,10 +173,16 @@ namespace ts { const oldCompilerOptions = useOldState ? oldState!.compilerOptions : undefined; const canCopySemanticDiagnostics = useOldState && oldState!.semanticDiagnosticsPerFile && !!state.semanticDiagnosticsPerFile && !compilerOptionsAffectSemanticDiagnostics(compilerOptions, oldCompilerOptions!); + // We can only reuse emit signatures (i.e. .d.ts signatures) if the .d.ts file is unchanged, + // which will eg be depedent on change in options like declarationDir and outDir options are unchanged. + // We need to look in oldState.compilerOptions, rather than oldCompilerOptions (i.e.we need to disregard useOldState) because + // oldCompilerOptions can be undefined if there was change in say module from None to some other option + // which would make useOldState as false since we can now use reference maps that are needed to track what to emit, what to check etc + // but that option change does not affect d.ts file name so emitSignatures should still be reused. const canCopyEmitSignatures = compilerOptions.composite && oldState?.emitSignatures && !outFilePath && - !compilerOptionsAffectDeclarationPath(compilerOptions, oldCompilerOptions!); + !compilerOptionsAffectDeclarationPath(compilerOptions, oldState.compilerOptions); if (useOldState) { // Copy old state's changed files set oldState!.changedFilesSet?.forEach(value => state.changedFilesSet.add(value)); diff --git a/src/testRunner/unittests/tsc/composite.ts b/src/testRunner/unittests/tsc/composite.ts index 8eb0ea3bd8595..48a5464d59e6a 100644 --- a/src/testRunner/unittests/tsc/composite.ts +++ b/src/testRunner/unittests/tsc/composite.ts @@ -81,5 +81,26 @@ namespace ts { }), commandLineArgs: ["--composite", "false", "--p", "src/project", "--tsBuildInfoFile", "null"], }); + + verifyTscWithEdits({ + scenario: "composite", + subScenario: "converting to modules", + fs: () => loadProjectFromFiles({ + "/src/project/src/main.ts": "const x = 10;", + "/src/project/tsconfig.json": JSON.stringify({ + compilerOptions: { + module: "none", + composite: true, + }, + }), + }), + commandLineArgs: ["-p", "/src/project"], + edits: [ + { + subScenario: "convert to modules", + modifyFs: fs => replaceText(fs, "/src/project/tsconfig.json", "none", "es2015"), + } + ] + }); }); } diff --git a/tests/baselines/reference/tsc/composite/converting-to-modules.js b/tests/baselines/reference/tsc/composite/converting-to-modules.js new file mode 100644 index 0000000000000..eb49c94eb3d64 --- /dev/null +++ b/tests/baselines/reference/tsc/composite/converting-to-modules.js @@ -0,0 +1,126 @@ +Input:: +//// [/lib/lib.d.ts] +/// +interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number { toExponential: any; } +interface Object {} +interface RegExp {} +interface String { charAt: any; } +interface Array { length: number; [n: number]: T; } +interface ReadonlyArray {} +declare const console: { log(msg: any): void; }; + +//// [/src/project/src/main.ts] +const x = 10; + +//// [/src/project/tsconfig.json] +{"compilerOptions":{"module":"none","composite":true}} + + + +Output:: +/lib/tsc -p /src/project +exitCode:: ExitStatus.Success + + +//// [/src/project/src/main.d.ts] +declare const x = 10; + + +//// [/src/project/src/main.js] +var x = 10; + + +//// [/src/project/tsconfig.tsbuildinfo] +{"program":{"fileNames":["../../lib/lib.d.ts","./src/main.ts"],"fileInfos":[{"version":"3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };","affectsGlobalScope":true},{"version":"5029505981-const x = 10;","signature":"-3198459068-declare const x = 10;\r\n","affectsGlobalScope":true}],"options":{"composite":true,"module":0},"semanticDiagnosticsPerFile":[1,2],"latestChangedDtsFile":"./src/main.d.ts"},"version":"FakeTSVersion"} + +//// [/src/project/tsconfig.tsbuildinfo.readable.baseline.txt] +{ + "program": { + "fileNames": [ + "../../lib/lib.d.ts", + "./src/main.ts" + ], + "fileInfos": { + "../../lib/lib.d.ts": { + "version": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "signature": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "affectsGlobalScope": true + }, + "./src/main.ts": { + "version": "5029505981-const x = 10;", + "signature": "-3198459068-declare const x = 10;\r\n", + "affectsGlobalScope": true + } + }, + "options": { + "composite": true, + "module": 0 + }, + "semanticDiagnosticsPerFile": [ + "../../lib/lib.d.ts", + "./src/main.ts" + ], + "latestChangedDtsFile": "./src/main.d.ts" + }, + "version": "FakeTSVersion", + "size": 816 +} + + + +Change:: convert to modules +Input:: +//// [/src/project/tsconfig.json] +{"compilerOptions":{"module":"es2015","composite":true}} + + + +Output:: +/lib/tsc -p /src/project +exitCode:: ExitStatus.Success + + +//// [/src/project/src/main.js] file written with same contents +//// [/src/project/tsconfig.tsbuildinfo] +{"program":{"fileNames":["../../lib/lib.d.ts","./src/main.ts"],"fileInfos":[{"version":"3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };","affectsGlobalScope":true},{"version":"5029505981-const x = 10;","signature":"-3198459068-declare const x = 10;\r\n","affectsGlobalScope":true}],"options":{"composite":true,"module":5},"referencedMap":[],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,2],"latestChangedDtsFile":"./src/main.d.ts"},"version":"FakeTSVersion"} + +//// [/src/project/tsconfig.tsbuildinfo.readable.baseline.txt] +{ + "program": { + "fileNames": [ + "../../lib/lib.d.ts", + "./src/main.ts" + ], + "fileInfos": { + "../../lib/lib.d.ts": { + "version": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "signature": "3858781397-/// \ninterface Boolean {}\ninterface Function {}\ninterface CallableFunction {}\ninterface NewableFunction {}\ninterface IArguments {}\ninterface Number { toExponential: any; }\ninterface Object {}\ninterface RegExp {}\ninterface String { charAt: any; }\ninterface Array { length: number; [n: number]: T; }\ninterface ReadonlyArray {}\ndeclare const console: { log(msg: any): void; };", + "affectsGlobalScope": true + }, + "./src/main.ts": { + "version": "5029505981-const x = 10;", + "signature": "-3198459068-declare const x = 10;\r\n", + "affectsGlobalScope": true + } + }, + "options": { + "composite": true, + "module": 5 + }, + "referencedMap": {}, + "exportedModulesMap": {}, + "semanticDiagnosticsPerFile": [ + "../../lib/lib.d.ts", + "./src/main.ts" + ], + "latestChangedDtsFile": "./src/main.d.ts" + }, + "version": "FakeTSVersion", + "size": 859 +} +