Skip to content

Commit e78bf35

Browse files
committed
Fix nested dist when referenced package has ts file
1 parent ba0fb29 commit e78bf35

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

internal/compiler/fileloader.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,26 @@ func (p *fileLoader) resolveTypeReferenceDirectives(file *ast.SourceFile, meta a
331331

332332
const externalHelpersModuleNameText = "tslib" // TODO(jakebailey): dedupe
333333

334+
// findPackageRoot walks up the directory tree to find the nearest package.json file
335+
// and returns the directory containing it, or empty string if not found
336+
func (p *fileLoader) findPackageRoot(startDir string) string {
337+
current := startDir
338+
for {
339+
packageJsonPath := tspath.CombinePaths(current, "package.json")
340+
if p.opts.Host.FS().FileExists(packageJsonPath) {
341+
return current
342+
}
343+
344+
parent := tspath.GetDirectoryPath(current)
345+
if parent == current {
346+
// Reached root directory
347+
break
348+
}
349+
current = parent
350+
}
351+
return ""
352+
}
353+
334354
func (p *fileLoader) resolveImportsAndModuleAugmentations(file *ast.SourceFile, meta ast.SourceFileMetaData) (
335355
toParse []resolvedRef,
336356
resolutionsInFile module.ModeAwareCache[*module.ResolvedModule],
@@ -401,13 +421,41 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(file *ast.SourceFile,
401421
// - noResolve is falsy
402422
// - module name comes from the list of imports
403423
// - it's not a top level JavaScript module that exceeded the search max
424+
// - it's not a TypeScript source file from an external dependency (which would cause nested output directories)
404425

405426
importIndex := index - importsStart
406427

428+
// Check if this is a TypeScript source file from an external dependency
429+
// External dependencies should only contribute declaration files (.d.ts) and JavaScript files (.js/.jsx)
430+
// not TypeScript source files (.ts/.tsx) which would be included in compilation
431+
isExternalDependency := resolvedModule.IsExternalLibraryImport
432+
433+
// In monorepo setups, also check if this is a cross-package import (package name import that resolves outside current package)
434+
if !isExternalDependency && !tspath.IsExternalModuleNameRelative(moduleName) {
435+
// This is a package name import (e.g., "@tsgo-repros/my-package-b")
436+
// Check if the resolved file is outside the current package directory
437+
currentPackageDir := tspath.GetDirectoryPath(file.FileName())
438+
resolvedPackageDir := tspath.GetDirectoryPath(resolvedModule.ResolvedFileName)
439+
440+
// Find the root of the current package (look for package.json)
441+
currentPackageRoot := p.findPackageRoot(currentPackageDir)
442+
resolvedPackageRoot := p.findPackageRoot(resolvedPackageDir)
443+
444+
// If they have different package roots, this is a cross-package dependency
445+
if currentPackageRoot != "" && resolvedPackageRoot != "" && currentPackageRoot != resolvedPackageRoot {
446+
isExternalDependency = true
447+
}
448+
}
449+
450+
isTypeScriptSourceFromExternalDep := isExternalDependency &&
451+
(resolvedModule.Extension == tspath.ExtensionTs || resolvedModule.Extension == tspath.ExtensionTsx ||
452+
resolvedModule.Extension == tspath.ExtensionMts || resolvedModule.Extension == tspath.ExtensionCts)
453+
407454
shouldAddFile := moduleName != "" &&
408455
getResolutionDiagnostic(optionsForFile, resolvedModule, file) == nil &&
409456
!optionsForFile.NoResolve.IsTrue() &&
410457
!(isJsFile && !optionsForFile.GetAllowJS()) &&
458+
!isTypeScriptSourceFromExternalDep &&
411459
(importIndex < 0 || (importIndex < len(file.Imports()) && (ast.IsInJSFile(file.Imports()[importIndex]) || file.Imports()[importIndex].Flags&ast.NodeFlagsJSDoc == 0)))
412460

413461
if shouldAddFile {

0 commit comments

Comments
 (0)