Skip to content

Commit 3b218d1

Browse files
committed
Use correct options for parsing paths
1 parent 49f5e24 commit 3b218d1

File tree

3 files changed

+36
-88
lines changed

3 files changed

+36
-88
lines changed

internal/module/resolver.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"slices"
66
"strings"
7+
"sync"
78

89
"github.com/microsoft/typescript-go/internal/ast"
910
"github.com/microsoft/typescript-go/internal/collections"
@@ -77,6 +78,12 @@ type resolutionState struct {
7778
failedLookupLocations []string
7879
affectingLocations []string
7980
diagnostics []*ast.Diagnostic
81+
82+
// Similar to whats on resolver but only done if compilerOptions are for project reference redirect
83+
// Cached representation for `core.CompilerOptions.paths`.
84+
// Doesn't handle other path patterns like in `typesVersions`.
85+
parsedPatternsForPathsOnce sync.Once
86+
parsedPatternsForPaths *ParsedPatterns
8087
}
8188

8289
func newResolutionState(
@@ -1117,6 +1124,16 @@ func (r *resolutionState) tryLoadModuleUsingOptionalResolutionSettings() *resolv
11171124
}
11181125
}
11191126

1127+
func (r *resolutionState) getParsedPatternsForPaths() *ParsedPatterns {
1128+
if r.compilerOptions == r.resolver.compilerOptions {
1129+
return r.resolver.getParsedPatternsForPaths()
1130+
}
1131+
r.parsedPatternsForPathsOnce.Do(func() {
1132+
r.parsedPatternsForPaths = TryParsePatterns(r.compilerOptions.Paths)
1133+
})
1134+
return r.parsedPatternsForPaths
1135+
}
1136+
11201137
func (r *resolutionState) tryLoadModuleUsingPathsIfEligible() *resolved {
11211138
if r.compilerOptions.Paths.Size() > 0 && !tspath.PathIsRelative(r.name) {
11221139
if r.tracer != nil {
@@ -1126,7 +1143,7 @@ func (r *resolutionState) tryLoadModuleUsingPathsIfEligible() *resolved {
11261143
return continueSearching()
11271144
}
11281145
baseDirectory := r.compilerOptions.GetPathsBasePath(r.resolver.host.GetCurrentDirectory())
1129-
pathPatterns := r.resolver.getParsedPatternsForPaths()
1146+
pathPatterns := r.getParsedPatternsForPaths()
11301147
return r.tryLoadModuleUsingPaths(
11311148
r.extensions,
11321149
r.name,

testdata/baselines/reference/tsbuild/moduleResolution/resolves-specifier-in-output-declaration-file-from-referenced-project-correctly-with-preserveSymlinks.js

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type { TheNum } from 'const';
3636
}
3737

3838
tsgo -b packages/pkg1 --verbose --traceResolution
39-
ExitStatus:: DiagnosticsPresent_OutputsGenerated
39+
ExitStatus:: Success
4040
Output::
4141
[HH:MM:SS AM] Projects in this build:
4242
* packages/pkg2/tsconfig.json
@@ -94,44 +94,11 @@ Using compiler options of project reference redirect '/user/username/projects/my
9494
Module resolution kind is not specified, using 'Bundler'.
9595
Resolving in CJS mode with conditions 'require', 'types'.
9696
'paths' option is specified, looking for a pattern to match module name 'const'.
97-
File '/user/username/projects/myproject/node_modules/pkg2/build/package.json' does not exist.
98-
Found 'package.json' at '/user/username/projects/myproject/node_modules/pkg2/package.json'.
99-
Loading module 'const' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
100-
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
101-
Directory '/user/username/projects/myproject/node_modules/pkg2/build/node_modules' does not exist, skipping all lookups in it.
102-
Directory '/user/username/projects/myproject/node_modules/pkg2/build/node_modules/@types' does not exist, skipping all lookups in it.
103-
Directory '/user/username/projects/myproject/node_modules/pkg2/node_modules' does not exist, skipping all lookups in it.
104-
Directory '/user/username/projects/myproject/node_modules/pkg2/node_modules/@types' does not exist, skipping all lookups in it.
105-
File '/user/username/projects/myproject/node_modules/const.ts' does not exist.
106-
File '/user/username/projects/myproject/node_modules/const.tsx' does not exist.
107-
File '/user/username/projects/myproject/node_modules/const.d.ts' does not exist.
108-
Directory '/user/username/projects/myproject/node_modules/@types' does not exist, skipping all lookups in it.
109-
Directory '/user/username/projects/node_modules' does not exist, skipping all lookups in it.
110-
Directory '/user/username/projects/node_modules/@types' does not exist, skipping all lookups in it.
111-
Directory '/user/username/node_modules' does not exist, skipping all lookups in it.
112-
Directory '/user/username/node_modules/@types' does not exist, skipping all lookups in it.
113-
Directory '/user/node_modules' does not exist, skipping all lookups in it.
114-
Directory '/user/node_modules/@types' does not exist, skipping all lookups in it.
115-
Directory '/node_modules' does not exist, skipping all lookups in it.
116-
Directory '/node_modules/@types' does not exist, skipping all lookups in it.
117-
Searching all ancestor node_modules directories for fallback extensions: JavaScript, JSON.
118-
Directory '/user/username/projects/myproject/node_modules/pkg2/build/node_modules' does not exist, skipping all lookups in it.
119-
Directory '/user/username/projects/myproject/node_modules/pkg2/node_modules' does not exist, skipping all lookups in it.
120-
File '/user/username/projects/myproject/node_modules/const.js' does not exist.
121-
File '/user/username/projects/myproject/node_modules/const.jsx' does not exist.
122-
Directory '/user/username/projects/node_modules' does not exist, skipping all lookups in it.
123-
Directory '/user/username/node_modules' does not exist, skipping all lookups in it.
124-
Directory '/user/node_modules' does not exist, skipping all lookups in it.
125-
Directory '/node_modules' does not exist, skipping all lookups in it.
126-
======== Module name 'const' was not resolved. ========
127-
node_modules/pkg2/build/index.d.ts:1:29 - error TS2307: Cannot find module 'const' or its corresponding type declarations.
128-
129-
1 export type { TheNum } from 'const';
130-
   ~~~~~~~
131-
132-
133-
Found 1 error in node_modules/pkg2/build/index.d.ts:1
134-
97+
Module name 'const', matched pattern 'const'.
98+
Trying substitution './const', candidate module location: './const'.
99+
Loading module as file / folder, candidate module location '/user/username/projects/myproject/packages/pkg2/const', target file types: TypeScript, JavaScript, Declaration, JSON.
100+
File '/user/username/projects/myproject/packages/pkg2/const.ts' exists - use it as a name resolution result.
101+
======== Module name 'const' was successfully resolved to '/user/username/projects/myproject/packages/pkg2/const.ts'. ========
135102
//// [/home/src/tslibs/TS/Lib/lib.d.ts] *Lib*
136103
/// <reference no-default-lib="true"/>
137104
interface Boolean {}
@@ -163,11 +130,10 @@ exports.theNum = void 0;
163130
exports.theNum = 42;
164131

165132
//// [/user/username/projects/myproject/packages/pkg1/build/tsconfig.tsbuildinfo] *new*
166-
{"version":"FakeTSVersion","errors":true,"root":["../index.ts"]}
133+
{"version":"FakeTSVersion","root":["../index.ts"]}
167134
//// [/user/username/projects/myproject/packages/pkg1/build/tsconfig.tsbuildinfo.readable.baseline.txt] *new*
168135
{
169136
"version": "FakeTSVersion",
170-
"errors": true,
171137
"root": [
172138
{
173139
"files": [
@@ -176,7 +142,7 @@ exports.theNum = 42;
176142
"original": "../index.ts"
177143
}
178144
],
179-
"size": 64
145+
"size": 50
180146
}
181147
//// [/user/username/projects/myproject/packages/pkg2/build/const.d.ts] *new*
182148
export type TheNum = 42;
@@ -280,6 +246,7 @@ Signatures::
280246
packages/pkg1/tsconfig.json::
281247
SemanticDiagnostics::
282248
*refresh* /home/src/tslibs/TS/Lib/lib.d.ts
249+
*refresh* /user/username/projects/myproject/packages/pkg2/build/const.d.ts
283250
*refresh* /user/username/projects/myproject/node_modules/pkg2/build/index.d.ts
284251
*refresh* /user/username/projects/myproject/packages/pkg1/index.ts
285252
Signatures::

testdata/baselines/reference/tsbuild/moduleResolution/resolves-specifier-in-output-declaration-file-from-referenced-project-correctly.js

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export type { TheNum } from 'const';
3636
}
3737

3838
tsgo -b packages/pkg1 --verbose --traceResolution
39-
ExitStatus:: DiagnosticsPresent_OutputsGenerated
39+
ExitStatus:: Success
4040
Output::
4141
[HH:MM:SS AM] Projects in this build:
4242
* packages/pkg2/tsconfig.json
@@ -95,47 +95,11 @@ Using compiler options of project reference redirect '/user/username/projects/my
9595
Module resolution kind is not specified, using 'Bundler'.
9696
Resolving in CJS mode with conditions 'require', 'types'.
9797
'paths' option is specified, looking for a pattern to match module name 'const'.
98-
File '/user/username/projects/myproject/packages/pkg2/build/package.json' does not exist.
99-
Found 'package.json' at '/user/username/projects/myproject/packages/pkg2/package.json'.
100-
Loading module 'const' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
101-
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
102-
Directory '/user/username/projects/myproject/packages/pkg2/build/node_modules' does not exist, skipping all lookups in it.
103-
Directory '/user/username/projects/myproject/packages/pkg2/build/node_modules/@types' does not exist, skipping all lookups in it.
104-
Directory '/user/username/projects/myproject/packages/pkg2/node_modules' does not exist, skipping all lookups in it.
105-
Directory '/user/username/projects/myproject/packages/pkg2/node_modules/@types' does not exist, skipping all lookups in it.
106-
Directory '/user/username/projects/myproject/packages/node_modules' does not exist, skipping all lookups in it.
107-
Directory '/user/username/projects/myproject/packages/node_modules/@types' does not exist, skipping all lookups in it.
108-
File '/user/username/projects/myproject/node_modules/const.ts' does not exist.
109-
File '/user/username/projects/myproject/node_modules/const.tsx' does not exist.
110-
File '/user/username/projects/myproject/node_modules/const.d.ts' does not exist.
111-
Directory '/user/username/projects/myproject/node_modules/@types' does not exist, skipping all lookups in it.
112-
Directory '/user/username/projects/node_modules' does not exist, skipping all lookups in it.
113-
Directory '/user/username/projects/node_modules/@types' does not exist, skipping all lookups in it.
114-
Directory '/user/username/node_modules' does not exist, skipping all lookups in it.
115-
Directory '/user/username/node_modules/@types' does not exist, skipping all lookups in it.
116-
Directory '/user/node_modules' does not exist, skipping all lookups in it.
117-
Directory '/user/node_modules/@types' does not exist, skipping all lookups in it.
118-
Directory '/node_modules' does not exist, skipping all lookups in it.
119-
Directory '/node_modules/@types' does not exist, skipping all lookups in it.
120-
Searching all ancestor node_modules directories for fallback extensions: JavaScript, JSON.
121-
Directory '/user/username/projects/myproject/packages/pkg2/build/node_modules' does not exist, skipping all lookups in it.
122-
Directory '/user/username/projects/myproject/packages/pkg2/node_modules' does not exist, skipping all lookups in it.
123-
Directory '/user/username/projects/myproject/packages/node_modules' does not exist, skipping all lookups in it.
124-
File '/user/username/projects/myproject/node_modules/const.js' does not exist.
125-
File '/user/username/projects/myproject/node_modules/const.jsx' does not exist.
126-
Directory '/user/username/projects/node_modules' does not exist, skipping all lookups in it.
127-
Directory '/user/username/node_modules' does not exist, skipping all lookups in it.
128-
Directory '/user/node_modules' does not exist, skipping all lookups in it.
129-
Directory '/node_modules' does not exist, skipping all lookups in it.
130-
======== Module name 'const' was not resolved. ========
131-
packages/pkg2/build/index.d.ts:1:29 - error TS2307: Cannot find module 'const' or its corresponding type declarations.
132-
133-
1 export type { TheNum } from 'const';
134-
   ~~~~~~~
135-
136-
137-
Found 1 error in packages/pkg2/build/index.d.ts:1
138-
98+
Module name 'const', matched pattern 'const'.
99+
Trying substitution './const', candidate module location: './const'.
100+
Loading module as file / folder, candidate module location '/user/username/projects/myproject/packages/pkg2/const', target file types: TypeScript, JavaScript, Declaration, JSON.
101+
File '/user/username/projects/myproject/packages/pkg2/const.ts' exists - use it as a name resolution result.
102+
======== Module name 'const' was successfully resolved to '/user/username/projects/myproject/packages/pkg2/const.ts'. ========
139103
//// [/home/src/tslibs/TS/Lib/lib.d.ts] *Lib*
140104
/// <reference no-default-lib="true"/>
141105
interface Boolean {}
@@ -167,11 +131,10 @@ exports.theNum = void 0;
167131
exports.theNum = 42;
168132

169133
//// [/user/username/projects/myproject/packages/pkg1/build/tsconfig.tsbuildinfo] *new*
170-
{"version":"FakeTSVersion","errors":true,"root":["../index.ts"]}
134+
{"version":"FakeTSVersion","root":["../index.ts"]}
171135
//// [/user/username/projects/myproject/packages/pkg1/build/tsconfig.tsbuildinfo.readable.baseline.txt] *new*
172136
{
173137
"version": "FakeTSVersion",
174-
"errors": true,
175138
"root": [
176139
{
177140
"files": [
@@ -180,7 +143,7 @@ exports.theNum = 42;
180143
"original": "../index.ts"
181144
}
182145
],
183-
"size": 64
146+
"size": 50
184147
}
185148
//// [/user/username/projects/myproject/packages/pkg2/build/const.d.ts] *new*
186149
export type TheNum = 42;
@@ -284,6 +247,7 @@ Signatures::
284247
packages/pkg1/tsconfig.json::
285248
SemanticDiagnostics::
286249
*refresh* /home/src/tslibs/TS/Lib/lib.d.ts
250+
*refresh* /user/username/projects/myproject/packages/pkg2/build/const.d.ts
287251
*refresh* /user/username/projects/myproject/packages/pkg2/build/index.d.ts
288252
*refresh* /user/username/projects/myproject/packages/pkg1/index.ts
289253
Signatures::

0 commit comments

Comments
 (0)