Skip to content

Commit d011906

Browse files
committed
* add test for the fix for overwrite emitting error * cr feedback
1 parent 702efd5 commit d011906

File tree

43 files changed

+148
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+148
-8
lines changed

src/compiler/core.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,18 @@ namespace ts {
11271127
};
11281128
}
11291129

1130+
export function createCompilerDiagnosticFromMessageChain(chain: DiagnosticMessageChain): Diagnostic {
1131+
return {
1132+
file: undefined,
1133+
start: undefined,
1134+
length: undefined,
1135+
1136+
code: chain.code,
1137+
category: chain.category,
1138+
messageText: chain.next ? chain : chain.messageText
1139+
};
1140+
}
1141+
11301142
export function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage, ...args: any[]): DiagnosticMessageChain;
11311143
export function chainDiagnosticMessages(details: DiagnosticMessageChain, message: DiagnosticMessage): DiagnosticMessageChain {
11321144
let text = getLocaleSpecificMessage(message);

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3158,5 +3158,9 @@
31583158
"Implement inherited abstract class": {
31593159
"category": "Message",
31603160
"code": 90007
3161+
},
3162+
"Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig": {
3163+
"category": "Error",
3164+
"code": 90009
31613165
}
31623166
}

src/compiler/program.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ namespace ts {
239239
const { line, character } = getLineAndCharacterOfPosition(diagnostic.file, diagnostic.start);
240240
const fileName = diagnostic.file.fileName;
241241
const relativeFileName = convertToRelativePath(fileName, host.getCurrentDirectory(), fileName => host.getCanonicalFileName(fileName));
242-
output += `${ relativeFileName }(${ line + 1 },${ character + 1 }): `;
242+
output += `${relativeFileName}(${line + 1},${character + 1}): `;
243243
}
244244

245245
const category = DiagnosticCategory[diagnostic.category].toLowerCase();
246-
output += `${ category } TS${ diagnostic.code }: ${ flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine()) }${ host.getNewLine() }`;
246+
output += `${category} TS${diagnostic.code}: ${flattenDiagnosticMessageText(diagnostic.messageText, host.getNewLine())}${host.getNewLine()}`;
247247
}
248248
return output;
249249
}
@@ -1685,13 +1685,24 @@ namespace ts {
16851685
const emitFilePath = toPath(emitFileName, currentDirectory, getCanonicalFileName);
16861686
// Report error if the output overwrites input file
16871687
if (filesByName.contains(emitFilePath)) {
1688-
createEmitBlockingDiagnostics(emitFileName, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file);
1688+
if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) {
1689+
blockEmittingOfFile(emitFileName);
1690+
}
1691+
else {
1692+
let chain: DiagnosticMessageChain;
1693+
if (!options.configFilePath) {
1694+
// The program is from either an inferred project or an external project
1695+
chain = chainDiagnosticMessages(/*details*/ undefined, Diagnostics.Adding_a_tsconfig_json_file_will_help_organize_projects_that_contain_both_TypeScript_and_JavaScript_files_Learn_more_at_https_Colon_Slash_Slashaka_ms_Slashtsconfig);
1696+
}
1697+
chain = chainDiagnosticMessages(chain, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName);
1698+
blockEmittingOfFile(emitFileName, createCompilerDiagnosticFromMessageChain(chain));
1699+
}
16891700
}
16901701

16911702
// Report error if multiple files write into same file
16921703
if (emitFilesSeen.contains(emitFilePath)) {
16931704
// Already seen the same emit file - report error
1694-
createEmitBlockingDiagnostics(emitFileName, Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files);
1705+
blockEmittingOfFile(emitFileName, createCompilerDiagnostic(Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName));
16951706
}
16961707
else {
16971708
emitFilesSeen.set(emitFilePath, true);
@@ -1700,9 +1711,11 @@ namespace ts {
17001711
}
17011712
}
17021713

1703-
function createEmitBlockingDiagnostics(emitFileName: string, message: DiagnosticMessage) {
1714+
function blockEmittingOfFile(emitFileName: string, diag?: Diagnostic) {
17041715
hasEmitBlockingDiagnostics.set(toPath(emitFileName, currentDirectory, getCanonicalFileName), true);
1705-
programDiagnostics.add(createCompilerDiagnostic(message, emitFileName));
1716+
if (diag) {
1717+
programDiagnostics.add(diag);
1718+
}
17061719
}
17071720
}
17081721

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3058,6 +3058,7 @@ namespace ts {
30583058
moduleResolution?: ModuleResolutionKind;
30593059
newLine?: NewLineKind;
30603060
noEmit?: boolean;
3061+
/*@internal*/noEmitOverwritenFiles?: boolean;
30613062
noEmitHelpers?: boolean;
30623063
noEmitOnError?: boolean;
30633064
noErrorTruncation?: boolean;

src/harness/fourslash.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,18 @@ namespace FourSlash {
12531253
resultString += "Diagnostics:" + Harness.IO.newLine();
12541254
const diagnostics = ts.getPreEmitDiagnostics(this.languageService.getProgram());
12551255
for (const diagnostic of diagnostics) {
1256-
resultString += " " + diagnostic.messageText + Harness.IO.newLine();
1256+
if (typeof diagnostic.messageText !== "string") {
1257+
let chainedMessage = <ts.DiagnosticMessageChain>diagnostic.messageText;
1258+
let indentation = " ";
1259+
while (chainedMessage) {
1260+
resultString += indentation + chainedMessage.messageText + Harness.IO.newLine();
1261+
chainedMessage = chainedMessage.next;
1262+
indentation = indentation + " ";
1263+
}
1264+
}
1265+
else {
1266+
resultString += " " + diagnostic.messageText + Harness.IO.newLine();
1267+
}
12571268
}
12581269
}
12591270

src/harness/unittests/tsserverProjectSystem.ts

+24
Original file line numberDiff line numberDiff line change
@@ -2505,4 +2505,28 @@ namespace ts.projectSystem {
25052505
checkProjectActualFiles(projectService.inferredProjects[0], [f.path]);
25062506
});
25072507
});
2508+
2509+
describe("No overwrite emit error", () => {
2510+
it("for inferred project", () => {
2511+
const f1 = {
2512+
path: "/a/b/f1.js",
2513+
content: "function test1() { }"
2514+
};
2515+
const host = createServerHost([f1, libFile]);
2516+
const session = createSession(host);
2517+
openFilesForSession([f1], session);
2518+
2519+
const projectService = session.getProjectService();
2520+
checkNumberOfProjects(projectService, { inferredProjects: 1 });
2521+
const projectName = projectService.inferredProjects[0].getProjectName();
2522+
2523+
const diags = session.executeCommand(<server.protocol.CompilerOptionsDiagnosticsRequest>{
2524+
type: "request",
2525+
command: server.CommandNames.CompilerOptionsDiagnosticsFull,
2526+
seq: 2,
2527+
arguments: { projectFileName: projectName }
2528+
}).response;
2529+
assert.isTrue(diags.length === 0);
2530+
});
2531+
});
25082532
}

src/server/project.ts

+4
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,10 @@ namespace ts.server {
161161
this.compilerOptions.allowNonTsExtensions = true;
162162
}
163163

164+
if (this.projectKind === ProjectKind.Inferred) {
165+
this.compilerOptions.noEmitOverwritenFiles = true;
166+
}
167+
164168
if (languageServiceEnabled) {
165169
this.enableLanguageService();
166170
}

tests/baselines/reference/compileOnSaveWorksWhenEmitBlockingErrorOnOtherFile.baseline

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
EmitSkipped: true
22
Diagnostics:
3-
Cannot write file '/tests/cases/fourslash/b.js' because it would overwrite input file.
3+
Cannot write file '/tests/cases/fourslash/b.js' because it would overwrite input file.
4+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
45

56
EmitSkipped: false
67
FileName : /tests/cases/fourslash/a.js

tests/baselines/reference/declarationFileOverwriteError.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.d.ts' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.d.ts' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/compiler/a.d.ts (0 errors) ====
68

79
declare class c {

tests/baselines/reference/declarationFileOverwriteErrorWithOut.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/out.d.ts' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/compiler/out.d.ts' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/compiler/out.d.ts (0 errors) ====
68

79
declare class c {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/conformance/salsa/myFile01.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/conformance/salsa/myFile01.js' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/conformance/salsa/myFile01.js (0 errors) ====
68

79
export default "hello";
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/conformance/salsa/myFile02.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/conformance/salsa/myFile02.js' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/conformance/salsa/myFile02.js (0 errors) ====
68

79
export default "hello";

tests/baselines/reference/jsFileCompilationAmbientVarDeclarationSyntax.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,1): error TS8009: 'declare' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
declare var v;
810
~~~~~~~

tests/baselines/reference/jsFileCompilationEmitBlockedCorrectly.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
error TS5056: Cannot write file 'tests/cases/compiler/a.js' because it would be overwritten by multiple input files.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
!!! error TS5056: Cannot write file 'tests/cases/compiler/a.js' because it would be overwritten by multiple input files.
79
==== tests/cases/compiler/a.ts (0 errors) ====
810
class c {

tests/baselines/reference/jsFileCompilationEnumSyntax.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,6): error TS8015: 'enum declarations' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
enum E { }
810
~

tests/baselines/reference/jsFileCompilationErrorOnDeclarationsWithJsFileReferenceWithNoOut.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
22
error TS5055: Cannot write file 'tests/cases/compiler/c.js' because it would overwrite input file.
3+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
34

45

56
!!! error TS5053: Option 'allowJs' cannot be specified with option 'declaration'.
67
!!! error TS5055: Cannot write file 'tests/cases/compiler/c.js' because it would overwrite input file.
8+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
79
==== tests/cases/compiler/a.ts (0 errors) ====
810
class c {
911
}

tests/baselines/reference/jsFileCompilationExportAssignmentSyntax.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,1): error TS8003: 'export=' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
export = b;
810
~~~~~~~~~~~

tests/baselines/reference/jsFileCompilationHeritageClauseSyntaxOfClass.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,9): error TS8005: 'implements clauses' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
class C implements D { }
810
~~~~~~~~~~~~

tests/baselines/reference/jsFileCompilationImportEqualsSyntax.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,1): error TS8002: 'import ... =' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
import a = b;
810
~~~~~~~~~~~~~

tests/baselines/reference/jsFileCompilationInterfaceSyntax.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,11): error TS8006: 'interface declarations' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
interface I { }
810
~

tests/baselines/reference/jsFileCompilationModuleSyntax.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,8): error TS8007: 'module declarations' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
module M { }
810
~

tests/baselines/reference/jsFileCompilationNoErrorWithoutDeclarationsWithJsFileReferenceWithNoOut.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/c.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23

34

45
!!! error TS5055: Cannot write file 'tests/cases/compiler/c.js' because it would overwrite input file.
6+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
57
==== tests/cases/compiler/a.ts (0 errors) ====
68
class c {
79
}

tests/baselines/reference/jsFileCompilationOptionalParameter.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(1,13): error TS8009: '?' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
function F(p?) { }
810
~

tests/baselines/reference/jsFileCompilationPublicMethodSyntaxOfClass.errors.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
2+
Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
23
tests/cases/compiler/a.js(2,5): error TS8009: 'public' can only be used in a .ts file.
34

45

56
!!! error TS5055: Cannot write file 'tests/cases/compiler/a.js' because it would overwrite input file.
7+
!!! error TS5055: Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig
68
==== tests/cases/compiler/a.js (1 errors) ====
79
class C {
810
public foo() {

0 commit comments

Comments
 (0)