Skip to content

Commit ae7a1b4

Browse files
authored
Merge pull request #31541 from microsoft/packageId
Always use resolved file to figure out subModule name in package id
2 parents e601333 + 5d188a8 commit ae7a1b4

File tree

31 files changed

+443
-253
lines changed

31 files changed

+443
-253
lines changed

src/compiler/diagnosticMessages.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -3811,10 +3811,6 @@
38113811
"category": "Error",
38123812
"code": 6189
38133813
},
3814-
"Found 'package.json' at '{0}'. Package ID is '{1}'.": {
3815-
"category": "Message",
3816-
"code": 6190
3817-
},
38183814
"Whether to keep outdated console output in watch mode instead of clearing the screen.": {
38193815
"category": "Message",
38203816
"code": 6191
@@ -3923,6 +3919,14 @@
39233919
"category": "Message",
39243920
"code": 6217
39253921
},
3922+
"======== Module name '{0}' was successfully resolved to '{1}' with Package ID '{2}'. ========": {
3923+
"category": "Message",
3924+
"code": 6218
3925+
},
3926+
"======== Type reference directive '{0}' was successfully resolved to '{1}' with Package ID '{2}', primary: {3}. ========": {
3927+
"category": "Message",
3928+
"code": 6219
3929+
},
39263930

39273931
"Projects to reference": {
39283932
"category": "Message",
@@ -4975,7 +4979,7 @@
49754979
"code": 95079
49764980
},
49774981

4978-
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer." :{
4982+
"No value exists in scope for the shorthand property '{0}'. Either declare one or provide an initializer.": {
49794983
"category": "Error",
49804984
"code": 18004
49814985
},

src/compiler/moduleNameResolver.ts

+65-85
Large diffs are not rendered by default.

tests/baselines/reference/duplicatePackage_relativeImportWithinPackage.trace.json

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"======== Resolving module 'foo/use' from '/index.ts'. ========",
33
"Module resolution kind is not specified, using 'NodeJs'.",
44
"Loading module 'foo/use' from 'node_modules' folder, target file type 'TypeScript'.",
5+
"Found 'package.json' at '/node_modules/foo/package.json'.",
56
"'package.json' does not have a 'typesVersions' field.",
6-
"Found 'package.json' at '/node_modules/foo/package.json'. Package ID is 'foo/use/[email protected]'.",
77
"File '/node_modules/foo/use.ts' does not exist.",
88
"File '/node_modules/foo/use.tsx' does not exist.",
99
"File '/node_modules/foo/use.d.ts' exist - use it as a name resolution result.",
1010
"Resolving real path for '/node_modules/foo/use.d.ts', result '/node_modules/foo/use.d.ts'.",
11-
"======== Module name 'foo/use' was successfully resolved to '/node_modules/foo/use.d.ts'. ========",
11+
"======== Module name 'foo/use' was successfully resolved to '/node_modules/foo/use.d.ts' with Package ID 'foo/[email protected]'. ========",
1212
"======== Resolving module 'a' from '/index.ts'. ========",
1313
"Module resolution kind is not specified, using 'NodeJs'.",
1414
"Loading module 'a' from 'node_modules' folder, target file type 'TypeScript'.",
@@ -27,17 +27,14 @@
2727
"File '/node_modules/foo/index.ts' does not exist.",
2828
"File '/node_modules/foo/index.tsx' does not exist.",
2929
"File '/node_modules/foo/index.d.ts' exist - use it as a name resolution result.",
30+
"Found 'package.json' at '/node_modules/foo/package.json'.",
3031
"'package.json' does not have a 'typesVersions' field.",
31-
"Found 'package.json' at '/node_modules/foo/package.json'. Package ID is 'foo/[email protected]'.",
32-
"======== Module name './index' was successfully resolved to '/node_modules/foo/index.d.ts'. ========",
32+
"======== Module name './index' was successfully resolved to '/node_modules/foo/index.d.ts' with Package ID 'foo/[email protected]'. ========",
3333
"======== Resolving module 'foo' from '/node_modules/a/index.d.ts'. ========",
3434
"Module resolution kind is not specified, using 'NodeJs'.",
3535
"Loading module 'foo' from 'node_modules' folder, target file type 'TypeScript'.",
36-
"'package.json' does not have a 'typings' field.",
37-
"'package.json' does not have a 'types' field.",
38-
"'package.json' does not have a 'main' field.",
36+
"Found 'package.json' at '/node_modules/a/node_modules/foo/package.json'.",
3937
"'package.json' does not have a 'typesVersions' field.",
40-
"Found 'package.json' at '/node_modules/a/node_modules/foo/package.json'. Package ID is 'foo/[email protected]'.",
4138
"File '/node_modules/a/node_modules/foo.ts' does not exist.",
4239
"File '/node_modules/a/node_modules/foo.tsx' does not exist.",
4340
"File '/node_modules/a/node_modules/foo.d.ts' does not exist.",
@@ -48,5 +45,5 @@
4845
"File '/node_modules/a/node_modules/foo/index.tsx' does not exist.",
4946
"File '/node_modules/a/node_modules/foo/index.d.ts' exist - use it as a name resolution result.",
5047
"Resolving real path for '/node_modules/a/node_modules/foo/index.d.ts', result '/node_modules/a/node_modules/foo/index.d.ts'.",
51-
"======== Module name 'foo' was successfully resolved to '/node_modules/a/node_modules/foo/index.d.ts'. ========"
48+
"======== Module name 'foo' was successfully resolved to '/node_modules/a/node_modules/foo/index.d.ts' with Package ID 'foo/[email protected]'. ========"
5249
]

tests/baselines/reference/duplicatePackage_relativeImportWithinPackage_scoped.trace.json

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"======== Resolving module '@foo/bar/use' from '/index.ts'. ========",
33
"Module resolution kind is not specified, using 'NodeJs'.",
44
"Loading module '@foo/bar/use' from 'node_modules' folder, target file type 'TypeScript'.",
5+
"Found 'package.json' at '/node_modules/@foo/bar/package.json'.",
56
"'package.json' does not have a 'typesVersions' field.",
6-
"Found 'package.json' at '/node_modules/@foo/bar/package.json'. Package ID is '@foo/bar/use/[email protected]'.",
77
"File '/node_modules/@foo/bar/use.ts' does not exist.",
88
"File '/node_modules/@foo/bar/use.tsx' does not exist.",
99
"File '/node_modules/@foo/bar/use.d.ts' exist - use it as a name resolution result.",
1010
"Resolving real path for '/node_modules/@foo/bar/use.d.ts', result '/node_modules/@foo/bar/use.d.ts'.",
11-
"======== Module name '@foo/bar/use' was successfully resolved to '/node_modules/@foo/bar/use.d.ts'. ========",
11+
"======== Module name '@foo/bar/use' was successfully resolved to '/node_modules/@foo/bar/use.d.ts' with Package ID '@foo/bar/[email protected]'. ========",
1212
"======== Resolving module 'a' from '/index.ts'. ========",
1313
"Module resolution kind is not specified, using 'NodeJs'.",
1414
"Loading module 'a' from 'node_modules' folder, target file type 'TypeScript'.",
@@ -27,17 +27,14 @@
2727
"File '/node_modules/@foo/bar/index.ts' does not exist.",
2828
"File '/node_modules/@foo/bar/index.tsx' does not exist.",
2929
"File '/node_modules/@foo/bar/index.d.ts' exist - use it as a name resolution result.",
30+
"Found 'package.json' at '/node_modules/@foo/bar/package.json'.",
3031
"'package.json' does not have a 'typesVersions' field.",
31-
"Found 'package.json' at '/node_modules/@foo/bar/package.json'. Package ID is '@foo/bar/[email protected]'.",
32-
"======== Module name './index' was successfully resolved to '/node_modules/@foo/bar/index.d.ts'. ========",
32+
"======== Module name './index' was successfully resolved to '/node_modules/@foo/bar/index.d.ts' with Package ID '@foo/bar/[email protected]'. ========",
3333
"======== Resolving module '@foo/bar' from '/node_modules/a/index.d.ts'. ========",
3434
"Module resolution kind is not specified, using 'NodeJs'.",
3535
"Loading module '@foo/bar' from 'node_modules' folder, target file type 'TypeScript'.",
36-
"'package.json' does not have a 'typings' field.",
37-
"'package.json' does not have a 'types' field.",
38-
"'package.json' does not have a 'main' field.",
36+
"Found 'package.json' at '/node_modules/a/node_modules/@foo/bar/package.json'.",
3937
"'package.json' does not have a 'typesVersions' field.",
40-
"Found 'package.json' at '/node_modules/a/node_modules/@foo/bar/package.json'. Package ID is '@foo/bar/[email protected]'.",
4138
"File '/node_modules/a/node_modules/@foo/bar.ts' does not exist.",
4239
"File '/node_modules/a/node_modules/@foo/bar.tsx' does not exist.",
4340
"File '/node_modules/a/node_modules/@foo/bar.d.ts' does not exist.",
@@ -48,5 +45,5 @@
4845
"File '/node_modules/a/node_modules/@foo/bar/index.tsx' does not exist.",
4946
"File '/node_modules/a/node_modules/@foo/bar/index.d.ts' exist - use it as a name resolution result.",
5047
"Resolving real path for '/node_modules/a/node_modules/@foo/bar/index.d.ts', result '/node_modules/a/node_modules/@foo/bar/index.d.ts'.",
51-
"======== Module name '@foo/bar' was successfully resolved to '/node_modules/a/node_modules/@foo/bar/index.d.ts'. ========"
48+
"======== Module name '@foo/bar' was successfully resolved to '/node_modules/a/node_modules/@foo/bar/index.d.ts' with Package ID '@foo/bar/[email protected]'. ========"
5249
]

tests/baselines/reference/library-reference-10.trace.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
[
22
"======== Resolving type reference directive 'jquery', containing file '/foo/consumer.ts', root directory './types'. ========",
33
"Resolving with primary search path './types'.",
4-
"'package.json' has 'typings' field 'jquery.d.ts' that references 'types/jquery/jquery.d.ts'.",
5-
"'package.json' does not have a 'typesVersions' field.",
64
"Found 'package.json' at './types/jquery/package.json'.",
5+
"'package.json' does not have a 'typesVersions' field.",
76
"'package.json' has 'typings' field 'jquery.d.ts' that references 'types/jquery/jquery.d.ts'.",
87
"File 'types/jquery/jquery.d.ts' exist - use it as a name resolution result.",
98
"Resolving real path for 'types/jquery/jquery.d.ts', result '/foo/types/jquery/jquery.d.ts'.",
109
"======== Type reference directive 'jquery' was successfully resolved to '/foo/types/jquery/jquery.d.ts', primary: true. ========",
1110
"======== Resolving type reference directive 'jquery', containing file '/foo/__inferred type names__.ts', root directory './types'. ========",
1211
"Resolving with primary search path './types'.",
13-
"'package.json' has 'typings' field 'jquery.d.ts' that references 'types/jquery/jquery.d.ts'.",
14-
"'package.json' does not have a 'typesVersions' field.",
1512
"Found 'package.json' at './types/jquery/package.json'.",
13+
"'package.json' does not have a 'typesVersions' field.",
1614
"'package.json' has 'typings' field 'jquery.d.ts' that references 'types/jquery/jquery.d.ts'.",
1715
"File 'types/jquery/jquery.d.ts' exist - use it as a name resolution result.",
1816
"Resolving real path for 'types/jquery/jquery.d.ts', result '/foo/types/jquery/jquery.d.ts'.",

tests/baselines/reference/library-reference-11.trace.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
"Root directory cannot be determined, skipping primary search paths.",
44
"Looking up in 'node_modules' folder, initial location '/a/b'.",
55
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
6-
"'package.json' has 'typings' field 'jquery.d.ts' that references '/a/node_modules/jquery/jquery.d.ts'.",
7-
"'package.json' does not have a 'typesVersions' field.",
86
"Found 'package.json' at '/a/node_modules/jquery/package.json'.",
7+
"'package.json' does not have a 'typesVersions' field.",
98
"File '/a/node_modules/jquery.d.ts' does not exist.",
109
"'package.json' has 'typings' field 'jquery.d.ts' that references '/a/node_modules/jquery/jquery.d.ts'.",
1110
"File '/a/node_modules/jquery/jquery.d.ts' exist - use it as a name resolution result.",

tests/baselines/reference/library-reference-12.trace.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
"Root directory cannot be determined, skipping primary search paths.",
44
"Looking up in 'node_modules' folder, initial location '/a/b'.",
55
"Directory '/a/b/node_modules' does not exist, skipping all lookups in it.",
6-
"'package.json' does not have a 'typings' field.",
7-
"'package.json' has 'types' field 'dist/jquery.d.ts' that references '/a/node_modules/jquery/dist/jquery.d.ts'.",
8-
"'package.json' does not have a 'typesVersions' field.",
96
"Found 'package.json' at '/a/node_modules/jquery/package.json'.",
7+
"'package.json' does not have a 'typesVersions' field.",
108
"File '/a/node_modules/jquery.d.ts' does not exist.",
119
"'package.json' does not have a 'typings' field.",
1210
"'package.json' has 'types' field 'dist/jquery.d.ts' that references '/a/node_modules/jquery/dist/jquery.d.ts'.",

tests/baselines/reference/library-reference-2.trace.json

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
[
22
"======== Resolving type reference directive 'jquery', containing file '/consumer.ts', root directory '/types'. ========",
33
"Resolving with primary search path '/types'.",
4-
"'package.json' does not have a 'typings' field.",
5-
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
6-
"'package.json' does not have a 'typesVersions' field.",
74
"Found 'package.json' at '/types/jquery/package.json'.",
5+
"'package.json' does not have a 'typesVersions' field.",
86
"'package.json' does not have a 'typings' field.",
97
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
108
"File '/types/jquery/jquery.d.ts' exist - use it as a name resolution result.",
119
"Resolving real path for '/types/jquery/jquery.d.ts', result '/types/jquery/jquery.d.ts'.",
1210
"======== Type reference directive 'jquery' was successfully resolved to '/types/jquery/jquery.d.ts', primary: true. ========",
1311
"======== Resolving type reference directive 'jquery', containing file '/test/__inferred type names__.ts', root directory '/types'. ========",
1412
"Resolving with primary search path '/types'.",
15-
"'package.json' does not have a 'typings' field.",
16-
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
17-
"'package.json' does not have a 'typesVersions' field.",
1813
"Found 'package.json' at '/types/jquery/package.json'.",
14+
"'package.json' does not have a 'typesVersions' field.",
1915
"'package.json' does not have a 'typings' field.",
2016
"'package.json' has 'types' field 'jquery.d.ts' that references '/types/jquery/jquery.d.ts'.",
2117
"File '/types/jquery/jquery.d.ts' exist - use it as a name resolution result.",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//// [tests/cases/compiler/moduleResolutionPackageIdWithRelativeAndAbsolutePath.ts] ////
2+
3+
//// [package.json]
4+
{
5+
"name": "troublesome-lib",
6+
"version": "1.17.1"
7+
}
8+
//// [Compactable.d.ts]
9+
import { Option } from './Option';
10+
export class Compactable {
11+
option: Option;
12+
}
13+
//// [Option.d.ts]
14+
export class Option {
15+
someProperty: string;
16+
}
17+
//// [app.d.ts]
18+
import { Option } from "troublesome-lib/lib/Option";
19+
export class SharedOption extends Option { }
20+
export const makeSharedOption: () => SharedOption;
21+
//// [index.d.ts]
22+
import { Compactable } from "troublesome-lib/lib/Compactable"; // Including this will resolve Option as relative through the imports of compactable
23+
//// [package.json]
24+
{
25+
"name": "troublesome-lib",
26+
"version": "1.17.1"
27+
}
28+
//// [Compactable.d.ts]
29+
import { Option } from './Option';
30+
export class Compactable {
31+
option: Option;
32+
}
33+
//// [Option.d.ts]
34+
export class Option {
35+
someProperty: string;
36+
}
37+
//// [app.ts]
38+
import * as t from "anotherLib"; // Include the lib that recursively includes option as relative module resolution in this directory
39+
import { makeSharedOption } from "@shared/lib/app"; // Includes option as module in shared folder but as module in node_modules folder
40+
41+
42+
//// [/project/src/app.js]
43+
"use strict";
44+
exports.__esModule = true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
=== /project/src/app.ts ===
2+
import * as t from "anotherLib"; // Include the lib that recursively includes option as relative module resolution in this directory
3+
>t : Symbol(t, Decl(app.ts, 0, 6))
4+
5+
import { makeSharedOption } from "@shared/lib/app"; // Includes option as module in shared folder but as module in node_modules folder
6+
>makeSharedOption : Symbol(makeSharedOption, Decl(app.ts, 1, 8))
7+
8+
=== /shared/node_modules/troublesome-lib/lib/Option.d.ts ===
9+
export class Option {
10+
>Option : Symbol(Option, Decl(Option.d.ts, 0, 0))
11+
12+
someProperty: string;
13+
>someProperty : Symbol(Option.someProperty, Decl(Option.d.ts, 0, 21))
14+
}
15+
=== /shared/lib/app.d.ts ===
16+
import { Option } from "troublesome-lib/lib/Option";
17+
>Option : Symbol(Option, Decl(app.d.ts, 0, 8))
18+
19+
export class SharedOption extends Option { }
20+
>SharedOption : Symbol(SharedOption, Decl(app.d.ts, 0, 52))
21+
>Option : Symbol(Option, Decl(app.d.ts, 0, 8))
22+
23+
export const makeSharedOption: () => SharedOption;
24+
>makeSharedOption : Symbol(makeSharedOption, Decl(app.d.ts, 2, 12))
25+
>SharedOption : Symbol(SharedOption, Decl(app.d.ts, 0, 52))
26+
27+
=== /project/node_modules/anotherLib/index.d.ts ===
28+
import { Compactable } from "troublesome-lib/lib/Compactable"; // Including this will resolve Option as relative through the imports of compactable
29+
>Compactable : Symbol(Compactable, Decl(index.d.ts, 0, 8))
30+
31+
=== /project/node_modules/troublesome-lib/lib/Compactable.d.ts ===
32+
import { Option } from './Option';
33+
>Option : Symbol(Option, Decl(Compactable.d.ts, 0, 8))
34+
35+
export class Compactable {
36+
>Compactable : Symbol(Compactable, Decl(Compactable.d.ts, 0, 34))
37+
38+
option: Option;
39+
>option : Symbol(Compactable.option, Decl(Compactable.d.ts, 1, 26))
40+
>Option : Symbol(Option, Decl(Compactable.d.ts, 0, 8))
41+
}
42+
=== /project/node_modules/troublesome-lib/lib/Option.d.ts ===
43+
export class Option {
44+
>Option : Symbol(Option, Decl(Option.d.ts, 0, 0))
45+
46+
someProperty: string;
47+
>someProperty : Symbol(Option.someProperty, Decl(Option.d.ts, 0, 21))
48+
}

0 commit comments

Comments
 (0)