@@ -331,6 +331,26 @@ func (p *fileLoader) resolveTypeReferenceDirectives(file *ast.SourceFile, meta a
331
331
332
332
const externalHelpersModuleNameText = "tslib" // TODO(jakebailey): dedupe
333
333
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
+
334
354
func (p * fileLoader ) resolveImportsAndModuleAugmentations (file * ast.SourceFile , meta ast.SourceFileMetaData ) (
335
355
toParse []resolvedRef ,
336
356
resolutionsInFile module.ModeAwareCache [* module.ResolvedModule ],
@@ -401,13 +421,41 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(file *ast.SourceFile,
401
421
// - noResolve is falsy
402
422
// - module name comes from the list of imports
403
423
// - 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)
404
425
405
426
importIndex := index - importsStart
406
427
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
+
407
454
shouldAddFile := moduleName != "" &&
408
455
getResolutionDiagnostic (optionsForFile , resolvedModule , file ) == nil &&
409
456
! optionsForFile .NoResolve .IsTrue () &&
410
457
! (isJsFile && ! optionsForFile .GetAllowJS ()) &&
458
+ ! isTypeScriptSourceFromExternalDep &&
411
459
(importIndex < 0 || (importIndex < len (file .Imports ()) && (ast .IsInJSFile (file .Imports ()[importIndex ]) || file .Imports ()[importIndex ].Flags & ast .NodeFlagsJSDoc == 0 )))
412
460
413
461
if shouldAddFile {
0 commit comments