Skip to content

Commit b9145f9

Browse files
authored
Merge pull request #30980 from Microsoft/configChangeForEmit
Make sure to emit again if change in compiler option affects emit
2 parents a40b08d + 15ae8a7 commit b9145f9

File tree

8 files changed

+238
-5
lines changed

8 files changed

+238
-5
lines changed

src/compiler/builder.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,7 @@ namespace ts {
236236
}
237237
});
238238

239-
if (oldCompilerOptions &&
240-
(oldCompilerOptions.outDir !== compilerOptions.outDir ||
241-
oldCompilerOptions.declarationDir !== compilerOptions.declarationDir ||
242-
(oldCompilerOptions.outFile || oldCompilerOptions.out) !== (compilerOptions.outFile || compilerOptions.out))) {
239+
if (oldCompilerOptions && compilerOptionsAffectEmit(compilerOptions, oldCompilerOptions)) {
243240
// Add all files to affectedFilesPendingEmit since emit changed
244241
state.affectedFilesPendingEmit = concatenate(state.affectedFilesPendingEmit, newProgram.getSourceFiles().map(f => f.path));
245242
if (state.affectedFilesPendingEmitIndex === undefined) {

src/compiler/commandLineParser.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,33 +281,38 @@ namespace ts {
281281
name: "declaration",
282282
shortName: "d",
283283
type: "boolean",
284+
affectsEmit: true,
284285
showInSimplifiedHelpView: true,
285286
category: Diagnostics.Basic_Options,
286287
description: Diagnostics.Generates_corresponding_d_ts_file,
287288
},
288289
{
289290
name: "declarationMap",
290291
type: "boolean",
292+
affectsEmit: true,
291293
showInSimplifiedHelpView: true,
292294
category: Diagnostics.Basic_Options,
293295
description: Diagnostics.Generates_a_sourcemap_for_each_corresponding_d_ts_file,
294296
},
295297
{
296298
name: "emitDeclarationOnly",
297299
type: "boolean",
300+
affectsEmit: true,
298301
category: Diagnostics.Advanced_Options,
299302
description: Diagnostics.Only_emit_d_ts_declaration_files,
300303
},
301304
{
302305
name: "sourceMap",
303306
type: "boolean",
307+
affectsEmit: true,
304308
showInSimplifiedHelpView: true,
305309
category: Diagnostics.Basic_Options,
306310
description: Diagnostics.Generates_corresponding_map_file,
307311
},
308312
{
309313
name: "outFile",
310314
type: "string",
315+
affectsEmit: true,
311316
isFilePath: true,
312317
paramType: Diagnostics.FILE,
313318
showInSimplifiedHelpView: true,
@@ -317,6 +322,7 @@ namespace ts {
317322
{
318323
name: "outDir",
319324
type: "string",
325+
affectsEmit: true,
320326
isFilePath: true,
321327
paramType: Diagnostics.DIRECTORY,
322328
showInSimplifiedHelpView: true,
@@ -326,6 +332,7 @@ namespace ts {
326332
{
327333
name: "rootDir",
328334
type: "string",
335+
affectsEmit: true,
329336
isFilePath: true,
330337
paramType: Diagnostics.LOCATION,
331338
category: Diagnostics.Basic_Options,
@@ -334,13 +341,15 @@ namespace ts {
334341
{
335342
name: "composite",
336343
type: "boolean",
344+
affectsEmit: true,
337345
isTSConfigOnly: true,
338346
category: Diagnostics.Basic_Options,
339347
description: Diagnostics.Enable_project_compilation,
340348
},
341349
{
342350
name: "tsBuildInfoFile",
343351
type: "string",
352+
affectsEmit: true,
344353
isFilePath: true,
345354
paramType: Diagnostics.FILE,
346355
category: Diagnostics.Basic_Options,
@@ -349,26 +358,30 @@ namespace ts {
349358
{
350359
name: "removeComments",
351360
type: "boolean",
361+
affectsEmit: true,
352362
showInSimplifiedHelpView: true,
353363
category: Diagnostics.Basic_Options,
354364
description: Diagnostics.Do_not_emit_comments_to_output,
355365
},
356366
{
357367
name: "noEmit",
358368
type: "boolean",
369+
affectsEmit: true,
359370
showInSimplifiedHelpView: true,
360371
category: Diagnostics.Basic_Options,
361372
description: Diagnostics.Do_not_emit_outputs,
362373
},
363374
{
364375
name: "importHelpers",
365376
type: "boolean",
377+
affectsEmit: true,
366378
category: Diagnostics.Basic_Options,
367379
description: Diagnostics.Import_emit_helpers_from_tslib
368380
},
369381
{
370382
name: "downlevelIteration",
371383
type: "boolean",
384+
affectsEmit: true,
372385
category: Diagnostics.Basic_Options,
373386
description: Diagnostics.Provide_full_support_for_iterables_in_for_of_spread_and_destructuring_when_targeting_ES5_or_ES3
374387
},
@@ -580,26 +593,30 @@ namespace ts {
580593
{
581594
name: "sourceRoot",
582595
type: "string",
596+
affectsEmit: true,
583597
paramType: Diagnostics.LOCATION,
584598
category: Diagnostics.Source_Map_Options,
585599
description: Diagnostics.Specify_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations,
586600
},
587601
{
588602
name: "mapRoot",
589603
type: "string",
604+
affectsEmit: true,
590605
paramType: Diagnostics.LOCATION,
591606
category: Diagnostics.Source_Map_Options,
592607
description: Diagnostics.Specify_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations,
593608
},
594609
{
595610
name: "inlineSourceMap",
596611
type: "boolean",
612+
affectsEmit: true,
597613
category: Diagnostics.Source_Map_Options,
598614
description: Diagnostics.Emit_a_single_file_with_source_maps_instead_of_having_a_separate_file
599615
},
600616
{
601617
name: "inlineSources",
602618
type: "boolean",
619+
affectsEmit: true,
603620
category: Diagnostics.Source_Map_Options,
604621
description: Diagnostics.Emit_the_source_alongside_the_sourcemaps_within_a_single_file_requires_inlineSourceMap_or_sourceMap_to_be_set
605622
},
@@ -635,6 +652,7 @@ namespace ts {
635652
{
636653
name: "out",
637654
type: "string",
655+
affectsEmit: true,
638656
isFilePath: false, // This is intentionally broken to support compatability with existing tsconfig files
639657
// for correct behaviour, please use outFile
640658
category: Diagnostics.Advanced_Options,
@@ -644,6 +662,7 @@ namespace ts {
644662
{
645663
name: "reactNamespace",
646664
type: "string",
665+
affectsEmit: true,
647666
category: Diagnostics.Advanced_Options,
648667
description: Diagnostics.Deprecated_Use_jsxFactory_instead_Specify_the_object_invoked_for_createElement_when_targeting_react_JSX_emit
649668
},
@@ -662,6 +681,7 @@ namespace ts {
662681
{
663682
name: "emitBOM",
664683
type: "boolean",
684+
affectsEmit: true,
665685
category: Diagnostics.Advanced_Options,
666686
description: Diagnostics.Emit_a_UTF_8_Byte_Order_Mark_BOM_in_the_beginning_of_output_files
667687
},
@@ -677,6 +697,7 @@ namespace ts {
677697
crlf: NewLineKind.CarriageReturnLineFeed,
678698
lf: NewLineKind.LineFeed
679699
}),
700+
affectsEmit: true,
680701
paramType: Diagnostics.NEWLINE,
681702
category: Diagnostics.Advanced_Options,
682703
description: Diagnostics.Specify_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix,
@@ -704,6 +725,7 @@ namespace ts {
704725
{
705726
name: "stripInternal",
706727
type: "boolean",
728+
affectsEmit: true,
707729
category: Diagnostics.Advanced_Options,
708730
description: Diagnostics.Do_not_emit_declarations_for_code_that_has_an_internal_annotation,
709731
},
@@ -724,24 +746,28 @@ namespace ts {
724746
{
725747
name: "noEmitHelpers",
726748
type: "boolean",
749+
affectsEmit: true,
727750
category: Diagnostics.Advanced_Options,
728751
description: Diagnostics.Do_not_generate_custom_helper_functions_like_extends_in_compiled_output
729752
},
730753
{
731754
name: "noEmitOnError",
732755
type: "boolean",
756+
affectsEmit: true,
733757
category: Diagnostics.Advanced_Options,
734758
description: Diagnostics.Do_not_emit_outputs_if_any_errors_were_reported,
735759
},
736760
{
737761
name: "preserveConstEnums",
738762
type: "boolean",
763+
affectsEmit: true,
739764
category: Diagnostics.Advanced_Options,
740765
description: Diagnostics.Do_not_erase_const_enum_declarations_in_generated_code
741766
},
742767
{
743768
name: "declarationDir",
744769
type: "string",
770+
affectsEmit: true,
745771
isFilePath: true,
746772
paramType: Diagnostics.DIRECTORY,
747773
category: Diagnostics.Advanced_Options,
@@ -826,6 +852,10 @@ namespace ts {
826852
export const semanticDiagnosticsOptionDeclarations: ReadonlyArray<CommandLineOption> =
827853
optionDeclarations.filter(option => !!option.affectsSemanticDiagnostics);
828854

855+
/* @internal */
856+
export const affectsEmitOptionDeclarations: ReadonlyArray<CommandLineOption> =
857+
optionDeclarations.filter(option => !!option.affectsEmit);
858+
829859
/* @internal */
830860
export const moduleResolutionOptionDeclarations: ReadonlyArray<CommandLineOption> =
831861
optionDeclarations.filter(option => !!option.affectsModuleResolution);

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4829,6 +4829,7 @@ namespace ts {
48294829
affectsModuleResolution?: true; // currently same effect as `affectsSourceFile`
48304830
affectsBindDiagnostics?: true; // true if this affects binding (currently same effect as `affectsSourceFile`)
48314831
affectsSemanticDiagnostics?: true; // true if option affects semantic diagnostics
4832+
affectsEmit?: true; // true if the options affects emit
48324833
}
48334834

48344835
/* @internal */

src/compiler/utilities.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7320,6 +7320,11 @@ namespace ts {
73207320
semanticDiagnosticsOptionDeclarations.some(option => !isJsonEqual(getCompilerOptionValue(oldOptions, option), getCompilerOptionValue(newOptions, option)));
73217321
}
73227322

7323+
export function compilerOptionsAffectEmit(newOptions: CompilerOptions, oldOptions: CompilerOptions): boolean {
7324+
return oldOptions !== newOptions &&
7325+
affectsEmitOptionDeclarations.some(option => !isJsonEqual(getCompilerOptionValue(oldOptions, option), getCompilerOptionValue(newOptions, option)));
7326+
}
7327+
73237328
export function getCompilerOptionValue(options: CompilerOptions, option: CommandLineOption): unknown {
73247329
return option.strictFlag ? getStrictOptionValue(options, option.name as StrictOptionName) : options[option.name];
73257330
}

src/testRunner/unittests/tsbuild/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ Mismatch Actual(path, actual, expected): ${JSON.stringify(arrayFrom(mapDefinedIt
305305
actualReadFileMap = undefined!;
306306
host = undefined!;
307307
});
308-
if (!baselineOnly) {
308+
if (!baselineOnly || verifyDiagnostics) {
309309
it(`verify diagnostics`, () => {
310310
host.assertDiagnosticMessages(...(incrementalExpectedDiagnostics || emptyArray));
311311
});

src/testRunner/unittests/tsbuild/sample.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,47 @@ class someClass { }`),
720720
"/src/tests/tsconfig.tsbuildinfo",
721721
]
722722
});
723+
724+
verifyTsbuildOutput({
725+
scenario: "when declaration option changes",
726+
projFs: () => projFs,
727+
time,
728+
tick,
729+
proj: "sample1",
730+
rootNames: ["/src/core"],
731+
expectedMapFileNames: emptyArray,
732+
lastProjectOutputJs: "/src/core/index.js",
733+
initialBuild: {
734+
modifyFs: fs => fs.writeFileSync("/src/core/tsconfig.json", `{
735+
"compilerOptions": {
736+
"incremental": true,
737+
"skipDefaultLibCheck": true
738+
}
739+
}`),
740+
expectedDiagnostics: [
741+
getExpectedDiagnosticForProjectsInBuild("src/core/tsconfig.json"),
742+
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/core/tsconfig.json", "src/core/anotherModule.js"],
743+
[Diagnostics.Building_project_0, "/src/core/tsconfig.json"],
744+
]
745+
},
746+
incrementalDtsChangedBuild: {
747+
modifyFs: fs => replaceText(fs, "/src/core/tsconfig.json", `"incremental": true,`, `"incremental": true, "declaration": true,`),
748+
expectedDiagnostics: [
749+
getExpectedDiagnosticForProjectsInBuild("src/core/tsconfig.json"),
750+
[Diagnostics.Project_0_is_out_of_date_because_output_file_1_does_not_exist, "src/core/tsconfig.json", "src/core/anotherModule.d.ts"],
751+
[Diagnostics.Building_project_0, "/src/core/tsconfig.json"]
752+
]
753+
},
754+
outputFiles: [
755+
"/src/core/anotherModule.js",
756+
"/src/core/anotherModule.d.ts",
757+
"/src/core/index.js",
758+
"/src/core/index.d.ts",
759+
"/src/core/tsconfig.tsbuildinfo",
760+
],
761+
baselineOnly: true,
762+
verifyDiagnostics: true
763+
});
723764
});
724765
});
725766
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//// [/src/core/anotherModule.d.ts]
2+
export declare const World = "hello";
3+
4+
5+
//// [/src/core/index.d.ts]
6+
export declare const someString: string;
7+
export declare function leftPad(s: string, n: number): string;
8+
export declare function multiply(a: number, b: number): number;
9+
10+
11+
//// [/src/core/tsconfig.json]
12+
{
13+
"compilerOptions": {
14+
"incremental": true, "declaration": true,
15+
"skipDefaultLibCheck": true
16+
}
17+
}
18+
19+
//// [/src/core/tsconfig.tsbuildinfo]
20+
{
21+
"program": {
22+
"fileInfos": {
23+
"/lib/lib.d.ts": {
24+
"version": "/lib/lib.d.ts",
25+
"signature": "/lib/lib.d.ts"
26+
},
27+
"/lib/lib.es5.d.ts": {
28+
"version": "/lib/lib.es5.d.ts",
29+
"signature": "/lib/lib.es5.d.ts"
30+
},
31+
"/lib/lib.dom.d.ts": {
32+
"version": "/lib/lib.dom.d.ts",
33+
"signature": "/lib/lib.dom.d.ts"
34+
},
35+
"/lib/lib.webworker.importscripts.d.ts": {
36+
"version": "/lib/lib.webworker.importscripts.d.ts",
37+
"signature": "/lib/lib.webworker.importscripts.d.ts"
38+
},
39+
"/lib/lib.scripthost.d.ts": {
40+
"version": "/lib/lib.scripthost.d.ts",
41+
"signature": "/lib/lib.scripthost.d.ts"
42+
},
43+
"/src/core/anothermodule.ts": {
44+
"version": "-2676574883",
45+
"signature": "-8396256275"
46+
},
47+
"/src/core/index.ts": {
48+
"version": "-18749805970",
49+
"signature": "1874987148"
50+
},
51+
"/src/core/some_decl.d.ts": {
52+
"version": "-9253692965",
53+
"signature": "-9253692965"
54+
}
55+
},
56+
"options": {
57+
"incremental": true,
58+
"declaration": true,
59+
"skipDefaultLibCheck": true,
60+
"configFilePath": "/src/core/tsconfig.json"
61+
},
62+
"referencedMap": {},
63+
"exportedModulesMap": {},
64+
"semanticDiagnosticsPerFile": [
65+
"/lib/lib.d.ts",
66+
"/lib/lib.dom.d.ts",
67+
"/lib/lib.es5.d.ts",
68+
"/lib/lib.scripthost.d.ts",
69+
"/lib/lib.webworker.importscripts.d.ts",
70+
"/src/core/anothermodule.ts",
71+
"/src/core/index.ts",
72+
"/src/core/some_decl.d.ts"
73+
]
74+
},
75+
"version": "FakeTSVersion"
76+
}
77+

0 commit comments

Comments
 (0)