Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit763e4d7

Browse files
Copilotjakebailey
andcommitted
Fix destructuring re-exports using type from symlinked node-modules to use package names
Co-authored-by: jakebailey <5341706+jakebailey@users.noreply.github.com>
1 parent180bc9f commit763e4d7

File tree

1 file changed

+61
-7
lines changed

1 file changed

+61
-7
lines changed

‎internal/modulespecifiers/specifiers.go

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,10 @@ func computeModuleSpecifiers(
338338

339339
for_,modulePath:=rangemodulePaths {
340340
varspecifierstring
341-
ifmodulePath.IsInNodeModules {
342-
specifier=tryGetModuleNameAsNodeModule(modulePath,info,importingSourceFile,host,compilerOptions,userPreferences/*packageNameOnly*/,false,options.OverrideImportMode)
343-
}
341+
// Try to generate a node module specifier for all paths, not just those marked IsInNodeModules
342+
// This handles symlinked packages where the real path doesn't contain node_modules
343+
// but a package name can still be determined
344+
specifier=tryGetModuleNameAsNodeModule(modulePath,info,importingSourceFile,host,compilerOptions,userPreferences/*packageNameOnly*/,false,options.OverrideImportMode)
344345
iflen(specifier)>0&&!(forAutoImport&&isExcludedByRegex(specifier,preferences.excludeRegexes)) {
345346
nodeModulesSpecifiers=append(nodeModulesSpecifiers,specifier)
346347
ifmodulePath.IsRedirect {
@@ -378,9 +379,16 @@ func computeModuleSpecifiers(
378379
}else {
379380
pathsSpecifiers=append(pathsSpecifiers,local)
380381
}
381-
}elseifforAutoImport|| (!importedFileIsInNodeModules||!modulePath.IsInNodeModules) {
382-
// Only add to relative specifiers if this path is NOT in node_modules.
383-
// For symlinked packages, we want node_modules paths to be prioritized over real paths.
382+
}elseifforAutoImport||!importedFileIsInNodeModules||modulePath.IsInNodeModules {
383+
// Why this extra conditional, not just an `else`? If some path to the file contained
384+
// 'node_modules', but we can't create a non-relative specifier (e.g. "@foo/bar/path/to/file"),
385+
// that means we had to go through a *sibling's* node_modules, not one we can access directly.
386+
// If some path to the file was in node_modules but another was not, this likely indicates that
387+
// we have a monorepo structure with symlinks. In this case, the non-nodeModules path is
388+
// probably the realpath, e.g. "../bar/path/to/file", but a relative path to another package
389+
// in a monorepo is probably not portable. So, the module specifier we actually go with will be
390+
// the relative path through node_modules, so that the declaration emitter can produce a
391+
// portability error. (See declarationEmitReexportedSymlinkReference3)
384392
relativeSpecifiers=append(relativeSpecifiers,local)
385393
}
386394
}
@@ -638,6 +646,50 @@ func tryGetModuleNameFromRootDirs(
638646
returnprocessEnding(shortest,allowedEndings,compilerOptions,host)
639647
}
640648

649+
functryGetPackageNameForSymlinkedFile(fileNamestring,infoInfo,hostModuleSpecifierGenerationHost)string {
650+
// Look for package.json in the file's directory or parent directories
651+
currentDir:=tspath.GetDirectoryPath(fileName)
652+
forlen(currentDir)>0 {
653+
packageJsonPath:=tspath.CombinePaths(currentDir,"package.json")
654+
ifhost.FileExists(packageJsonPath) {
655+
packageInfo:=host.GetPackageJsonInfo(packageJsonPath)
656+
ifpackageInfo!=nil&&packageInfo.GetContents()!=nil {
657+
packageName,ok:=packageInfo.GetContents().Name.GetValue()
658+
ifok&&len(packageName)>0 {
659+
// Check if this package can be resolved from the importing source directory
660+
// by looking for a symlink in node_modules
661+
ifcanResolveAsPackage(packageName,info.SourceDirectory,host) {
662+
returnpackageName
663+
}
664+
}
665+
}
666+
}
667+
parentDir:=tspath.GetDirectoryPath(currentDir)
668+
ifparentDir==currentDir {
669+
break
670+
}
671+
currentDir=parentDir
672+
}
673+
return""
674+
}
675+
676+
funccanResolveAsPackage(packageNamestring,importingDirstring,hostModuleSpecifierGenerationHost)bool {
677+
// Walk up from importing directory looking for node_modules that contains this package
678+
currentDir:=importingDir
679+
forlen(currentDir)>0 {
680+
nodeModulesPath:=tspath.CombinePaths(currentDir,"node_modules",packageName)
681+
ifhost.FileExists(tspath.CombinePaths(nodeModulesPath,"package.json")) {
682+
returntrue
683+
}
684+
parentDir:=tspath.GetDirectoryPath(currentDir)
685+
ifparentDir==currentDir {
686+
break
687+
}
688+
currentDir=parentDir
689+
}
690+
returnfalse
691+
}
692+
641693
functryGetModuleNameAsNodeModule(
642694
pathObjModulePath,
643695
infoInfo,
@@ -650,7 +702,9 @@ func tryGetModuleNameAsNodeModule(
650702
)string {
651703
parts:=getNodeModulePathParts(pathObj.FileName)
652704
ifparts==nil {
653-
return""
705+
// For symlinked packages, the real path may not contain node_modules
706+
// Try to infer package name by looking for package.json
707+
returntryGetPackageNameForSymlinkedFile(pathObj.FileName,info,host)
654708
}
655709

656710
// Simplify the full file path to something that can be resolved by Node.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp