diff --git a/lib/util/resolveCommand.js b/lib/util/resolveCommand.js index c46d8d7..23e0fd5 100644 --- a/lib/util/resolveCommand.js +++ b/lib/util/resolveCommand.js @@ -4,11 +4,22 @@ const path = require('path'); const which = require('which'); const pathKey = require('path-key')(); +let isMainThread; + +try { + isMainThread = require('worker_threads').isMainThread; +} catch (error) { + // Worker threads are not supported in the current node version + // because of this we're always in the main thread + isMainThread = true; +} + function resolveCommandAttempt(parsed, withoutPathExt) { const cwd = process.cwd(); const hasCustomCwd = parsed.options.cwd != null; - // Worker threads do not have process.chdir() - const shouldSwitchCwd = hasCustomCwd && process.chdir !== undefined; + // Worker threads do not have process.chdir() so we should only use it if + // we're running in the main thread + const shouldSwitchCwd = hasCustomCwd && isMainThread; // If a custom `cwd` was specified, we need to change the process cwd // because `which` will do stat calls but does not support a custom cwd