Skip to content

Commit 0d95ce2

Browse files
committed
Add error for referencing referenced project input file
1 parent 1558c7f commit 0d95ce2

8 files changed

+1376
-1
lines changed

Diff for: src/compiler/checker.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ import {
261261
getCombinedLocalAndExportSymbolFlags,
262262
getCombinedModifierFlags,
263263
getCombinedNodeFlags,
264+
getCommonSourceDirectoryOfConfig,
264265
getContainingClass,
265266
getContainingClassExcludingClassDecorators,
266267
getContainingClassStaticBlock,
@@ -353,6 +354,7 @@ import {
353354
getPropertyAssignmentAliasLikeExpression,
354355
getPropertyNameForPropertyNameNode,
355356
getPropertyNameFromType,
357+
getRelativePathFromDirectory,
356358
getRelativePathFromFile,
357359
getResolutionDiagnostic,
358360
getResolutionModeOverride,
@@ -4665,6 +4667,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
46654667
else if (
46664668
compilerOptions.rewriteRelativeImportExtensions
46674669
&& !(location.flags & NodeFlags.Ambient)
4670+
&& !isDeclarationFileName(moduleReference)
46684671
&& !isLiteralImportTypeNode(location)
46694672
&& !isPartOfTypeOnlyImportOrExportDeclaration(location)
46704673
) {
@@ -4676,13 +4679,29 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
46764679
getRelativePathFromFile(getNormalizedAbsolutePath(currentSourceFile.fileName, host.getCurrentDirectory()), resolvedModule.resolvedFileName, hostGetCanonicalFileName(host)),
46774680
);
46784681
}
4679-
else if (resolvedModule.resolvedUsingTsExtension && !shouldRewrite && !isDeclarationFileName(moduleReference) && sourceFileMayBeEmitted(sourceFile, host)) {
4682+
else if (resolvedModule.resolvedUsingTsExtension && !shouldRewrite && sourceFileMayBeEmitted(sourceFile, host)) {
46804683
error(
46814684
errorNode,
46824685
Diagnostics.This_import_uses_a_0_extension_to_resolve_to_an_input_TypeScript_file_but_will_not_be_rewritten_during_emit_because_it_is_not_a_relative_path,
46834686
getAnyExtensionFromPath(moduleReference),
46844687
);
46854688
}
4689+
else if (resolvedModule.resolvedUsingTsExtension && shouldRewrite) {
4690+
const redirect = host.getResolvedProjectReferenceToRedirect(sourceFile.path);
4691+
if (redirect) {
4692+
const ignoreCase = !host.useCaseSensitiveFileNames();
4693+
const ownRootDir = host.getCommonSourceDirectory();
4694+
const otherRootDir = getCommonSourceDirectoryOfConfig(redirect.commandLine, ignoreCase);
4695+
const rootDirPath = getRelativePathFromDirectory(ownRootDir, otherRootDir, ignoreCase);
4696+
const outDirPath = getRelativePathFromDirectory(compilerOptions.outDir || ownRootDir, redirect.commandLine.options.outDir || otherRootDir, ignoreCase);
4697+
if (rootDirPath !== outDirPath) {
4698+
error(
4699+
errorNode,
4700+
Diagnostics.This_import_path_is_unsafe_to_rewrite_because_it_resolves_to_another_project_and_the_relative_path_between_the_projects_output_files_is_not_the_same_as_the_relative_path_between_its_input_files,
4701+
);
4702+
}
4703+
}
4704+
}
46864705
}
46874706

46884707
if (sourceFile.symbol) {

Diff for: src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -3956,6 +3956,10 @@
39563956
"category": "Error",
39573957
"code": 2875
39583958
},
3959+
"This import path is unsafe to rewrite because it resolves to another project, and the relative path between the projects' output files is not the same as the relative path between its input files.": {
3960+
"category": "Error",
3961+
"code": 2876
3962+
},
39593963

39603964
"Import declaration '{0}' is using private name '{1}'.": {
39613965
"category": "Error",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// === Syntax and Semantic Diagnostics ===
2+
Syntactic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences1.ts':
3+
4+
5+
==== /tests/cases/fourslash/server/packages/common/src/index.ts (0 errors) ====
6+
export {};
7+
==== /tests/cases/fourslash/server/packages/main/src/index.ts (0 errors) ====
8+
import {} from "../../common/src/index.ts";
9+
10+
Semantic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences1.ts':
11+
/tests/cases/fourslash/server/packages/main/src/index.ts(1,16): error TS2876: This import path is unsafe to rewrite because it resolves to another project, and the relative path between the projects' output files is not the same as the relative path between its input files.
12+
13+
14+
==== /tests/cases/fourslash/server/packages/common/src/index.ts (0 errors) ====
15+
export {};
16+
==== /tests/cases/fourslash/server/packages/main/src/index.ts (1 errors) ====
17+
import {} from "../../common/src/index.ts";
18+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
19+
!!! error TS2876: This import path is unsafe to rewrite because it resolves to another project, and the relative path between the projects' output files is not the same as the relative path between its input files.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// === Syntax and Semantic Diagnostics ===
2+
Syntactic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences2.ts':
3+
4+
5+
==== /tests/cases/fourslash/server/src/compiler/parser.ts (0 errors) ====
6+
export {};
7+
==== /tests/cases/fourslash/server/src/services/services.ts (0 errors) ====
8+
import {} from "../compiler/parser.ts";
9+
10+
Semantic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences2.ts':
11+
12+
13+
==== /tests/cases/fourslash/server/src/compiler/parser.ts (0 errors) ====
14+
export {};
15+
==== /tests/cases/fourslash/server/src/services/services.ts (0 errors) ====
16+
import {} from "../compiler/parser.ts";

0 commit comments

Comments
 (0)