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

Does not work inside workers threads with custom current working directory #130

Closed
Lakitna opened this issue Apr 3, 2020 · 4 comments · Fixed by #132
Closed

Does not work inside workers threads with custom current working directory #130

Lakitna opened this issue Apr 3, 2020 · 4 comments · Fixed by #132

Comments

@Lakitna
Copy link

Lakitna commented Apr 3, 2020

I'm using node-cross-spawn via execa inside a worker thread.

I seem to have found an issue related to #127 that only pops up if you set a custom cwd.

I've traced the error back to this line: https://github.com/moxystudio/node-cross-spawn/blob/master/lib/util/resolveCommand.js#L11

When we're running in a worker thread process.chdir is not undefined. Instead, it's a named function.

console.log(process.chdir);
// [Function: unavailableInWorker] { disabled: true }

Therefore shouldSwitchCwd is true when running in a worker thread with custom cwd.

I can see one reliable way of improving this check:

const { isMainThread } = require('worker_threads');

const shouldSwitchCwd = hasCustomCwd && isMainThread;

Though I'm not sure what would happen to the cwd option and how it would affect things. I'm not very well versed in these matters. These changes do fix the issue for me in any case so I'll make a PR for this change.

@satazor
Copy link
Contributor

satazor commented Apr 3, 2020

Thanks @Lakitna for the PR.

Can you check this out @ehmicky?

@ehmicky
Copy link
Contributor

ehmicky commented Apr 3, 2020

Fixed in #132.

@satazor
Copy link
Contributor

satazor commented Apr 3, 2020

@Lakitna it seems that @ehmicky’s PR is more correct, can you check it it solved the issue you are having before merging?

@Lakitna
Copy link
Author

Lakitna commented Apr 3, 2020

Yeah, that looks much nicer.

It works just fine on node@13 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants