Skip to content
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

tsconfig paths not resolving in imported package #76

Closed
1 task
IlyaSemenov opened this issue Aug 9, 2022 · 8 comments
Closed
1 task

tsconfig paths not resolving in imported package #76

IlyaSemenov opened this issue Aug 9, 2022 · 8 comments
Labels
bug Something isn't working pending triage

Comments

@IlyaSemenov
Copy link
Contributor

IlyaSemenov commented Aug 9, 2022

Bug description

I have a monorepo with two packages a and b. I run a script in package a which imports a module from package b. The module in package b imports a submodule from package b using tsconfig paths.

I expect the script to run normally.

Instead, it crashes:

❯ p tsx test.ts 
Error: Cannot find module '@/lib'
Require stack:
- /Users/semenov/tmp/tsx-monorepo-alias/packages/b/index.ts
- /Users/semenov/tmp/tsx-monorepo-alias/packages/a/test.ts
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.u.default._resolveFilename (/Users/semenov/tmp/tsx-monorepo-alias/node_modules/.pnpm/@[email protected]/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:1486)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/semenov/tmp/tsx-monorepo-alias/packages/b/index.ts:1:8)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.v (/Users/semenov/tmp/tsx-monorepo-alias/node_modules/.pnpm/@[email protected]/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:933)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/semenov/tmp/tsx-monorepo-alias/packages/b/index.ts',
    '/Users/semenov/tmp/tsx-monorepo-alias/packages/a/test.ts'
  ]
}

Reproduction

https://github.com/IlyaSemenov/tsx-monorepo-import-reproduction includes step by step reproduction.

By the way, if you run pnpm add esbuild-runner and run the script with pnpm esr test.ts, it will work! (esbuild-runner is unfortunately abandoned and has other issues)

Environment

System:
    OS: macOS 12.5
    CPU: (8) arm64 Apple M1
    Memory: 74.92 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.15.0 - ~/.nodenv/versions/16.15.0/bin/node
    Yarn: 1.22.19 - ~/.nodenv/versions/16.15.0/bin/yarn
    npm: 8.5.5 - ~/.nodenv/versions/16.15.0/bin/npm

tsx is version 3.8.1

Can you contribute a fix?

  • I’m interested in opening a pull request for this issue.
@IlyaSemenov IlyaSemenov added bug Something isn't working pending triage labels Aug 9, 2022
@IlyaSemenov IlyaSemenov changed the title tsconfig paths not resolving in referenced repo tsconfig paths not resolving in imported package Aug 9, 2022
@privatenumber
Copy link
Owner

This is expected behavior.

A TypeScript compilation—which is happening at runtime in this case—, cannot load more than one tsconfig.json at once. I imagine esr is looking up a tsconfig.json for each file, but that's expensive and also not correct behavior.

I verified that tsx's behavior is consistent with ts-node.

@IlyaSemenov
Copy link
Contributor Author

@privatenumber But this is not consistent with esbuild itself! In the reproduction repo:

❯ p esbuild test.ts --bundle > test.js

❯ cat test.js
"use strict";
(() => {
  // ../b/lib.ts
  console.log("imported lib");
})();

❯ node test.js
imported lib

So it's possible to build a production build with these aliases but not run it in dev mode with tsx? Please reconsider your decision.

@IlyaSemenov
Copy link
Contributor Author

@privatenumber thoughts?

@privatenumber
Copy link
Owner

Curious how esbuild works if setting a different tsconfig path?

https://esbuild.github.io/api/#tsconfig

@IlyaSemenov
Copy link
Contributor Author

@privatenumber esbuild supports multiple tsconfig.json from referenced folders - same as I would expect from tsx.

This is explicitly stated by Evan, e.g.:

@privatenumber
Copy link
Owner

privatenumber commented Sep 8, 2022

How does it handle custom tsconfig.json paths via the tsconfig CLI flag for multiple projects?

e.g. If project A uses tsconfig.a.json and its dependency project B uses tsconfig.b.json, how is it configured?

@IlyaSemenov
Copy link
Contributor Author

I'm not following how is that related to the case. From what I've quickly gathered, there's no esbuild API option to provide different custom named tsconfig's for different projects. Why would that matter?

@privatenumber
Copy link
Owner

In case the uncompiled dependency project has a custom tsconfig (eg. with paths configured).

I'm going to lock this thread in favor of #96 since this one is already closed.

Repository owner locked and limited conversation to collaborators Oct 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working pending triage
Projects
None yet
Development

No branches or pull requests

2 participants