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

[Bug] Problem with running via ts-node when entry file is in a different directory #181

Open
Gikkman opened this issue Oct 20, 2023 · 3 comments

Comments

@Gikkman
Copy link

Gikkman commented Oct 20, 2023

If I try to run a project using ts-node, and the entry file the ts-node command points to is in a different directory from your current working directory, then the path remapping won't be correct. If you build the project however, then the path mapping will be correct. It might be that I configure something wrong along the way, but I can't really figure out what that would be in that case.

I've set up an example project you can clone that shows the issue: https://github.com/Gikkman/typescript-transform-paths-reproduce-ts-node-problem

Clone the project, then run npm i to set everything up. Running npm run dev:a will try to run ts-node in a way that crashes, but running npm run dev:b will run it in a way so it works. You can then build the project, using npm run build. If you inspect the output (under the build folder), you'll see that the build output has correct mappings. Running the built projects via npm run start:a or npm run start:b will also show that both works post-build.

If I were to theorize, I'm guessing the first file's path resolution doesn't take into consideration that it might not be in the same directory as script is invoked from. It isn't hard to work around IMO (just make a bootstrap script in the same directory as the package.json) but I thought it might be worth mentioning.

@Gikkman
Copy link
Author

Gikkman commented Oct 21, 2023

I can also mention, I've tried replacing this library to tsconfig-paths, and they can correctly run my project using ts-node. You can't however use this library as plugins in compilerOptions at the same time, or they will interfere with one another.

I pushed a branch to show that it works using ts-node. See here. Unfortunately, since that library can't help with compilation, it does make the build setup require a lot of extra config files. See an example for using both in tandem here.

@nonara
Copy link
Collaborator

nonara commented Oct 30, 2023

Thanks for the report!

ts-node does some creative things when creating its Program instance and determining the config / CompilerOptions to use. Given what you're describing with the directory differences, that's probably the culprit.

Have you tried specifying the tsconfig file when running the command? I will take a look at the repro when I'm able, if that doesn't work. Just thought I'd send a quick reply first.

@Gikkman
Copy link
Author

Gikkman commented Nov 2, 2023

Hi.

I tried your suggestion of manually setting the tsconfig file for each project (i.e. -P ./tsconfig.json), but it didn't have any effect.

But I dug some more and did some additional testing, and I think it has something to do with how ts-node decides what is the "directory to search from". If I run ts-node with the --showConfig flag, I get the following result for my two projects:

For pack-a:

{
  "ts-node": {
    "require": [
      "(...)/typescript-transform-paths-reproduce-ts-node-problem/node_modules/typescript-transform-paths/register.js"
    ],
    "transpileOnly": true,
    "cwd": "(...)/typescript-transform-paths-reproduce-ts-node-problem/pack-a",
    "projectSearchDir": "(...)/typescript-transform-paths-reproduce-ts-node-problem/pack-a/src",
    "project": "(...)/typescript-transform-paths-reproduce-ts-node-problem/pack-a/tsconfig.json"
  },
  "compilerOptions": {
    ...
  }
}

For pack-a:

{
  "ts-node": {
    "require": [
      "(...)/typescript-transform-paths-reproduce-ts-node-problem/node_modules/typescript-transform-paths/register.js"
    ],
    "transpileOnly": true,
    "cwd": "(...)/typescript-transform-paths-reproduce-ts-node-problem/pack-b",
    "projectSearchDir": "(...)/typescript-transform-paths-reproduce-ts-node-problem/pack-b",
    "project": "(...)/typescript-transform-paths-reproduce-ts-node-problem/pack-b/tsconfig.json"
  },
  "compilerOptions": {
    ...
  }
}

Notice that the only difference between the two is the projectSearchDir property. I think ts-node does some kind of internal maigc to try to guess where the source root is, and that somehow affects the resolution logic.

From this find, I tried to manipulate ts-node and get it to set the projectSearchDir to the same directory as the tsconfig.json file. First, I tried setting the projectSearchDir manually in the tsconfig.json file, but that doesn't seem to do the trick (does ts-node even respect the setting?). Next, I tried to create a empty nop.ts file next to the tsconfig.json file. This does actually work, even if the entry point file you target is not in the same directory. I pushed a branch showing the workaround here.

So in the end, I am not entirely sure if the problem is with this library or if it is with ts-node itself. I think this issue or maybe this issue might describe the same problem that I describe here. And given that it does work well when I run tsc, I am beginning to suspect ts-node is the actual culprit. With that said, I don't know how tsconfig-paths have solved it. Guess that's what I'll look into next.

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

No branches or pull requests

2 participants