Skip to content

Commit e9fde2c

Browse files
Copilotandrewbranch
andcommitted
Fix: debug and implement correct diagnostic logic for unsafe import rewrites
The diagnostic for error TS2876 was not being triggered because the rewrite check logic was placed outside the `errorNode != nil` block. Moving it inside the error handling block and fixing the conditional logic now correctly detects when a module specifier looks like a file name but resolves to a different location, triggering the appropriate "unsafe to rewrite" error. Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
1 parent 144f55f commit e9fde2c

File tree

5 files changed

+72
-50
lines changed

5 files changed

+72
-50
lines changed

internal/checker/checker.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14497,35 +14497,35 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1449714497
tsExtension,
1449814498
)
1449914499
}
14500+
} else if c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
14501+
location.Flags&ast.NodeFlagsAmbient == 0 &&
14502+
!tspath.IsDeclarationFileName(moduleReference) &&
14503+
!ast.IsLiteralImportTypeNode(location) &&
14504+
!ast.IsPartOfTypeOnlyImportOrExportDeclaration(location) {
14505+
shouldRewrite := c.shouldRewriteModuleSpecifier(moduleReference)
14506+
if !resolvedModule.ResolvedUsingTsExtension && shouldRewrite {
14507+
relativeToSourceFile := tspath.GetRelativePathFromDirectory(
14508+
tspath.GetDirectoryPath(tspath.GetNormalizedAbsolutePath(importingSourceFile.FileName(), c.program.GetCurrentDirectory())),
14509+
resolvedModule.ResolvedFileName,
14510+
tspath.ComparePathsOptions{
14511+
UseCaseSensitiveFileNames: c.program.UseCaseSensitiveFileNames(),
14512+
CurrentDirectory: c.program.GetCurrentDirectory(),
14513+
},
14514+
)
14515+
c.error(
14516+
errorNode,
14517+
diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_looks_like_a_file_name_but_actually_resolves_to_0,
14518+
relativeToSourceFile,
14519+
)
14520+
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && c.sourceFileMayBeEmitted(sourceFile) {
14521+
c.error(
14522+
errorNode,
14523+
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,
14524+
tspath.GetAnyExtensionFromPath(moduleReference, nil, false),
14525+
)
14526+
}
14527+
// TODO: Add project reference check when GetResolvedProjectReferenceToRedirect is implemented
1450014528
}
14501-
} else if c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
14502-
location.Flags&ast.NodeFlagsAmbient == 0 &&
14503-
!tspath.IsDeclarationFileName(moduleReference) &&
14504-
!ast.IsLiteralImportTypeNode(location) &&
14505-
!ast.IsPartOfTypeOnlyImportOrExportDeclaration(location) {
14506-
shouldRewrite := c.shouldRewriteModuleSpecifier(moduleReference)
14507-
if !resolvedModule.ResolvedUsingTsExtension && shouldRewrite {
14508-
relativeToSourceFile := tspath.GetRelativePathFromDirectory(
14509-
tspath.GetDirectoryPath(tspath.GetNormalizedAbsolutePath(importingSourceFile.FileName(), c.program.GetCurrentDirectory())),
14510-
resolvedModule.ResolvedFileName,
14511-
tspath.ComparePathsOptions{
14512-
UseCaseSensitiveFileNames: c.program.UseCaseSensitiveFileNames(),
14513-
CurrentDirectory: c.program.GetCurrentDirectory(),
14514-
},
14515-
)
14516-
c.error(
14517-
errorNode,
14518-
diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_looks_like_a_file_name_but_actually_resolves_to_0,
14519-
relativeToSourceFile,
14520-
)
14521-
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && c.sourceFileMayBeEmitted(sourceFile) {
14522-
c.error(
14523-
errorNode,
14524-
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,
14525-
tspath.GetAnyExtensionFromPath(moduleReference, nil, false),
14526-
)
14527-
}
14528-
// TODO: Add project reference check when GetResolvedProjectReferenceToRedirect is implemented
1452914529
}
1453014530

1453114531
if sourceFile.Symbol != nil {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
2+
3+
4+
==== index.ts (1 errors) ====
5+
import foo = require("./foo.ts"); // Error
6+
~~~~~~~~~~
7+
!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
8+
import type _foo = require("./foo.ts"); // Ok
9+
10+
==== foo.ts/index.ts (0 errors) ====
11+
export = {};
12+

testdata/baselines/reference/submodule/conformance/cjsErrors(module=node18).errors.txt.diff

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
+++ new.cjsErrors(module=node18).errors.txt
33
@@= skipped -0, +0 lines =@@
44
-index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
5-
-
6-
-
7-
-==== index.ts (1 errors) ====
8-
- import foo = require("./foo.ts"); // Error
9-
- ~~~~~~~~~~
5+
+index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
6+
7+
8+
==== index.ts (1 errors) ====
9+
import foo = require("./foo.ts"); // Error
10+
~~~~~~~~~~
1011
-!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
11-
- import type _foo = require("./foo.ts"); // Ok
12-
-
13-
-==== foo.ts/index.ts (0 errors) ====
14-
- export = {};
15-
-
16-
+<no content>
12+
+!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
13+
import type _foo = require("./foo.ts"); // Ok
14+
15+
==== foo.ts/index.ts (0 errors) ====
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
2+
3+
4+
==== index.ts (1 errors) ====
5+
import foo = require("./foo.ts"); // Error
6+
~~~~~~~~~~
7+
!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
8+
import type _foo = require("./foo.ts"); // Ok
9+
10+
==== foo.ts/index.ts (0 errors) ====
11+
export = {};
12+

testdata/baselines/reference/submodule/conformance/cjsErrors(module=nodenext).errors.txt.diff

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
+++ new.cjsErrors(module=nodenext).errors.txt
33
@@= skipped -0, +0 lines =@@
44
-index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
5-
-
6-
-
7-
-==== index.ts (1 errors) ====
8-
- import foo = require("./foo.ts"); // Error
9-
- ~~~~~~~~~~
5+
+index.ts(1,22): error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
6+
7+
8+
==== index.ts (1 errors) ====
9+
import foo = require("./foo.ts"); // Error
10+
~~~~~~~~~~
1011
-!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "./foo.ts/index.ts".
11-
- import type _foo = require("./foo.ts"); // Ok
12-
-
13-
-==== foo.ts/index.ts (0 errors) ====
14-
- export = {};
15-
-
16-
+<no content>
12+
+!!! error TS2876: This relative import path is unsafe to rewrite because it looks like a file name, but actually resolves to "foo.ts/index.ts".
13+
import type _foo = require("./foo.ts"); // Ok
14+
15+
==== foo.ts/index.ts (0 errors) ====

0 commit comments

Comments
 (0)