privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
8.61k stars 132 forks source link

(pnpm) Using `tsx` in `postinstall` script generates unexpected file at `./tsx-0/` #596

Closed NamesMT closed 4 days ago

NamesMT commented 1 week ago

Acknowledgements

Minimal reproduction URL

https://stackblitz.com/github/namesmt/repro-tsx

Problem & expected behavior (under 200 words)

Alternative github repo because stackblitz could not load for me, idk if it's just me: https://github.com/NamesMT/repro-tsx

Simply run pnpm install, an unexpected file is created along with the folder tsx-0 image

It does not happen with normal npm

Bugs are expected to be fixed by those affected by it

Compensating engineering work will speed up resolution and support the project

privatenumber commented 1 week ago

I can't reproduce this on my end. Can you use GitHub Actions or something to demonstrate it?

NamesMT commented 1 week ago

Hello @privatenumber,

Here is a oneline reproduction using docker run: docker run -it --rm namesmt/images-alpine:node-dev zsh -c "cd home && nlx giget gh:namesmt/repro-tsx repro-tsx && cd repro-tsx && ni && ls && ls tsx-0 && zsh"

image

privatenumber commented 1 week ago

Sorry but a screenshot doesn't help. And I don't use Docker.

I need a reproduction env for me to debug and test fixes in. GitHub Actions or any kind of CI would work best.

NamesMT commented 1 week ago

Hello @privatenumber, idk why but for github's default runners I could not reproduce it, but does indeed exists because I could reproduce the issue on my work machine which uses plain Ubuntu,

I have also do more tests and it is indeed reproducible on various docker images: ubuntu, node/node:slim, alpine.

I have also able to reproduce it on my company Gitlab CI with node:alpine image.

For your convenience, I have setup a repo in which I was able to use Github Actions to reproduce it (using docker container action): https://github.com/NamesMT/repro-tsx-action You could simply fork it and push commits/PR to test.

Thanks a lot.

privatenumber commented 1 week ago

I appreciate it, thanks @NamesMT

I haven't confirmed this, but I believe this is actually https://github.com/pnpm/pnpm/issues/7330

tsx creates a cache directory in os.tmpdir() which implies pnpm in Alpine is returning the current working directory in the env variables.

Will take a closer look later on.

NamesMT commented 1 week ago

That seems to be it, maybe we can apply a workaround like jiti#123?

privatenumber commented 1 week ago

If it's rooted in pnpm, then it's not a tsx bug. And I would rather not patch bugs from other tools via tsx.

This is for the same reasons I would relay any Node bugs we get to https://github.com/nodejs/node. Encouraging fixes to be made in the right places maximizes benefit to the community.

Can you verify this is what's causing it by running pnpm with unsafe-perm? https://github.com/pnpm/npm-lifecycle/blob/1e0f149176877f906968a01087cf63290652ccbc/index.js#L100 If so, try opening a PR there.

NamesMT commented 1 week ago

Yes I can verify that running pnpm with unsafe-perm, the issue is not present, thanks a lot for finding the root cause. Don't have time to open a PR yet but I'll do it when I do and no one has created one specific to it.

privatenumber commented 4 days ago

Closing as it's a bug in pnpm rather than tsx