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

Running from non-project-root fails to resolve tsconfig "paths" #482

Open
4 of 6 tasks
sou-long opened this issue Feb 25, 2024 · 4 comments
Open
4 of 6 tasks

Running from non-project-root fails to resolve tsconfig "paths" #482

sou-long opened this issue Feb 25, 2024 · 4 comments
Labels
bug Something isn't working pending triage tsconfig

Comments

@sou-long
Copy link

sou-long commented Feb 25, 2024

Acknowledgements

  • I searched existing issues before opening this one to avoid duplicates
  • I understand this is not a place for seek help, but to report a bug
  • I understand that the bug must be proven first with a minimal reproduction
  • I will be polite, respectful, and considerate of people's time and effort

Minimal reproduction URL

https://stackblitz.com/edit/node-hpyz3n

Version

4.7.0

Node.js version

18.18.2

Package manager

yarn

Operating system

Windows

Problem & Expected behavior

What happened?

I added a custom path into tsconfig.json: "paths": {"@/*": ["./*"]}. Then if I run tsx index.ts in a project directory, all ok. If I run it from other directory, as the linked stackblitz already does (see .stackblitzrc):

cd ..
node-hpyz3n/node_modules/.bin/tsx node-hpyz3n/index.ts

then tsx cannot resolve modules referred to by "@/", e.g. import "@/other.js".

This prevents using tsx as a loader for cli tools of projects that use tsconfig paths, as in:

// cli.ts
#!/usr/bin/env tsx
import "@/other.js"
...

// package.json
{
  "bin": {"my-tool":"cli.ts"},
  ...
}

// shell
$ cd my-project
$ yarn link
$ cd /some/other/path
$ my-tool

This behavior doesn't seem to depend on path template names (@, @src, without @, etc).

What did you expect instead?

For tsx to resolve tsconfig paths based on a resolved project root, e.g. tsconfig.json/compilerOptions/{rootDir,baseUrl}. Or maybe based on tsconfig.json's location.

So, basically, if regular imports work relative to the importing source file, tsconfig-paths imports, being baseUrl-relative in normal circumstances, should work relative to where tsconfig.json thinks the root is. I believe there's no reason to resolve modules outside of a project root.

Do you have an error stack trace?

Error: Cannot find module '@/other.js'
Require stack:
- /home/projects/node-hpyz3n/index.ts
    at Module._resolveFilename (https://nodehpyz3n-b5hh.w-credentialless.staticblitz.com/blitz.6f234770.js:54:12642)
    at d.default._resolveFilename (file:///home/projects/node-hpyz3n/node_modules/.pnpm/@[email protected]/node_modules/@esbuild-kit/cjs-loader/dist/index.js:1:1529)
    at Module._load (https://nodehpyz3n-b5hh.w-credentialless.staticblitz.com/blitz.6f234770.js:54:10056)
    at Module.require (https://nodehpyz3n-b5hh.w-credentialless.staticblitz.com/blitz.6f234770.js:54:13775)
    at i (https://nodehpyz3n-b5hh.w-credentialless.staticblitz.com/blitz.6f234770.js:98:2198)
    at _0x16f7fc (https://nodehpyz3n-b5hh.w-credentialless.staticblitz.com/blitz.6f234770.js:352:194520)
    at eval (file:///home/projects/node-hpyz3n/index.ts:2:132)
    at Object.eval (file:///home/projects/node-hpyz3n/index.ts:3:3)
    at Object.function (https://nodehpyz3n-b5hh.w-credentialless.staticblitz.com/blitz.6f234770.js:352:195362)
    at Module._compile (https://nodehpyz3n-b5hh.w-credentialless.staticblitz.com/blitz.6f234770.js:54:14871) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/home/projects/node-hpyz3n/index.ts' ]
}

Do you have documentation links? Do you have screenshots?

Nope.

Contributions

  • I plan to open a pull request for this issue
  • I plan to make a financial contribution to this project
@sou-long sou-long added bug Something isn't working pending triage labels Feb 25, 2024
@roysandrew
Copy link

roysandrew commented Feb 26, 2024

Duplicates #265 I think

@privatenumber
Copy link
Owner

Please fix your reproduction to:

  • Remove unnecessary code
  • Remove extraneous steps; specifically in your case, move the TS project to a nested dir so you don't need to ask others to run a more complex command.

@sou-long
Copy link
Author

sou-long commented Mar 3, 2024

Done. A nested dir doesn't work with stackblitz due to path issues, but I modified .stackblitzrc so it takes the described steps and reproduces the issue immediately.

@sjoholma

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage tsconfig
Projects
None yet
Development

No branches or pull requests

4 participants