Skip to content

Commit 5e9b070

Browse files
authored
Actually set impliedNodeFormat in more cases (microsoft#59479)
1 parent 3404817 commit 5e9b070

File tree

270 files changed

+6386
-600
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

270 files changed

+6386
-600
lines changed

src/compiler/program.ts

+7-10
Original file line numberDiff line numberDiff line change
@@ -1392,16 +1392,13 @@ export function getImpliedNodeFormatForFileWorker(
13921392
host: ModuleResolutionHost,
13931393
options: CompilerOptions,
13941394
) {
1395-
switch (getEmitModuleResolutionKind(options)) {
1396-
case ModuleResolutionKind.Node16:
1397-
case ModuleResolutionKind.NodeNext:
1398-
return fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Mjs]) ? ModuleKind.ESNext :
1399-
fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs]) ? ModuleKind.CommonJS :
1400-
fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Ts, Extension.Tsx, Extension.Js, Extension.Jsx]) ? lookupFromPackageJson() :
1401-
undefined; // other extensions, like `json` or `tsbuildinfo`, are set as `undefined` here but they should never be fed through the transformer pipeline
1402-
default:
1403-
return undefined;
1404-
}
1395+
const moduleResolution = getEmitModuleResolutionKind(options);
1396+
const shouldLookupFromPackageJson = ModuleResolutionKind.Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind.NodeNext
1397+
|| pathContainsNodeModules(fileName);
1398+
return fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Mjs]) ? ModuleKind.ESNext :
1399+
fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs]) ? ModuleKind.CommonJS :
1400+
shouldLookupFromPackageJson && fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Ts, Extension.Tsx, Extension.Js, Extension.Jsx]) ? lookupFromPackageJson() :
1401+
undefined; // other extensions, like `json` or `tsbuildinfo`, are set as `undefined` here but they should never be fed through the transformer pipeline
14051402

14061403
function lookupFromPackageJson(): Partial<CreateSourceFileOptions> {
14071404
const state = getTemporaryModuleResolutionState(packageJsonInfoCache, host, options);

tests/baselines/reference/allowJsCrossMonorepoPackage.trace.json

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@
7373
"File '/node_modules/pkg/index.d.ts' exists - use it as a name resolution result.",
7474
"Resolving real path for '/node_modules/pkg/index.d.ts', result '/node_modules/pkg/index.d.ts'.",
7575
"======== Module name 'pkg' was successfully resolved to '/node_modules/pkg/index.d.ts'. ========",
76+
"File '/node_modules/pkg/package.json' does not exist according to earlier cached lookups.",
77+
"File '/node_modules/package.json' does not exist.",
78+
"File '/package.json' does not exist.",
7679
"======== Resolving module '@typescript/lib-es5' from '/packages/main/__lib_node_modules_lookup_lib.es5.d.ts__.ts'. ========",
7780
"Explicitly specified module resolution kind: 'Node10'.",
7881
"Loading module '@typescript/lib-es5' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/bundlerConditionsExcludesNode(module=esnext).trace.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[
2+
"Found 'package.json' at '/node_modules/conditions/package.json'.",
3+
"File '/node_modules/conditions/package.json' exists according to earlier cached lookups.",
24
"======== Resolving module 'conditions' from '/main.ts'. ========",
35
"Explicitly specified module resolution kind: 'Bundler'.",
46
"Resolving in CJS mode with conditions 'import', 'types'.",
57
"File '/package.json' does not exist.",
68
"Loading module 'conditions' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
79
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
8-
"Found 'package.json' at '/node_modules/conditions/package.json'.",
10+
"File '/node_modules/conditions/package.json' exists according to earlier cached lookups.",
911
"Entering conditional exports.",
1012
"Saw non-matching condition 'node'.",
1113
"Matched 'exports' condition 'default'.",

tests/baselines/reference/bundlerConditionsExcludesNode(module=preserve).trace.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[
2+
"Found 'package.json' at '/node_modules/conditions/package.json'.",
3+
"File '/node_modules/conditions/package.json' exists according to earlier cached lookups.",
24
"======== Resolving module 'conditions' from '/main.ts'. ========",
35
"Explicitly specified module resolution kind: 'Bundler'.",
46
"Resolving in CJS mode with conditions 'import', 'types'.",
57
"File '/package.json' does not exist.",
68
"Loading module 'conditions' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
79
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
8-
"Found 'package.json' at '/node_modules/conditions/package.json'.",
10+
"File '/node_modules/conditions/package.json' exists according to earlier cached lookups.",
911
"Entering conditional exports.",
1012
"Saw non-matching condition 'node'.",
1113
"Matched 'exports' condition 'default'.",

tests/baselines/reference/bundlerImportESM(module=esnext).js

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { esm } from "./esm.mjs";
1616
//// [esm.mjs]
1717
export var esm = 0;
1818
//// [not-actually-cjs.cjs]
19-
export {};
19+
"use strict";
20+
Object.defineProperty(exports, "__esModule", { value: true });
2021
//// [still-not-cjs.js]
2122
export {};

tests/baselines/reference/bundlerNodeModules1(module=esnext).errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error TS6504: File '/node_modules/dual/index.cjs' is a JavaScript file. Did you
44
error TS6504: File '/node_modules/dual/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
55
The file is in the program because:
66
Root file specified for compilation
7-
/main.cts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.
7+
/main.cts(1,10): error TS2305: Module '"dual"' has no exported member 'esm'.
88
/main.mts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.
99
/main.ts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.
1010

@@ -54,6 +54,6 @@ error TS6504: File '/node_modules/dual/index.js' is a JavaScript file. Did you m
5454

5555
==== /main.cts (1 errors) ====
5656
import { esm, cjs } from "dual";
57-
~~~
58-
!!! error TS2305: Module '"dual"' has no exported member 'cjs'.
57+
~~~
58+
!!! error TS2305: Module '"dual"' has no exported member 'esm'.
5959

tests/baselines/reference/bundlerNodeModules1(module=esnext).js

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ export {};
4242
//// [main.mjs]
4343
export {};
4444
//// [main.cjs]
45-
export {};
45+
"use strict";
46+
Object.defineProperty(exports, "__esModule", { value: true });

tests/baselines/reference/bundlerNodeModules1(module=esnext).trace.json

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[
2+
"Found 'package.json' at '/node_modules/dual/package.json'.",
23
"======== Resolving module 'dual' from '/main.ts'. ========",
34
"Explicitly specified module resolution kind: 'Bundler'.",
45
"Resolving in CJS mode with conditions 'import', 'types'.",
56
"File '/package.json' does not exist.",
67
"Loading module 'dual' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
78
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
8-
"Found 'package.json' at '/node_modules/dual/package.json'.",
9+
"File '/node_modules/dual/package.json' exists according to earlier cached lookups.",
910
"Entering conditional exports.",
1011
"Matched 'exports' condition 'import'.",
1112
"Using 'exports' subpath '.' with target './index.js'.",
@@ -22,8 +23,23 @@
2223
"Resolution for module 'dual' was found in cache from location '/'.",
2324
"======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.ts' with Package ID 'dual/index.d.ts@1.0.0'. ========",
2425
"======== Resolving module 'dual' from '/main.cts'. ========",
25-
"Resolution for module 'dual' was found in cache from location '/'.",
26-
"======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.ts' with Package ID 'dual/index.d.ts@1.0.0'. ========",
26+
"Explicitly specified module resolution kind: 'Bundler'.",
27+
"Resolving in CJS mode with conditions 'require', 'types'.",
28+
"File '/package.json' does not exist according to earlier cached lookups.",
29+
"Loading module 'dual' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
30+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
31+
"File '/node_modules/dual/package.json' exists according to earlier cached lookups.",
32+
"Entering conditional exports.",
33+
"Saw non-matching condition 'import'.",
34+
"Matched 'exports' condition 'require'.",
35+
"Using 'exports' subpath '.' with target './index.cjs'.",
36+
"File name '/node_modules/dual/index.cjs' has a '.cjs' extension - stripping it.",
37+
"File '/node_modules/dual/index.cts' does not exist.",
38+
"File '/node_modules/dual/index.d.cts' exists - use it as a name resolution result.",
39+
"Resolved under condition 'require'.",
40+
"Exiting conditional exports.",
41+
"Resolving real path for '/node_modules/dual/index.d.cts', result '/node_modules/dual/index.d.cts'.",
42+
"======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.cts' with Package ID 'dual/index.d.cts@1.0.0'. ========",
2743
"======== Resolving module '@typescript/lib-es5' from '/.src/__lib_node_modules_lookup_lib.es5.d.ts__.ts'. ========",
2844
"Explicitly specified module resolution kind: 'Node10'.",
2945
"Loading module '@typescript/lib-es5' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/bundlerNodeModules1(module=esnext).types

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import { esm, cjs } from "dual";
2626

2727
=== /main.cts ===
2828
import { esm, cjs } from "dual";
29-
>esm : number
30-
> : ^^^^^^
31-
>cjs : any
29+
>esm : any
3230
> : ^^^
31+
>cjs : number
32+
> : ^^^^^^
3333

tests/baselines/reference/bundlerNodeModules1(module=preserve).errors.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error TS6504: File '/node_modules/dual/index.cjs' is a JavaScript file. Did you
44
error TS6504: File '/node_modules/dual/index.js' is a JavaScript file. Did you mean to enable the 'allowJs' option?
55
The file is in the program because:
66
Root file specified for compilation
7-
/main.cts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.
7+
/main.cts(1,10): error TS2305: Module '"dual"' has no exported member 'esm'.
88
/main.mts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.
99
/main.ts(1,15): error TS2305: Module '"dual"' has no exported member 'cjs'.
1010

@@ -54,6 +54,6 @@ error TS6504: File '/node_modules/dual/index.js' is a JavaScript file. Did you m
5454

5555
==== /main.cts (1 errors) ====
5656
import { esm, cjs } from "dual";
57-
~~~
58-
!!! error TS2305: Module '"dual"' has no exported member 'cjs'.
57+
~~~
58+
!!! error TS2305: Module '"dual"' has no exported member 'esm'.
5959

tests/baselines/reference/bundlerNodeModules1(module=preserve).trace.json

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[
2+
"Found 'package.json' at '/node_modules/dual/package.json'.",
23
"======== Resolving module 'dual' from '/main.ts'. ========",
34
"Explicitly specified module resolution kind: 'Bundler'.",
45
"Resolving in CJS mode with conditions 'import', 'types'.",
56
"File '/package.json' does not exist.",
67
"Loading module 'dual' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
78
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
8-
"Found 'package.json' at '/node_modules/dual/package.json'.",
9+
"File '/node_modules/dual/package.json' exists according to earlier cached lookups.",
910
"Entering conditional exports.",
1011
"Matched 'exports' condition 'import'.",
1112
"Using 'exports' subpath '.' with target './index.js'.",
@@ -22,8 +23,23 @@
2223
"Resolution for module 'dual' was found in cache from location '/'.",
2324
"======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.ts' with Package ID 'dual/index.d.ts@1.0.0'. ========",
2425
"======== Resolving module 'dual' from '/main.cts'. ========",
25-
"Resolution for module 'dual' was found in cache from location '/'.",
26-
"======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.ts' with Package ID 'dual/index.d.ts@1.0.0'. ========",
26+
"Explicitly specified module resolution kind: 'Bundler'.",
27+
"Resolving in CJS mode with conditions 'require', 'types'.",
28+
"File '/package.json' does not exist according to earlier cached lookups.",
29+
"Loading module 'dual' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
30+
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
31+
"File '/node_modules/dual/package.json' exists according to earlier cached lookups.",
32+
"Entering conditional exports.",
33+
"Saw non-matching condition 'import'.",
34+
"Matched 'exports' condition 'require'.",
35+
"Using 'exports' subpath '.' with target './index.cjs'.",
36+
"File name '/node_modules/dual/index.cjs' has a '.cjs' extension - stripping it.",
37+
"File '/node_modules/dual/index.cts' does not exist.",
38+
"File '/node_modules/dual/index.d.cts' exists - use it as a name resolution result.",
39+
"Resolved under condition 'require'.",
40+
"Exiting conditional exports.",
41+
"Resolving real path for '/node_modules/dual/index.d.cts', result '/node_modules/dual/index.d.cts'.",
42+
"======== Module name 'dual' was successfully resolved to '/node_modules/dual/index.d.cts' with Package ID 'dual/index.d.cts@1.0.0'. ========",
2743
"======== Resolving module '@typescript/lib-es5' from '/.src/__lib_node_modules_lookup_lib.es5.d.ts__.ts'. ========",
2844
"Explicitly specified module resolution kind: 'Node10'.",
2945
"Loading module '@typescript/lib-es5' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/bundlerNodeModules1(module=preserve).types

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import { esm, cjs } from "dual";
2626

2727
=== /main.cts ===
2828
import { esm, cjs } from "dual";
29-
>esm : number
30-
> : ^^^^^^
31-
>cjs : any
29+
>esm : any
3230
> : ^^^
31+
>cjs : number
32+
> : ^^^^^^
3333

tests/baselines/reference/cachedModuleResolution1.trace.json

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[
2+
"File '/a/b/node_modules/package.json' does not exist.",
3+
"File '/a/b/package.json' does not exist.",
4+
"File '/a/package.json' does not exist.",
5+
"File '/package.json' does not exist.",
26
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
37
"Explicitly specified module resolution kind: 'Node10'.",
48
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/cachedModuleResolution2.trace.json

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[
2+
"File '/a/b/node_modules/package.json' does not exist.",
3+
"File '/a/b/package.json' does not exist.",
4+
"File '/a/package.json' does not exist.",
5+
"File '/package.json' does not exist.",
26
"======== Resolving module 'foo' from '/a/b/c/lib.ts'. ========",
37
"Explicitly specified module resolution kind: 'Node10'.",
48
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/cachedModuleResolution5.trace.json

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
[
2+
"File '/a/b/node_modules/package.json' does not exist.",
3+
"File '/a/b/package.json' does not exist.",
4+
"File '/a/package.json' does not exist.",
5+
"File '/package.json' does not exist.",
26
"======== Resolving module 'foo' from '/a/b/c/d/e/app.ts'. ========",
37
"Explicitly specified module resolution kind: 'Node10'.",
48
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",

tests/baselines/reference/conditionalExportsResolutionFallback(moduleresolution=bundler).trace.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[
2+
"File '/node_modules/dep/dist/package.json' does not exist.",
3+
"Found 'package.json' at '/node_modules/dep/package.json'.",
24
"======== Resolving module 'dep' from '/index.mts'. ========",
35
"Explicitly specified module resolution kind: 'Bundler'.",
46
"Resolving in CJS mode with conditions 'import', 'types'.",
57
"File '/package.json' does not exist.",
68
"Loading module 'dep' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
79
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
8-
"Found 'package.json' at '/node_modules/dep/package.json'.",
10+
"File '/node_modules/dep/package.json' exists according to earlier cached lookups.",
911
"Entering conditional exports.",
1012
"Matched 'exports' condition 'import'.",
1113
"Using 'exports' subpath '.' with target './dist/index.mjs'.",

tests/baselines/reference/customConditions(resolvepackagejsonexports=false).trace.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[
2+
"Found 'package.json' at '/node_modules/lodash/package.json'.",
3+
"File '/node_modules/lodash/package.json' exists according to earlier cached lookups.",
4+
"File '/node_modules/lodash/package.json' exists according to earlier cached lookups.",
25
"======== Resolving module 'lodash' from '/index.ts'. ========",
36
"Explicitly specified module resolution kind: 'Bundler'.",
47
"Resolving in CJS mode with conditions 'import', 'types', 'webpack', ' browser'.",
58
"File '/package.json' does not exist.",
69
"Loading module 'lodash' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
710
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
8-
"Found 'package.json' at '/node_modules/lodash/package.json'.",
11+
"File '/node_modules/lodash/package.json' exists according to earlier cached lookups.",
912
"File '/node_modules/lodash.ts' does not exist.",
1013
"File '/node_modules/lodash.tsx' does not exist.",
1114
"File '/node_modules/lodash.d.ts' does not exist.",

tests/baselines/reference/customConditions(resolvepackagejsonexports=true).trace.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
[
2+
"Found 'package.json' at '/node_modules/lodash/package.json'.",
3+
"File '/node_modules/lodash/package.json' exists according to earlier cached lookups.",
4+
"File '/node_modules/lodash/package.json' exists according to earlier cached lookups.",
25
"======== Resolving module 'lodash' from '/index.ts'. ========",
36
"Explicitly specified module resolution kind: 'Bundler'.",
47
"Resolving in CJS mode with conditions 'import', 'types', 'webpack', ' browser'.",
58
"File '/package.json' does not exist.",
69
"Loading module 'lodash' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.",
710
"Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.",
8-
"Found 'package.json' at '/node_modules/lodash/package.json'.",
11+
"File '/node_modules/lodash/package.json' exists according to earlier cached lookups.",
912
"Entering conditional exports.",
1013
"Saw non-matching condition 'browser'.",
1114
"Matched 'exports' condition 'webpack'.",

tests/baselines/reference/duplicatePackage_relativeImportWithinPackage.trace.json

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"File '/node_modules/a/index.d.ts' exists - use it as a name resolution result.",
2525
"Resolving real path for '/node_modules/a/index.d.ts', result '/node_modules/a/index.d.ts'.",
2626
"======== Module name 'a' was successfully resolved to '/node_modules/a/index.d.ts'. ========",
27+
"File '/node_modules/foo/package.json' exists according to earlier cached lookups.",
2728
"======== Resolving module './index' from '/node_modules/foo/use.d.ts'. ========",
2829
"Module resolution kind is not specified, using 'Node10'.",
2930
"Loading module as file / folder, candidate module location '/node_modules/foo/index', target file types: TypeScript, Declaration.",
@@ -32,6 +33,10 @@
3233
"File '/node_modules/foo/index.d.ts' exists - use it as a name resolution result.",
3334
"File '/node_modules/foo/package.json' exists according to earlier cached lookups.",
3435
"======== Module name './index' was successfully resolved to '/node_modules/foo/index.d.ts' with Package ID 'foo/index.d.ts@1.2.3'. ========",
36+
"File '/node_modules/foo/package.json' exists according to earlier cached lookups.",
37+
"File '/node_modules/a/package.json' does not exist according to earlier cached lookups.",
38+
"File '/node_modules/package.json' does not exist.",
39+
"File '/package.json' does not exist.",
3540
"======== Resolving module 'foo' from '/node_modules/a/index.d.ts'. ========",
3641
"Module resolution kind is not specified, using 'Node10'.",
3742
"Loading module 'foo' from 'node_modules' folder, target file types: TypeScript, Declaration.",
@@ -50,6 +55,7 @@
5055
"'package.json' does not have a 'peerDependencies' field.",
5156
"Resolving real path for '/node_modules/a/node_modules/foo/index.d.ts', result '/node_modules/a/node_modules/foo/index.d.ts'.",
5257
"======== Module name 'foo' was successfully resolved to '/node_modules/a/node_modules/foo/index.d.ts' with Package ID 'foo/index.d.ts@1.2.3'. ========",
58+
"File '/node_modules/a/node_modules/foo/package.json' exists according to earlier cached lookups.",
5359
"======== Resolving module '@typescript/lib-es5' from '/.src/__lib_node_modules_lookup_lib.es5.d.ts__.ts'. ========",
5460
"Explicitly specified module resolution kind: 'Node10'.",
5561
"Loading module '@typescript/lib-es5' from 'node_modules' folder, target file types: TypeScript, Declaration.",

0 commit comments

Comments
 (0)