From f654f18d861f96097515c849044df637a328419e Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 5 Apr 2022 13:00:37 -0400 Subject: [PATCH] fix: ensure ts source file with no-default-lib that augments the global scope gets emitted with incremental program (#48412) * fix: do not classify non-declaration files as default library source files * Add test. --- src/compiler/program.ts | 4 + src/testRunner/unittests/tsc/incremental.ts | 31 +++++++ ...ault-lib-that-augments-the-global-scope.js | 93 +++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 tests/baselines/reference/tsc/incremental/initial-build/ts-file-with-no-default-lib-that-augments-the-global-scope.js diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 2c9f07fe0bb..021c34ce3a7 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1958,6 +1958,10 @@ namespace ts { } function isSourceFileDefaultLibrary(file: SourceFile): boolean { + if (!file.isDeclarationFile) { + return false; + } + if (file.hasNoDefaultLib) { return true; } diff --git a/src/testRunner/unittests/tsc/incremental.ts b/src/testRunner/unittests/tsc/incremental.ts index 13a644a6208..2085038fd80 100644 --- a/src/testRunner/unittests/tsc/incremental.ts +++ b/src/testRunner/unittests/tsc/incremental.ts @@ -449,5 +449,36 @@ declare global { }, `\ninterface ReadonlyArray { readonly length: number }`), incrementalScenarios: noChangeOnlyRuns, }); + + verifyTsc({ + scenario: "incremental", + subScenario: "ts file with no-default-lib that augments the global scope", + fs: () => loadProjectFromFiles({ + "/src/project/src/main.ts": Utils.dedent` + /// + /// + + declare global { + interface Test { + } + } + + export {}; + `, + "/src/project/tsconfig.json": Utils.dedent` + { + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "incremental": true, + "outDir": "dist", + }, + }`, + }), + commandLineArgs: ["--p", "src/project", "--rootDir", "src/project/src"], + modifyFs: (fs) => { + fs.writeFileSync("/lib/lib.esnext.d.ts", libContent); + } + }); }); } diff --git a/tests/baselines/reference/tsc/incremental/initial-build/ts-file-with-no-default-lib-that-augments-the-global-scope.js b/tests/baselines/reference/tsc/incremental/initial-build/ts-file-with-no-default-lib-that-augments-the-global-scope.js new file mode 100644 index 00000000000..0ae751e9f56 --- /dev/null +++ b/tests/baselines/reference/tsc/incremental/initial-build/ts-file-with-no-default-lib-that-augments-the-global-scope.js @@ -0,0 +1,93 @@ +Input:: +//// [/lib/lib.d.ts] + + +//// [/lib/lib.esnext.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] +/// +/// + +declare global { + interface Test { + } +} + +export {}; + + +//// [/src/project/tsconfig.json] +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "incremental": true, + "outDir": "dist", + }, +} + + + +Output:: +/lib/tsc --p src/project --rootDir src/project/src +exitCode:: ExitStatus.Success + + +//// [/src/project/dist/main.js] +/// +/// +export {}; + + +//// [/src/project/tsconfig.tsbuildinfo] +{"program":{"fileNames":["../../lib/lib.esnext.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":"-2443389309-/// \n/// \n\ndeclare global {\n interface Test {\n }\n}\n\nexport {};\n","affectsGlobalScope":true}],"options":{"module":99,"outDir":"./dist","rootDir":"./src","target":99},"referencedMap":[],"exportedModulesMap":[],"semanticDiagnosticsPerFile":[1,2]},"version":"FakeTSVersion"} + +//// [/src/project/tsconfig.tsbuildinfo.readable.baseline.txt] +{ + "program": { + "fileNames": [ + "../../lib/lib.esnext.d.ts", + "./src/main.ts" + ], + "fileInfos": { + "../../lib/lib.esnext.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": "-2443389309-/// \n/// \n\ndeclare global {\n interface Test {\n }\n}\n\nexport {};\n", + "signature": "-2443389309-/// \n/// \n\ndeclare global {\n interface Test {\n }\n}\n\nexport {};\n", + "affectsGlobalScope": true + } + }, + "options": { + "module": 99, + "outDir": "./dist", + "rootDir": "./src", + "target": 99 + }, + "referencedMap": {}, + "exportedModulesMap": {}, + "semanticDiagnosticsPerFile": [ + "../../lib/lib.esnext.d.ts", + "./src/main.ts" + ] + }, + "version": "FakeTSVersion", + "size": 935 +} +