From f48a4ca86efc26f2f323daccfd3a59e367e885df 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 | 17 ++++++++++------ src/compiler/transformers/declarations.ts | 2 +- src/compiler/utilities.ts | 2 +- .../requireOfJsonFileWithoutOutDir.errors.txt | 20 ------------------- 4 files changed, 13 insertions(+), 28 deletions(-) delete mode 100644 tests/baselines/reference/requireOfJsonFileWithoutOutDir.errors.txt diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index fe54402d43481..49b6b82ea5e7d 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -49,8 +49,13 @@ namespace ts { return { jsFilePath, sourceMapFilePath, declarationFilePath, declarationMapPath, bundleInfoPath }; } else { - const jsFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options)); - const sourceMapFilePath = isJsonSourceFile(sourceFile) ? undefined : getSourceMapFilePath(jsFilePath, options); + const ownOutputFilePath = getOwnEmitOutputFilePath(sourceFile.fileName, host, getOutputExtension(sourceFile, options)); + // If json file emits to the same location skip writing it + const jsFilePath = isJsonSourceFile(sourceFile) && + comparePaths(sourceFile.fileName, ownOutputFilePath, host.getCurrentDirectory(), !host.useCaseSensitiveFileNames()) === Comparison.EqualTo ? + undefined : + ownOutputFilePath; + const sourceMapFilePath = 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); const declarationFilePath = ((forceDtsPaths || getEmitDeclarations(options)) && !isJs) ? getDeclarationEmitOutputFilePath(sourceFile.fileName, host) : undefined; @@ -134,7 +139,7 @@ namespace ts { emitDeclarationFileOrBundle(sourceFileOrBundle, declarationFilePath, declarationMapPath); if (!emitSkipped && emittedFilesList) { - if (!emitOnlyDtsFiles) { + if (!emitOnlyDtsFiles && jsFilePath) { emittedFilesList.push(jsFilePath); } if (sourceMapFilePath) { @@ -149,13 +154,13 @@ namespace ts { } } - function emitJsFileOrBundle(sourceFileOrBundle: SourceFile | Bundle, jsFilePath: string, sourceMapFilePath: string | undefined, bundleInfoPath: string | undefined) { + function emitJsFileOrBundle(sourceFileOrBundle: SourceFile | Bundle, jsFilePath: string | undefined, sourceMapFilePath: string | undefined, bundleInfoPath: string | undefined) { // Make sure not to write js file and source map file if any of them cannot be written - if (host.isEmitBlocked(jsFilePath) || compilerOptions.noEmit || compilerOptions.emitDeclarationOnly) { + if ((jsFilePath && host.isEmitBlocked(jsFilePath)) || compilerOptions.noEmit || compilerOptions.emitDeclarationOnly) { emitSkipped = true; return; } - if (emitOnlyDtsFiles) { + if (emitOnlyDtsFiles || !jsFilePath) { return; } // Transform the source files diff --git a/src/compiler/transformers/declarations.ts b/src/compiler/transformers/declarations.ts index 5312efb5aac03..00e787c421bc1 100644 --- a/src/compiler/transformers/declarations.ts +++ b/src/compiler/transformers/declarations.ts @@ -275,7 +275,7 @@ namespace ts { else { if (isBundledEmit && contains((node as Bundle).sourceFiles, file)) return; // Omit references to files which are being merged const paths = getOutputPathsFor(file, host, /*forceDtsPaths*/ true); - declFileName = paths.declarationFilePath || paths.jsFilePath; + declFileName = paths.declarationFilePath || paths.jsFilePath || file.fileName; } if (declFileName) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index ae75290d56ea0..564e74fc4d35d 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -3245,7 +3245,7 @@ namespace ts { } export interface EmitFileNames { - jsFilePath: string; + jsFilePath: string | undefined; sourceMapFilePath: string | undefined; declarationFilePath: string | undefined; declarationMapPath: string | undefined; 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