execa: `preferLocal` doesn't work for if dependent projects are using `pnpm`, `yarn pnp` or `deno install`

This was initially reported as a bug in my library:

But after further investigation, I think this should be addressed in execa.

I built a reproduction:

  1. https://github.com/felipecrs/repro-execa-pnpm
  2. https://github.com/felipecrs/repro-execa-pnpm-child
$ git clone https://github.com/felipecrs/repro-execa-pnpm-child

$ cd repro-execa-pnpm-child

$ npm install

# works
$ node index.js
0.10.5

$ rm -rf node_modules package-lock.json
$ pnpm install
$ node index.js
0.10.5
file:///home/felipecrs/repos/repro-execa-pnpm/project-a/node_modules/.pnpm/execa@9.6.0/node_modules/execa/lib/return/final-error.js:6
        return new ErrorClass(message, options);
               ^

ExecaError: Command failed with EACCES: ovsx --version
spawn ovsx EACCES
    at getFinalError (file:///home/felipecrs/repos/repro-execa-pnpm/project-a/node_modules/.pnpm/execa@9.6.0/node_modules/execa/lib/return/final-error.js:6:9)
    at makeError (file:///home/felipecrs/repos/repro-execa-pnpm/project-a/node_modules/.pnpm/execa@9.6.0/node_modules/execa/lib/return/result.js:108:16)
    at getAsyncResult (file:///home/felipecrs/repos/repro-execa-pnpm/project-a/node_modules/.pnpm/execa@9.6.0/node_modules/execa/lib/methods/main-async.js:168:4)
    at handlePromise (file:///home/felipecrs/repos/repro-execa-pnpm/project-a/node_modules/.pnpm/execa@9.6.0/node_modules/execa/lib/methods/main-async.js:151:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async main (file:///home/felipecrs/repos/repro-execa-pnpm/project-a/index.js:4:5)
    at async file:///home/felipecrs/repos/repro-execa-pnpm/project-b/index.js:3:1 {
  shortMessage: 'Command failed with EACCES: ovsx --version\nspawn ovsx EACCES',
  originalMessage: 'spawn ovsx EACCES',
  command: 'ovsx --version',
  escapedCommand: 'ovsx --version',
  cwd: '/home/felipecrs/repos/repro-execa-pnpm/project-b',
  durationMs: 70.879539,
  failed: true,
  timedOut: false,
  isCanceled: false,
  isGracefullyCanceled: false,
  isTerminated: false,
  isMaxBuffer: false,
  isForcefullyTerminated: false,
  code: 'EACCES',
  stdio: [ undefined, undefined, undefined ],
  ipcOutput: [],
  pipedFrom: [],
  [cause]: Error: spawn ovsx EACCES
      at ChildProcess._handle.onexit (node:internal/child_process:285:19)
      at onErrorNT (node:internal/child_process:483:16)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    errno: -13,
    code: 'EACCES',
    syscall: 'spawn ovsx',
    path: 'ovsx',
    spawnargs: [ '--version' ]
  }
}

It would be super nice if execa itself could provide a solution for it.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

I don’t think we should do anything here. It’s not possible to implement it reliably and our target is Node.js with npm. We can maybe succinctly document using localDir: import.meta.dirname, for those package managers.

I’m okay with this conclusion, btw.

Thanks for explaining @felipecrs, this makes sense.

I would lead towards not documenting it since it is a specific scenario, which we would still need to thoroughly test because documenting. But if more users are encountering this issue, then we can reconsider adding some documentation.

What do you think @sindresorhus?

I found a “solution”:

export async function main() {
  await execa("open-cli", ["--version"], {
    preferLocal: true,
+   localDir: import.meta.dirname,
    stdio: "inherit",
  });
}