From 2787a2793ad791c81891768a837fc10c2abed40f Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Thu, 11 Oct 2018 09:31:04 -0700 Subject: [PATCH] Skip writing json file if it is going to overwrite same location Fixes #24715 --- src/compiler/emitter.ts | 4 +++- .../requireOfJsonFileWithoutOutDir.errors.txt | 20 ------------------- 2 files changed, 3 insertions(+), 21 deletions(-) delete mode 100644 tests/baselines/reference/requireOfJsonFileWithoutOutDir.errors.txt diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 6b4b3a5c5104e..8617c8fccdeeb 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -52,7 +52,9 @@ namespace ts { else { const ownOutputFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options)); // If json file emits to the same location skip writing it, if emitDeclarationOnly skip writing it - const jsFilePath = options.emitDeclarationOnly ? undefined : ownOutputFilePath; + const isJsonEmittedToSameLocation = isJsonSourceFile(sourceFile) && + comparePaths(sourceFile.fileName, ownOutputFilePath, host.getCurrentDirectory(), !host.useCaseSensitiveFileNames()) === Comparison.EqualTo; + const jsFilePath = options.emitDeclarationOnly || isJsonEmittedToSameLocation ? undefined : ownOutputFilePath; const sourceMapFilePath = !jsFilePath || isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options); // For legacy reasons (ie, we have baselines capturing the behavior), js files don't report a .d.ts output path - this would only matter if `declaration` and `allowJs` were both on, which is currently an error const isJs = isSourceFileJS(sourceFile); diff --git a/tests/baselines/reference/requireOfJsonFileWithoutOutDir.errors.txt b/tests/baselines/reference/requireOfJsonFileWithoutOutDir.errors.txt deleted file mode 100644 index 07f0f1b84d76c..0000000000000 --- a/tests/baselines/reference/requireOfJsonFileWithoutOutDir.errors.txt +++ /dev/null @@ -1,20 +0,0 @@ -error TS5055: Cannot write file 'tests/cases/compiler/b.json' because it would overwrite input file. - Adding a tsconfig.json file will help organize projects that contain both TypeScript and JavaScript files. Learn more at https://aka.ms/tsconfig. - - -!!! error TS5055: Cannot write file 'tests/cases/compiler/b.json' because it would overwrite input file. -!!! 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. -==== tests/cases/compiler/file1.ts (0 errors) ==== - import b1 = require('./b.json'); - let x = b1.a; - import b2 = require('./b.json'); - if (x) { - let b = b2.b; - x = (b1.b === b); - } - -==== tests/cases/compiler/b.json (0 errors) ==== - { - "a": true, - "b": "hello" - } \ No newline at end of file