Support `process.chdir` in worker thread
sindresorhus opened this issue · 5 comments
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.
@nodejs/workers
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.
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.
To confirm: there's indeed no good, cross-platform way to do this except manual path wrangling. I suggest closing as wontfix.
Yeah, it’s unfortunate, but this is mostly just a general limitation when using threads. I’ll close this.