Skip to content

Commit a98f2f6

Browse files
committed
Mark any external pnp dependency as isExternalLibraryImport=true
1 parent 10aab61 commit a98f2f6

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

internal/module/resolver.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,10 @@ func (r *resolutionState) resolveNodeLikeWorker() *ResolvedModule {
475475
} else {
476476
candidate := normalizePathForCJSResolution(r.containingDirectory, r.name)
477477
resolved := r.nodeLoadModuleByRelativeName(r.extensions, candidate, false, true)
478+
478479
return r.createResolvedModule(
479480
resolved,
480-
resolved != nil && (tspath.IsExternalLibraryImport(resolved.path)),
481+
r.isExternalLibraryImport(resolved),
481482
)
482483
}
483484
return r.createResolvedModule(nil, false)
@@ -1089,7 +1090,8 @@ func (r *resolutionState) loadModuleFromSpecificNodeModulesDirectoryImpl(ext ext
10891090
}
10901091

10911092
func (r *resolutionState) createResolvedModuleHandlingSymlink(resolved *resolved) *ResolvedModule {
1092-
isExternalLibraryImport := resolved != nil && (tspath.IsExternalLibraryImport(resolved.path))
1093+
isExternalLibraryImport := r.isExternalLibraryImport(resolved)
1094+
10931095
if r.compilerOptions.PreserveSymlinks != core.TSTrue &&
10941096
isExternalLibraryImport &&
10951097
resolved.originalPath == "" &&
@@ -1137,7 +1139,8 @@ func (r *resolutionState) createResolvedTypeReferenceDirective(resolved *resolve
11371139
resolvedTypeReferenceDirective.ResolvedFileName = resolved.path
11381140
resolvedTypeReferenceDirective.Primary = primary
11391141
resolvedTypeReferenceDirective.PackageId = resolved.packageId
1140-
resolvedTypeReferenceDirective.IsExternalLibraryImport = tspath.IsExternalLibraryImport(resolved.path)
1142+
1143+
resolvedTypeReferenceDirective.IsExternalLibraryImport = r.isExternalLibraryImport(resolved)
11411144

11421145
if r.compilerOptions.PreserveSymlinks != core.TSTrue {
11431146
originalPath, resolvedFileName := r.getOriginalAndResolvedFileName(resolved.path)
@@ -1885,6 +1888,21 @@ func (r *resolutionState) conditionMatches(condition string) bool {
18851888
return false
18861889
}
18871890

1891+
func (r *resolutionState) isExternalLibraryImport(resolved *resolved) bool {
1892+
if resolved == nil {
1893+
return false
1894+
}
1895+
1896+
isExternalLibraryImport := strings.Contains(resolved.path, "/node_modules/")
1897+
1898+
pnpApi := r.resolver.host.PnpApi()
1899+
if pnpApi != nil && !isExternalLibraryImport {
1900+
isExternalLibraryImport = pnpApi.IsInPnpModule(resolved.path, r.containingDirectory)
1901+
}
1902+
1903+
return isExternalLibraryImport
1904+
}
1905+
18881906
func (r *resolutionState) getTraceFunc() func(string) {
18891907
if r.tracer != nil {
18901908
return r.tracer.write

internal/pnp/pnpapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,10 +346,10 @@ func (p *PnpApi) GetPackageLocationAbsolutePath(packageInfo *PackageInfo) string
346346
return tspath.ResolvePath(p.manifest.dirPath, packageLocation)
347347
}
348348

349+
// Checks if fromFileName and toFileName are in different pnp modules
349350
func (p *PnpApi) IsInPnpModule(fromFileName string, toFileName string) bool {
350351
fromLocator, _ := p.FindLocator(fromFileName)
351352
toLocator, _ := p.FindLocator(toFileName)
352-
// The targeted filename is in a pnp module different from the requesting filename
353353
return fromLocator != nil && toLocator != nil && fromLocator.Name != toLocator.Name
354354
}
355355

internal/tspath/path.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -869,17 +869,6 @@ func IsExternalModuleNameRelative(moduleName string) bool {
869869
return PathIsRelative(moduleName) || IsRootedDiskPath(moduleName)
870870
}
871871

872-
func IsExternalLibraryImport(path string) bool {
873-
// When PnP is enabled, some internal libraries can be resolved as virtual packages, which should be treated as external libraries
874-
// Since these virtual pnp packages don't have a `/node_modules/` folder, we need to check for the presence of `/__virtual__/` in the path
875-
// See https://yarnpkg.com/advanced/lexicon#virtual-package for more details
876-
return strings.Contains(path, "/node_modules/") || isPnpVirtualPath(path)
877-
}
878-
879-
func isPnpVirtualPath(path string) bool {
880-
return strings.Contains(path, "/__virtual__/")
881-
}
882-
883872
type ComparePathsOptions struct {
884873
UseCaseSensitiveFileNames bool
885874
CurrentDirectory string

0 commit comments

Comments
 (0)