Skip to content

Commit

Permalink
Add error for referencing referenced project input file
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Sep 11, 2024
1 parent 1558c7f commit 0d95ce2
Show file tree
Hide file tree
Showing 8 changed files with 1,376 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ import {
getCombinedLocalAndExportSymbolFlags,
getCombinedModifierFlags,
getCombinedNodeFlags,
getCommonSourceDirectoryOfConfig,
getContainingClass,
getContainingClassExcludingClassDecorators,
getContainingClassStaticBlock,
Expand Down Expand Up @@ -353,6 +354,7 @@ import {
getPropertyAssignmentAliasLikeExpression,
getPropertyNameForPropertyNameNode,
getPropertyNameFromType,
getRelativePathFromDirectory,
getRelativePathFromFile,
getResolutionDiagnostic,
getResolutionModeOverride,
Expand Down Expand Up @@ -4665,6 +4667,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
else if (
compilerOptions.rewriteRelativeImportExtensions
&& !(location.flags & NodeFlags.Ambient)
&& !isDeclarationFileName(moduleReference)
&& !isLiteralImportTypeNode(location)
&& !isPartOfTypeOnlyImportOrExportDeclaration(location)
) {
Expand All @@ -4676,13 +4679,29 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
getRelativePathFromFile(getNormalizedAbsolutePath(currentSourceFile.fileName, host.getCurrentDirectory()), resolvedModule.resolvedFileName, hostGetCanonicalFileName(host)),
);
}
else if (resolvedModule.resolvedUsingTsExtension && !shouldRewrite && !isDeclarationFileName(moduleReference) && sourceFileMayBeEmitted(sourceFile, host)) {
else if (resolvedModule.resolvedUsingTsExtension && !shouldRewrite && sourceFileMayBeEmitted(sourceFile, host)) {
error(
errorNode,
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,
getAnyExtensionFromPath(moduleReference),
);
}
else if (resolvedModule.resolvedUsingTsExtension && shouldRewrite) {
const redirect = host.getResolvedProjectReferenceToRedirect(sourceFile.path);
if (redirect) {
const ignoreCase = !host.useCaseSensitiveFileNames();
const ownRootDir = host.getCommonSourceDirectory();
const otherRootDir = getCommonSourceDirectoryOfConfig(redirect.commandLine, ignoreCase);
const rootDirPath = getRelativePathFromDirectory(ownRootDir, otherRootDir, ignoreCase);
const outDirPath = getRelativePathFromDirectory(compilerOptions.outDir || ownRootDir, redirect.commandLine.options.outDir || otherRootDir, ignoreCase);
if (rootDirPath !== outDirPath) {
error(
errorNode,
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,
);
}
}
}
}

if (sourceFile.symbol) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3956,6 +3956,10 @@
"category": "Error",
"code": 2875
},
"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.": {
"category": "Error",
"code": 2876
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// === Syntax and Semantic Diagnostics ===
Syntactic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences1.ts':


==== /tests/cases/fourslash/server/packages/common/src/index.ts (0 errors) ====
export {};
==== /tests/cases/fourslash/server/packages/main/src/index.ts (0 errors) ====
import {} from "../../common/src/index.ts";

Semantic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences1.ts':
/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.


==== /tests/cases/fourslash/server/packages/common/src/index.ts (0 errors) ====
export {};
==== /tests/cases/fourslash/server/packages/main/src/index.ts (1 errors) ====
import {} from "../../common/src/index.ts";
~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! 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 number Diff line number Diff line change
@@ -0,0 +1,16 @@
// === Syntax and Semantic Diagnostics ===
Syntactic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences2.ts':


==== /tests/cases/fourslash/server/src/compiler/parser.ts (0 errors) ====
export {};
==== /tests/cases/fourslash/server/src/services/services.ts (0 errors) ====
import {} from "../compiler/parser.ts";

Semantic Diagnostics for file '/tests/cases/fourslash/server/rewriteRelativeImportExtensionsProjectReferences2.ts':


==== /tests/cases/fourslash/server/src/compiler/parser.ts (0 errors) ====
export {};
==== /tests/cases/fourslash/server/src/services/services.ts (0 errors) ====
import {} from "../compiler/parser.ts";
Loading

0 comments on commit 0d95ce2

Please sign in to comment.