Skip to content

Commit 31de163

Browse files
authored
Do not mark #imports and self-name imports as external library imports in module resolution (microsoft#60228)
1 parent d61938d commit 31de163

19 files changed

+189
-179
lines changed

src/compiler/moduleNameResolver.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -1910,25 +1910,31 @@ function nodeModuleNameResolverWorker(
19101910
}
19111911

19121912
if (!isExternalModuleNameRelative(moduleName)) {
1913-
let resolved: SearchResult<Resolved> | undefined;
19141913
if (features & NodeResolutionFeatures.Imports && startsWith(moduleName, "#")) {
1915-
resolved = loadModuleFromImports(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
1916-
}
1917-
if (!resolved && features & NodeResolutionFeatures.SelfName) {
1918-
resolved = loadModuleFromSelfNameReference(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
1914+
const resolved = loadModuleFromImports(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
1915+
if (resolved) {
1916+
return resolved.value && { value: { resolved: resolved.value, isExternalLibraryImport: false } };
1917+
}
19191918
}
1920-
if (!resolved) {
1921-
if (moduleName.includes(":")) {
1922-
if (traceEnabled) {
1923-
trace(host, Diagnostics.Skipping_module_0_that_looks_like_an_absolute_URI_target_file_types_Colon_1, moduleName, formatExtensions(extensions));
1924-
}
1925-
return undefined;
1919+
1920+
if (features & NodeResolutionFeatures.SelfName) {
1921+
const resolved = loadModuleFromSelfNameReference(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
1922+
if (resolved) {
1923+
return resolved.value && { value: { resolved: resolved.value, isExternalLibraryImport: false } };
19261924
}
1925+
}
1926+
1927+
if (moduleName.includes(":")) {
19271928
if (traceEnabled) {
1928-
trace(host, Diagnostics.Loading_module_0_from_node_modules_folder_target_file_types_Colon_1, moduleName, formatExtensions(extensions));
1929+
trace(host, Diagnostics.Skipping_module_0_that_looks_like_an_absolute_URI_target_file_types_Colon_1, moduleName, formatExtensions(extensions));
19291930
}
1930-
resolved = loadModuleFromNearestNodeModulesDirectory(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
1931+
return undefined;
1932+
}
1933+
1934+
if (traceEnabled) {
1935+
trace(host, Diagnostics.Loading_module_0_from_node_modules_folder_target_file_types_Colon_1, moduleName, formatExtensions(extensions));
19311936
}
1937+
let resolved = loadModuleFromNearestNodeModulesDirectory(extensions, moduleName, containingDirectory, state, cache, redirectedReference);
19321938
if (extensions & Extensions.Declaration) {
19331939
resolved ??= resolveFromTypeRoot(moduleName, state);
19341940
}

tests/baselines/reference/nodeModulesAllowJsConditionalPackageExports(module=node16).js

+18-18
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,6 @@ export const cjsSource = true;
122122
}
123123
}
124124

125-
//// [index.js]
126-
// esm format file
127-
import * as cjs from "package/cjs";
128-
import * as mjs from "package/mjs";
129-
import * as type from "package";
130-
cjs;
131-
mjs;
132-
type;
133-
import * as cjsi from "inner/a";
134-
import * as mjsi from "inner/b";
135-
import * as typei from "inner";
136-
import * as ts from "inner/types";
137-
cjsi.mjsSource;
138-
mjsi.mjsSource;
139-
typei.mjsSource;
140-
ts.mjsSource;
141125
//// [index.mjs]
142126
// esm format file
143127
import * as cjs from "package/cjs";
@@ -195,11 +179,27 @@ cjsi.cjsSource;
195179
mjsi.cjsSource;
196180
typei.implicitCjsSource;
197181
ts.cjsSource;
182+
//// [index.js]
183+
// esm format file
184+
import * as cjs from "package/cjs";
185+
import * as mjs from "package/mjs";
186+
import * as type from "package";
187+
cjs;
188+
mjs;
189+
type;
190+
import * as cjsi from "inner/a";
191+
import * as mjsi from "inner/b";
192+
import * as typei from "inner";
193+
import * as ts from "inner/types";
194+
cjsi.mjsSource;
195+
mjsi.mjsSource;
196+
typei.mjsSource;
197+
ts.mjsSource;
198198

199199

200-
//// [index.d.ts]
201-
export {};
202200
//// [index.d.mts]
203201
export {};
204202
//// [index.d.cts]
205203
export {};
204+
//// [index.d.ts]
205+
export {};

tests/baselines/reference/nodeModulesAllowJsConditionalPackageExports(module=nodenext).js

+18-18
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,6 @@ export const cjsSource = true;
122122
}
123123
}
124124

125-
//// [index.js]
126-
// esm format file
127-
import * as cjs from "package/cjs";
128-
import * as mjs from "package/mjs";
129-
import * as type from "package";
130-
cjs;
131-
mjs;
132-
type;
133-
import * as cjsi from "inner/a";
134-
import * as mjsi from "inner/b";
135-
import * as typei from "inner";
136-
import * as ts from "inner/types";
137-
cjsi.mjsSource;
138-
mjsi.mjsSource;
139-
typei.mjsSource;
140-
ts.mjsSource;
141125
//// [index.mjs]
142126
// esm format file
143127
import * as cjs from "package/cjs";
@@ -195,11 +179,27 @@ cjsi.cjsSource;
195179
mjsi.cjsSource;
196180
typei.implicitCjsSource;
197181
ts.cjsSource;
182+
//// [index.js]
183+
// esm format file
184+
import * as cjs from "package/cjs";
185+
import * as mjs from "package/mjs";
186+
import * as type from "package";
187+
cjs;
188+
mjs;
189+
type;
190+
import * as cjsi from "inner/a";
191+
import * as mjsi from "inner/b";
192+
import * as typei from "inner";
193+
import * as ts from "inner/types";
194+
cjsi.mjsSource;
195+
mjsi.mjsSource;
196+
typei.mjsSource;
197+
ts.mjsSource;
198198

199199

200-
//// [index.d.ts]
201-
export {};
202200
//// [index.d.mts]
203201
export {};
204202
//// [index.d.cts]
205203
export {};
204+
//// [index.d.ts]
205+
export {};

tests/baselines/reference/nodeModulesAllowJsPackageExports(module=node16).js

+16-16
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,6 @@ export { type };
8888
}
8989
}
9090

91-
//// [index.js]
92-
// esm format file
93-
import * as cjs from "package/cjs";
94-
import * as mjs from "package/mjs";
95-
import * as type from "package";
96-
cjs;
97-
mjs;
98-
type;
99-
import * as cjsi from "inner/cjs";
100-
import * as mjsi from "inner/mjs";
101-
import * as typei from "inner";
102-
cjsi;
103-
mjsi;
104-
typei;
10591
//// [index.mjs]
10692
// esm format file
10793
import * as cjs from "package/cjs";
@@ -155,11 +141,25 @@ const typei = __importStar(require("inner"));
155141
cjsi;
156142
mjsi;
157143
typei;
144+
//// [index.js]
145+
// esm format file
146+
import * as cjs from "package/cjs";
147+
import * as mjs from "package/mjs";
148+
import * as type from "package";
149+
cjs;
150+
mjs;
151+
type;
152+
import * as cjsi from "inner/cjs";
153+
import * as mjsi from "inner/mjs";
154+
import * as typei from "inner";
155+
cjsi;
156+
mjsi;
157+
typei;
158158

159159

160-
//// [index.d.ts]
161-
export {};
162160
//// [index.d.mts]
163161
export {};
164162
//// [index.d.cts]
165163
export {};
164+
//// [index.d.ts]
165+
export {};

tests/baselines/reference/nodeModulesAllowJsPackageExports(module=nodenext).js

+16-16
Original file line numberDiff line numberDiff line change
@@ -88,20 +88,6 @@ export { type };
8888
}
8989
}
9090

91-
//// [index.js]
92-
// esm format file
93-
import * as cjs from "package/cjs";
94-
import * as mjs from "package/mjs";
95-
import * as type from "package";
96-
cjs;
97-
mjs;
98-
type;
99-
import * as cjsi from "inner/cjs";
100-
import * as mjsi from "inner/mjs";
101-
import * as typei from "inner";
102-
cjsi;
103-
mjsi;
104-
typei;
10591
//// [index.mjs]
10692
// esm format file
10793
import * as cjs from "package/cjs";
@@ -155,11 +141,25 @@ const typei = __importStar(require("inner"));
155141
cjsi;
156142
mjsi;
157143
typei;
144+
//// [index.js]
145+
// esm format file
146+
import * as cjs from "package/cjs";
147+
import * as mjs from "package/mjs";
148+
import * as type from "package";
149+
cjs;
150+
mjs;
151+
type;
152+
import * as cjsi from "inner/cjs";
153+
import * as mjsi from "inner/mjs";
154+
import * as typei from "inner";
155+
cjsi;
156+
mjsi;
157+
typei;
158158

159159

160-
//// [index.d.ts]
161-
export {};
162160
//// [index.d.mts]
163161
export {};
164162
//// [index.d.cts]
165163
export {};
164+
//// [index.d.ts]
165+
export {};

tests/baselines/reference/nodeModulesAllowJsPackageImports(module=node16).js

+10-10
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ type;
3737
}
3838
}
3939

40-
//// [index.js]
41-
// esm format file
42-
import * as cjs from "#cjs";
43-
import * as mjs from "#mjs";
44-
import * as type from "#type";
45-
cjs;
46-
mjs;
47-
type;
4840
//// [index.mjs]
4941
// esm format file
5042
import * as cjs from "#cjs";
@@ -86,11 +78,19 @@ const type = __importStar(require("#type"));
8678
cjs;
8779
mjs;
8880
type;
81+
//// [index.js]
82+
// esm format file
83+
import * as cjs from "#cjs";
84+
import * as mjs from "#mjs";
85+
import * as type from "#type";
86+
cjs;
87+
mjs;
88+
type;
8989

9090

91-
//// [index.d.ts]
92-
export {};
9391
//// [index.d.mts]
9492
export {};
9593
//// [index.d.cts]
9694
export {};
95+
//// [index.d.ts]
96+
export {};

tests/baselines/reference/nodeModulesAllowJsPackageImports(module=nodenext).js

+10-10
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ type;
3737
}
3838
}
3939

40-
//// [index.js]
41-
// esm format file
42-
import * as cjs from "#cjs";
43-
import * as mjs from "#mjs";
44-
import * as type from "#type";
45-
cjs;
46-
mjs;
47-
type;
4840
//// [index.mjs]
4941
// esm format file
5042
import * as cjs from "#cjs";
@@ -86,11 +78,19 @@ const type = __importStar(require("#type"));
8678
cjs;
8779
mjs;
8880
type;
81+
//// [index.js]
82+
// esm format file
83+
import * as cjs from "#cjs";
84+
import * as mjs from "#mjs";
85+
import * as type from "#type";
86+
cjs;
87+
mjs;
88+
type;
8989

9090

91-
//// [index.d.ts]
92-
export {};
9391
//// [index.d.mts]
9492
export {};
9593
//// [index.d.cts]
9694
export {};
95+
//// [index.d.ts]
96+
export {};

tests/baselines/reference/nodeModulesPackageImports(module=node16).trace.json

-24
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"Using 'imports' subpath '#cjs' with target './index.cjs'.",
88
"File name '/.src/index.cjs' has a '.cjs' extension - stripping it.",
99
"File '/.src/index.cts' exists - use it as a name resolution result.",
10-
"Resolving real path for '/.src/index.cts', result '/.src/index.cts'.",
1110
"======== Module name '#cjs' was successfully resolved to '/.src/index.cts'. ========",
1211
"======== Resolving module '#mjs' from '/.src/index.ts'. ========",
1312
"Module resolution kind is not specified, using 'Node16'.",
@@ -16,7 +15,6 @@
1615
"Using 'imports' subpath '#mjs' with target './index.mjs'.",
1716
"File name '/.src/index.mjs' has a '.mjs' extension - stripping it.",
1817
"File '/.src/index.mts' exists - use it as a name resolution result.",
19-
"Resolving real path for '/.src/index.mts', result '/.src/index.mts'.",
2018
"======== Module name '#mjs' was successfully resolved to '/.src/index.mts'. ========",
2119
"======== Resolving module '#type' from '/.src/index.ts'. ========",
2220
"Module resolution kind is not specified, using 'Node16'.",
@@ -25,7 +23,6 @@
2523
"Using 'imports' subpath '#type' with target './index.js'.",
2624
"File name '/.src/index.js' has a '.js' extension - stripping it.",
2725
"File '/.src/index.ts' exists - use it as a name resolution result.",
28-
"Resolving real path for '/.src/index.ts', result '/.src/index.ts'.",
2926
"======== Module name '#type' was successfully resolved to '/.src/index.ts'. ========",
3027
"======== Resolving module '#cjs' from '/.src/index.cts'. ========",
3128
"Module resolution kind is not specified, using 'Node16'.",
@@ -34,7 +31,6 @@
3431
"Using 'imports' subpath '#cjs' with target './index.cjs'.",
3532
"File name '/.src/index.cjs' has a '.cjs' extension - stripping it.",
3633
"File '/.src/index.cts' exists - use it as a name resolution result.",
37-
"Resolving real path for '/.src/index.cts', result '/.src/index.cts'.",
3834
"======== Module name '#cjs' was successfully resolved to '/.src/index.cts'. ========",
3935
"======== Resolving module '#mjs' from '/.src/index.cts'. ========",
4036
"Module resolution kind is not specified, using 'Node16'.",
@@ -43,7 +39,6 @@
4339
"Using 'imports' subpath '#mjs' with target './index.mjs'.",
4440
"File name '/.src/index.mjs' has a '.mjs' extension - stripping it.",
4541
"File '/.src/index.mts' exists - use it as a name resolution result.",
46-
"Resolving real path for '/.src/index.mts', result '/.src/index.mts'.",
4742
"======== Module name '#mjs' was successfully resolved to '/.src/index.mts'. ========",
4843
"======== Resolving module '#type' from '/.src/index.cts'. ========",
4944
"Module resolution kind is not specified, using 'Node16'.",
@@ -52,7 +47,6 @@
5247
"Using 'imports' subpath '#type' with target './index.js'.",
5348
"File name '/.src/index.js' has a '.js' extension - stripping it.",
5449
"File '/.src/index.ts' exists - use it as a name resolution result.",
55-
"Resolving real path for '/.src/index.ts', result '/.src/index.ts'.",
5650
"======== Module name '#type' was successfully resolved to '/.src/index.ts'. ========",
5751
"======== Resolving module '#cjs' from '/.src/index.mts'. ========",
5852
"Resolution for module '#cjs' was found in cache from location '/.src'.",
@@ -63,24 +57,6 @@
6357
"======== Resolving module '#type' from '/.src/index.mts'. ========",
6458
"Resolution for module '#type' was found in cache from location '/.src'.",
6559
"======== Module name '#type' was successfully resolved to '/.src/index.ts'. ========",
66-
"======== Resolving module '#cjs' from '/.src/index.mts'. ========",
67-
"Resolution for module '#cjs' was found in cache from location '/.src'.",
68-
"======== Module name '#cjs' was successfully resolved to '/.src/index.cts'. ========",
69-
"======== Resolving module '#mjs' from '/.src/index.mts'. ========",
70-
"Resolution for module '#mjs' was found in cache from location '/.src'.",
71-
"======== Module name '#mjs' was successfully resolved to '/.src/index.mts'. ========",
72-
"======== Resolving module '#type' from '/.src/index.mts'. ========",
73-
"Resolution for module '#type' was found in cache from location '/.src'.",
74-
"======== Module name '#type' was successfully resolved to '/.src/index.ts'. ========",
75-
"======== Resolving module '#cjs' from '/.src/index.cts'. ========",
76-
"Resolution for module '#cjs' was found in cache from location '/.src'.",
77-
"======== Module name '#cjs' was successfully resolved to '/.src/index.cts'. ========",
78-
"======== Resolving module '#mjs' from '/.src/index.cts'. ========",
79-
"Resolution for module '#mjs' was found in cache from location '/.src'.",
80-
"======== Module name '#mjs' was successfully resolved to '/.src/index.mts'. ========",
81-
"======== Resolving module '#type' from '/.src/index.cts'. ========",
82-
"Resolution for module '#type' was found in cache from location '/.src'.",
83-
"======== Module name '#type' was successfully resolved to '/.src/index.ts'. ========",
8460
"File '/.ts/package.json' does not exist.",
8561
"File '/package.json' does not exist.",
8662
"======== Resolving module '@typescript/lib-es2022' from '/.src/__lib_node_modules_lookup_lib.es2022.d.ts__.ts'. ========",

0 commit comments

Comments
 (0)