diff --git a/src/compiler/program.ts b/src/compiler/program.ts index c9f3de0f3d7fa..3b6ae08984a7f 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1537,21 +1537,24 @@ namespace ts { const emitFilePath = toPath(emitFileName, currentDirectory, getCanonicalFileName); // Report error if the output overwrites input file if (filesByName.contains(emitFilePath)) { - let chain: DiagnosticMessageChain; - if (!options.configFilePath) { - // The program is from either an inferred project or an external project - 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); + if (options.noEmitOverwritenFiles && !options.out && !options.outDir && !options.outFile) { + blockEmittingOfFile(emitFileName); + } + else { + let chain: DiagnosticMessageChain; + if (!options.configFilePath) { + // The program is from either an inferred project or an external project + 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); + } + chain = chainDiagnosticMessages(chain, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName); + blockEmittingOfFile(emitFileName, createCompilerDiagnosticFromMessageChain(chain)); } - chain = chainDiagnosticMessages(chain, Diagnostics.Cannot_write_file_0_because_it_would_overwrite_input_file, emitFileName); - const diagnostic = createCompilerDiagnosticFromMessageChain(chain); - createEmitBlockingDiagnostics(emitFileName, diagnostic); } // Report error if multiple files write into same file if (emitFilesSeen.contains(emitFilePath)) { // Already seen the same emit file - report error - const diagnostic = createCompilerDiagnostic(Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName); - createEmitBlockingDiagnostics(emitFileName, diagnostic); + blockEmittingOfFile(emitFileName, createCompilerDiagnostic(Diagnostics.Cannot_write_file_0_because_it_would_be_overwritten_by_multiple_input_files, emitFileName)); } else { emitFilesSeen.set(emitFilePath, true); @@ -1560,9 +1563,11 @@ namespace ts { } } - function createEmitBlockingDiagnostics(emitFileName: string, diag: Diagnostic) { + function blockEmittingOfFile(emitFileName: string, diag?: Diagnostic) { hasEmitBlockingDiagnostics.set(toPath(emitFileName, currentDirectory, getCanonicalFileName), true); - programDiagnostics.add(diag); + if (diag) { + programDiagnostics.add(diag); + } } } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index e6c13235c9b8a..458173d88f1a6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2984,6 +2984,7 @@ namespace ts { moduleResolution?: ModuleResolutionKind; newLine?: NewLineKind; noEmit?: boolean; + /*@internal*/noEmitOverwritenFiles?: boolean; noEmitHelpers?: boolean; noEmitOnError?: boolean; noErrorTruncation?: boolean; diff --git a/src/server/project.ts b/src/server/project.ts index 5f4f8d4049027..8b134d3fe1cc3 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -161,6 +161,10 @@ namespace ts.server { this.compilerOptions.allowNonTsExtensions = true; } + if (this.projectKind === ProjectKind.Inferred) { + this.compilerOptions.noEmitOverwritenFiles = true; + } + if (languageServiceEnabled) { this.enableLanguageService(); }