yarnpkg / berry

📦🐈 Active development trunk for Yarn ⚒
https://yarnpkg.com
BSD 2-Clause "Simplified" License
7.42k stars 1.11k forks source link

[Bug?]: @yarnpkg/cli runExit cwd parameter is ignored #6405

Open legobeat opened 3 months ago

legobeat commented 3 months ago

Self-service

Describe the bug

There seems to be no way to actually instrument the runExit function of @yarnpkg/cli to run on anything else but the project inferred as entrypoint.

The function has a cwd option but changing it seems to have no effect on where and how the command is run.

To reproduce

There is a reproduction here: https://github.com/legobeat/yarnpkg-cli-test/blob/main/src/index.js

The results can be observed here.

The above repository contains two packages. While one resides under a subdirectory of the other, they are not related through a shared workspace but completely independent projects and packages.

The foo package has a sayhello package script which we want to call from the yarnpkg-cli-test package.

When attempting to call runExit(['run', 'sayhello', { cwd: PATH_TO_foo }), yarn does not recognize the script. Upon further investigation, it seems like any of the cwd parameters are actually ineffective. The undocumented selfPath parameter appears to be intended to be used to override the path to the yarn binary and unrelated.

Is there something we're missing in order to be able to instrument execution of package scripts from arbitrary packagesd?

Environment

N/A

Additional context

No response

legobeat commented 3 months ago

Noticed that runExit is also exposed as an undocumented instance method on the Cli object returned from getCli. Trying to call that instead yields a different error which makes me think we shouldn't be calling the instance method in the first place.

[Example](https://github.com/legobeat/yarnpkg-cli-test/blob/as-instance/src/run-from-cli-instance.js) [Test result](https://github.com/legobeat/yarnpkg-cli-test/actions/runs/10070631658/job/27839452928#step:6:30) ``` Type Error: Cannot read properties of undefined (reading 'plugins') at Configuration.find (/home/runner/work/yarnpkg-cli-test/yarnpkg-cli-test/node_modules/@yarnpkg/core/lib/Configuration.js:916:55) at async RunCommand.execute (/home/runner/work/yarnpkg-cli-test/yarnpkg-cli-test/node_modules/@yarnpkg/plugin-essentials/lib/commands/run.js:38:31) at async RunCommand.validateAndExecute (/home/runner/work/yarnpkg-cli-test/yarnpkg-cli-test/node_modules/clipanion/lib/advanced/Command.js:73:26) at async Cli.run (/home/runner/work/yarnpkg-cli-test/yarnpkg-cli-test/node_modules/clipanion/lib/advanced/Cli.js:229:24) at async Cli.runExit (/home/runner/work/yarnpkg-cli-test/yarnpkg-cli-test/node_modules/clipanion/lib/advanced/Cli.js:238:28) at async main (/home/runner/work/yarnpkg-cli-test/yarnpkg-cli-test/src/run-from-cli-instance.js:10:3) ```