Skip to content

Prepare module resolution for resolving TS extensions #51171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e9f0a1a
WIP
andrewbranch Jul 28, 2022
5f9cf9d
Add extension error back unless noEmit is set
andrewbranch Jul 29, 2022
2421cf5
Add non-relative tests
andrewbranch Jul 29, 2022
660026e
Add error for importing from declaration file
andrewbranch Jul 29, 2022
2daa136
Merge branch 'main' into module-resolution/minimal
andrewbranch Sep 14, 2022
f49a368
Update unit test
andrewbranch Sep 14, 2022
af03675
Add explicit flag for importing from .ts extensions
andrewbranch Sep 14, 2022
496ef99
Add module specifier resolution changes
andrewbranch Sep 15, 2022
458c125
Add auto-import tests
andrewbranch Sep 19, 2022
7d64928
Disallow relative imports into node_modules
andrewbranch Sep 26, 2022
0249e0a
Ensure auto-imports don’t suggest ./node_modules;
andrewbranch Sep 26, 2022
6242ee3
Test a non-portable declaration emit issue
andrewbranch Sep 26, 2022
6cc0e18
Test auto-importing TSX file
andrewbranch Sep 26, 2022
240533a
Update path completions
andrewbranch Sep 26, 2022
eb9a4bb
Merge branch 'main' into module-resolution/minimal
andrewbranch Sep 26, 2022
619843e
Fix lint due to merge
andrewbranch Sep 27, 2022
2ceb4bc
Remove minimal-specific stuff
andrewbranch Oct 13, 2022
aad7fe7
Remove minimal tests
andrewbranch Oct 13, 2022
7fd3e72
Update unit tests
andrewbranch Oct 13, 2022
7cca6f3
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Oct 14, 2022
ea12012
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Oct 25, 2022
8655cf9
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 7, 2022
c55c454
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 8, 2022
6e1989e
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 15, 2022
fa188b1
Revamp string completions ending preferences
andrewbranch Nov 18, 2022
c6b963b
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 18, 2022
7225671
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 28, 2022
3b1d554
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 5, 2022
5909643
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 5, 2022
c3575de
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 6, 2022
c4ba4a5
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 9, 2022
4e73a46
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Disallow relative imports into node_modules
  • Loading branch information
andrewbranch committed Sep 26, 2022
commit 7d64928117dbad08fa63888118d7b5cbcde9cde4
5 changes: 4 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3740,7 +3740,10 @@ namespace ts {
const moduleResolutionKind = getEmitModuleResolutionKind(compilerOptions);
const resolutionIsNode16OrNext = moduleResolutionKind === ModuleResolutionKind.Node16 ||
moduleResolutionKind === ModuleResolutionKind.NodeNext;
if (tsExtension) {
if (moduleResolutionKind === ModuleResolutionKind.Minimal && pathContainsNodeModules(moduleReference)) {
error(errorNode, Diagnostics.Relative_imports_into_node_modules_are_not_allowed);
}
else if (tsExtension) {
errorOnTSExtensionImport(tsExtension);
}
else if (!compilerOptions.resolveJsonModule &&
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3571,6 +3571,10 @@
"category": "Error",
"code": 2845
},
"Relative imports into 'node_modules' are not allowed.": {
"category": "Error",
"code": 2846
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down
13 changes: 13 additions & 0 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2705,6 +2705,19 @@ namespace ts {
if (resolvedUsingSettings) {
return resolvedUsingSettings;
}
if (isExternalModuleNameRelative(moduleName) && pathContainsNodeModules(moduleName)) {
// A relative import into node_modules is a problem for a few reasons:
// 1. Portability - if the code gets published as a library, it will very likely break
// 2. It's unclear how we should resolve types. By typical relative import rules, we
// would ignore package.json fields that tell us where to find types and would have
// no special behavior linking up node_modules/@types with their implementations -
// we would only find .d.ts files as siblings of .js files. Any package that puts
// their types in a separate directory, or is typed by @types, would be broken.
// Some of these redirections would be safe to do, but others might reflect
// Node-specific resolution features that would only work with non-relative imports.
// There's a diagnostic issued for this case in the checker.
return noPackageId(/*resolved*/ undefined);
}
const resolvedRelative = loadModuleFromFileNoImplicitExtensions(extensions, candidate, /*onlyRecordFailures*/ false, state);
if (resolvedRelative) {
return noPackageId(resolvedRelative);
Expand Down
26 changes: 26 additions & 0 deletions tests/baselines/reference/minimal_nodeModules.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/main.ts(1,16): error TS2307: Cannot find module 'foo' or its corresponding type declarations.
/main.ts(2,16): error TS2846: Relative imports into 'node_modules' are not allowed.
/main.ts(3,21): error TS2846: Relative imports into 'node_modules' are not allowed.


==== /node_modules/foo/index.d.ts (0 errors) ====
import {} from "./other.js";
export {};

==== /node_modules/foo/other.d.ts (0 errors) ====
export {};

==== /node_modules/@types/foo/index.d.ts (0 errors) ====
export {};

==== /main.ts (3 errors) ====
import {} from "foo";
~~~~~
!!! error TS2307: Cannot find module 'foo' or its corresponding type declarations.
import {} from "./node_modules/foo/index.js";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2846: Relative imports into 'node_modules' are not allowed.
import type {} from "./node_modules/@types/foo/index.d.ts";
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2846: Relative imports into 'node_modules' are not allowed.

21 changes: 21 additions & 0 deletions tests/baselines/reference/minimal_nodeModules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/conformance/moduleResolution/minimal_nodeModules.ts] ////

//// [index.d.ts]
import {} from "./other.js";
export {};

//// [other.d.ts]
export {};

//// [index.d.ts]
export {};

//// [main.ts]
import {} from "foo";
import {} from "./node_modules/foo/index.js";
import type {} from "./node_modules/@types/foo/index.d.ts";


//// [main.js]
"use strict";
exports.__esModule = true;
16 changes: 16 additions & 0 deletions tests/baselines/reference/minimal_nodeModules.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== /node_modules/foo/index.d.ts ===
import {} from "./other.js";
No type information for this code.export {};
No type information for this code.
No type information for this code.=== /node_modules/foo/other.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /node_modules/@types/foo/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /main.ts ===
import {} from "foo";
No type information for this code.import {} from "./node_modules/foo/index.js";
No type information for this code.import type {} from "./node_modules/@types/foo/index.d.ts";
No type information for this code.
No type information for this code.
16 changes: 16 additions & 0 deletions tests/baselines/reference/minimal_nodeModules.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== /node_modules/foo/index.d.ts ===
import {} from "./other.js";
No type information for this code.export {};
No type information for this code.
No type information for this code.=== /node_modules/foo/other.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /node_modules/@types/foo/index.d.ts ===
export {};
No type information for this code.
No type information for this code.=== /main.ts ===
import {} from "foo";
No type information for this code.import {} from "./node_modules/foo/index.js";
No type information for this code.import type {} from "./node_modules/@types/foo/index.d.ts";
No type information for this code.
No type information for this code.
16 changes: 16 additions & 0 deletions tests/cases/conformance/moduleResolution/minimal_nodeModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @moduleResolution: minimal

// @Filename: /node_modules/foo/index.d.ts
import {} from "./other.js";
export {};

// @Filename: /node_modules/foo/other.d.ts
export {};

// @Filename: /node_modules/@types/foo/index.d.ts
export {};

// @Filename: /main.ts
import {} from "foo";
import {} from "./node_modules/foo/index.js";
import type {} from "./node_modules/@types/foo/index.d.ts";
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy