Skip to content

Fix nested dist when referenced package has a .ts file next to a .js file #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

maschwenk
Copy link
Contributor

Reproduction case:

I prompted Cursor with:

I'm having an issue where if we have this structure

package A
   depends on package B and imports from package B
package B
   has a dist folder with both an dist/index.js and dist/index.ts

when I build package A, the dist folder of A becomes nested. the Go implementation behaves this way but the javascript implementation doesn't. Can you figure this out?

and after a few turns got this, so still working out if this is valid or not but it does fix my issue.

Simpler fix for this is just not having .ts files in dist, which is generally a good thing in general, but I was just curious why this was behaving differently in strada/corsa.

@Copilot Copilot AI review requested due to automatic review settings June 17, 2025 21:31
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

A fix to prevent nested dist directories when referencing a package that ships both .js and .ts files.

  • Added findPackageRoot helper to identify package boundaries via package.json.
  • Enhanced import resolution to detect cross-package imports and skip TypeScript source files from external dependencies.
  • Updated shouldAddFile logic to exclude external .ts/.tsx/.mts/.cts files.
Comments suppressed due to low confidence (3)

internal/compiler/fileloader.go:434

  • [nitpick] The isExternalDependency flag is being overloaded to also represent cross-package imports. Consider introducing a separate isCrossPackageDependency flag for clarity and to simplify intent.
			if !isExternalDependency && !tspath.IsExternalModuleNameRelative(moduleName) {

internal/compiler/fileloader.go:458

  • Add a unit test for the scenario where a package’s dist contains both .js and .ts files to verify that external TypeScript sources are correctly excluded and no nested output folder is created.
				!isTypeScriptSourceFromExternalDep &&

internal/compiler/fileloader.go:336

  • Consider caching results of findPackageRoot (e.g., in a map) to avoid repeated filesystem walks for each import, which could affect performance in large monorepos.
func (p *fileLoader) findPackageRoot(startDir string) string {

@maschwenk maschwenk force-pushed the mfs-try-to-fix-nested-dist branch from 9778c12 to e78bf35 Compare June 17, 2025 21:46
@jakebailey
Copy link
Member

I'm not sure what's going on here; this code is very much unlike the original code. I think the fix is going to look very different to this.

@maschwenk
Copy link
Contributor Author

Per Discord, we've determined that the actual root cause of the issue here is the differences in implementation of

_submodules/TypeScript/src/compiler/utilities.ts's sourceFileMayBeEmitted versus

internal/compiler/emitter.go's sourceFileMayBeEmitted

I believe the actual case that should be returning false here is one of these:

    // Source file from node_modules are not emitted
    if (host.isSourceFileFromExternalLibrary(sourceFile)) return false;
    // forcing dts emit => file needs to be emitted
    if (forceDtsEmit) return true;
    // Check other conditions for file emit
    // Source files from referenced projects are not emitted
    if (host.isSourceOfProjectReferenceRedirect(sourceFile.fileName)) return false;

and I'm fairly sure it's isSourceFileFromExternalLibrary which is still not implemented in Go-land

@maschwenk maschwenk closed this Jun 18, 2025
@jakebailey
Copy link
Member

All of the code needed to implement isSourceFileFromExternalLibrary is actually there since #1189, actually. We just aren't collecting that list of files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants