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

Support process.chdir in worker thread #41673

Closed
sindresorhus opened this issue Jan 24, 2022 · 5 comments
Closed

Support process.chdir in worker thread #41673

sindresorhus opened this issue Jan 24, 2022 · 5 comments
Labels
feature request Issues that request new features to be added to Node.js. process Issues and PRs related to the process subsystem. wontfix Issues that will not be fixed. worker Issues and PRs related to Worker support.

Comments

@sindresorhus
Copy link

What is the problem this feature will solve?

Some test runners like AVA run tests files in separate worker threads, for improved performance and context isolation. It's common in tests to use process.chdir() to change the directory to be able to test how a certain thing works when running from a different place, or maybe you just want it to run in a temporary directory.

What is the feature you are proposing to solve the problem?

I'm aware process.chdir() works per process and not per thread, but maybe it would be possible to expose a process.withCwd(tempDirectory, () => {}) method, where the CWD change is only available inside the given synchronous callback?

What alternatives have you considered?

multi-process model + shared memory. That lets you support SharedArrayBuffer without the headaches of the multi-thread model.

@sindresorhus sindresorhus added the feature request Issues that request new features to be added to Node.js. label Jan 24, 2022
@Mesteery Mesteery added process Issues and PRs related to the process subsystem. worker Issues and PRs related to Worker support. feature request Issues that request new features to be added to Node.js. and removed feature request Issues that request new features to be added to Node.js. labels Jan 24, 2022
@Trott
Copy link
Member

Trott commented Jan 24, 2022

@nodejs/workers

@benjamingr
Copy link
Member

Currently all process.chdir does is call libuv's uv_chdir which in turn does chdir (or some weird windows hidden environment variable voodoo).

So I think the current working directory is typically a process-level concept at an operating system level - it looks like some but not all supported operating systems do support making threads with their own cwd (though it doesn't seem level) and the workaround has other complications.

From a quick google-stackoverflow it doesn't look possible with C# threads, Java threads, Python threads, rust threads.

@benjamingr
Copy link
Member

Doing this in userland (from the Kernel PoV not ours, that is, wrap all API calls to resolve paths and everywhere that can spawn a sub_processes) also seems like a pretty big undertaking with a lot of edge cases.

Given the complexity to do this on our side, the fact it's not possible on the OS side in a cross-platform way and lack of availability in other platforms I'm leaning towards -1 but it's entirely possible I'm missing something and I'll wait for more people to weigh in.

@bnoordhuis
Copy link
Member

To confirm: there's indeed no good, cross-platform way to do this except manual path wrangling. I suggest closing as wontfix.

@addaleax addaleax added the wontfix Issues that will not be fixed. label Feb 2, 2022
@addaleax
Copy link
Member

addaleax commented Feb 2, 2022

Yeah, it’s unfortunate, but this is mostly just a general limitation when using threads. I’ll close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. process Issues and PRs related to the process subsystem. wontfix Issues that will not be fixed. worker Issues and PRs related to Worker support.
Projects
None yet
Development

No branches or pull requests

6 participants