Skip to content

Commit e72f006

Browse files
authored
Merge pull request microsoft#30524 from Microsoft/moduleResolutionError
Report output file not built error for any module resolution that ends up to source file
2 parents adf760a + 8da384d commit e72f006

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,19 +2352,12 @@ namespace ts {
23522352
}
23532353

23542354
if (moduleNotFoundError) {
2355-
// For relative paths, see if this was possibly a projectReference redirect
2356-
if (pathIsRelative(moduleReference)) {
2357-
const sourceFile = getSourceFileOfNode(location);
2358-
const redirects = sourceFile.redirectedReferences;
2359-
if (redirects) {
2360-
const normalizedTargetPath = getNormalizedAbsolutePath(moduleReference, getDirectoryPath(sourceFile.fileName));
2361-
for (const ext of [Extension.Ts, Extension.Tsx]) {
2362-
const probePath = normalizedTargetPath + ext;
2363-
if (redirects.indexOf(probePath) >= 0) {
2364-
error(errorNode, Diagnostics.Output_file_0_has_not_been_built_from_source_file_1, moduleReference, probePath);
2365-
return undefined;
2366-
}
2367-
}
2355+
// See if this was possibly a projectReference redirect
2356+
if (resolvedModule) {
2357+
const redirect = host.getProjectReferenceRedirect(resolvedModule.resolvedFileName);
2358+
if (redirect) {
2359+
error(errorNode, Diagnostics.Output_file_0_has_not_been_built_from_source_file_1, redirect, resolvedModule.resolvedFileName);
2360+
return undefined;
23682361
}
23692362
}
23702363

src/compiler/program.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2268,7 +2268,6 @@ namespace ts {
22682268
if (refFile) {
22692269
const redirect = getProjectReferenceRedirect(fileName);
22702270
if (redirect) {
2271-
((refFile.redirectedReferences || (refFile.redirectedReferences = [])) as string[]).push(fileName);
22722271
fileName = redirect;
22732272
// Once we start redirecting to a file, we can potentially come back to it
22742273
// via a back-reference from another file in the .d.ts folder. If that happens we'll

src/compiler/types.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2740,12 +2740,6 @@ namespace ts {
27402740
/* @internal */ resolvedModules?: Map<ResolvedModuleFull | undefined>;
27412741
/* @internal */ resolvedTypeReferenceDirectiveNames: Map<ResolvedTypeReferenceDirective | undefined>;
27422742
/* @internal */ imports: ReadonlyArray<StringLiteralLike>;
2743-
/**
2744-
* When a file's references are redirected due to project reference directives,
2745-
* the original names of the references are stored in this array
2746-
*/
2747-
/* @internal*/
2748-
redirectedReferences?: ReadonlyArray<string>;
27492743
// Identifier only if `declare global`
27502744
/* @internal */ moduleAugmentations: ReadonlyArray<StringLiteral | Identifier>;
27512745
/* @internal */ patternAmbientModules?: PatternAmbientModule[];
@@ -3074,6 +3068,7 @@ namespace ts {
30743068
getSourceFiles(): ReadonlyArray<SourceFile>;
30753069
getSourceFile(fileName: string): SourceFile | undefined;
30763070
getResolvedTypeReferenceDirectives(): ReadonlyMap<ResolvedTypeReferenceDirective | undefined>;
3071+
getProjectReferenceRedirect(fileName: string): string | undefined;
30773072

30783073
readonly redirectTargetsMap: RedirectTargetsMap;
30793074
}

src/testRunner/unittests/config/projectReferences.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,28 @@ namespace ts {
284284
assertHasError("Issues a useful error", program.getSemanticDiagnostics(), Diagnostics.Output_file_0_has_not_been_built_from_source_file_1);
285285
});
286286
});
287+
288+
it("issues a nice error when the input file is missing when module reference is not relative", () => {
289+
const spec: TestSpecification = {
290+
"/alpha": {
291+
files: { "/alpha/a.ts": "export const m: number = 3;" },
292+
references: []
293+
},
294+
"/beta": {
295+
files: { "/beta/b.ts": "import { m } from '@alpha/a'" },
296+
references: ["../alpha"],
297+
options: {
298+
baseUrl: "./",
299+
paths: {
300+
"@alpha/*": ["/alpha/*"]
301+
}
302+
}
303+
}
304+
};
305+
testProjectReferences(spec, "/beta/tsconfig.json", program => {
306+
assertHasError("Issues a useful error", program.getSemanticDiagnostics(), Diagnostics.Output_file_0_has_not_been_built_from_source_file_1);
307+
});
308+
});
287309
});
288310

289311
/**

0 commit comments

Comments
 (0)