From 8da384d32ad44f6dace85a649151b1ba6128d3d0 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Wed, 20 Mar 2019 17:27:19 -0700 Subject: [PATCH] Report output file not built error for any module resolution that ends up to source file Fixes #29918 --- src/compiler/checker.ts | 19 +++++----------- src/compiler/program.ts | 1 - src/compiler/types.ts | 7 +----- .../unittests/config/projectReferences.ts | 22 +++++++++++++++++++ 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ea44fb0f3bd..22333f4ec02 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2352,19 +2352,12 @@ namespace ts { } if (moduleNotFoundError) { - // For relative paths, see if this was possibly a projectReference redirect - if (pathIsRelative(moduleReference)) { - const sourceFile = getSourceFileOfNode(location); - const redirects = sourceFile.redirectedReferences; - if (redirects) { - const normalizedTargetPath = getNormalizedAbsolutePath(moduleReference, getDirectoryPath(sourceFile.fileName)); - for (const ext of [Extension.Ts, Extension.Tsx]) { - const probePath = normalizedTargetPath + ext; - if (redirects.indexOf(probePath) >= 0) { - error(errorNode, Diagnostics.Output_file_0_has_not_been_built_from_source_file_1, moduleReference, probePath); - return undefined; - } - } + // See if this was possibly a projectReference redirect + if (resolvedModule) { + const redirect = host.getProjectReferenceRedirect(resolvedModule.resolvedFileName); + if (redirect) { + error(errorNode, Diagnostics.Output_file_0_has_not_been_built_from_source_file_1, redirect, resolvedModule.resolvedFileName); + return undefined; } } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 979423cdfaa..65e8fc81d9f 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2268,7 +2268,6 @@ namespace ts { if (refFile) { const redirect = getProjectReferenceRedirect(fileName); if (redirect) { - ((refFile.redirectedReferences || (refFile.redirectedReferences = [])) as string[]).push(fileName); fileName = redirect; // Once we start redirecting to a file, we can potentially come back to it // via a back-reference from another file in the .d.ts folder. If that happens we'll diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 088ae34ee34..bc12adf004f 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2740,12 +2740,6 @@ namespace ts { /* @internal */ resolvedModules?: Map; /* @internal */ resolvedTypeReferenceDirectiveNames: Map; /* @internal */ imports: ReadonlyArray; - /** - * When a file's references are redirected due to project reference directives, - * the original names of the references are stored in this array - */ - /* @internal*/ - redirectedReferences?: ReadonlyArray; // Identifier only if `declare global` /* @internal */ moduleAugmentations: ReadonlyArray; /* @internal */ patternAmbientModules?: PatternAmbientModule[]; @@ -3074,6 +3068,7 @@ namespace ts { getSourceFiles(): ReadonlyArray; getSourceFile(fileName: string): SourceFile | undefined; getResolvedTypeReferenceDirectives(): ReadonlyMap; + getProjectReferenceRedirect(fileName: string): string | undefined; readonly redirectTargetsMap: RedirectTargetsMap; } diff --git a/src/testRunner/unittests/config/projectReferences.ts b/src/testRunner/unittests/config/projectReferences.ts index 6c8863314fa..3acffd7a8c6 100644 --- a/src/testRunner/unittests/config/projectReferences.ts +++ b/src/testRunner/unittests/config/projectReferences.ts @@ -284,6 +284,28 @@ namespace ts { assertHasError("Issues a useful error", program.getSemanticDiagnostics(), Diagnostics.Output_file_0_has_not_been_built_from_source_file_1); }); }); + + it("issues a nice error when the input file is missing when module reference is not relative", () => { + const spec: TestSpecification = { + "/alpha": { + files: { "/alpha/a.ts": "export const m: number = 3;" }, + references: [] + }, + "/beta": { + files: { "/beta/b.ts": "import { m } from '@alpha/a'" }, + references: ["../alpha"], + options: { + baseUrl: "./", + paths: { + "@alpha/*": ["/alpha/*"] + } + } + } + }; + testProjectReferences(spec, "/beta/tsconfig.json", program => { + assertHasError("Issues a useful error", program.getSemanticDiagnostics(), Diagnostics.Output_file_0_has_not_been_built_from_source_file_1); + }); + }); }); /**