yarnpkg / yarn

The 1.x line is frozen - features and bugfixes now happen on https://github.com/yarnpkg/berry
https://classic.yarnpkg.com
Other
41.37k stars 2.72k forks source link

yarn bin points to incorrect path when package is scoped inside workspace. #8926

Open francoisgergaud opened 1 year ago

francoisgergaud commented 1 year ago

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

When yarn generate the cmd file for a executable defined in the bin section of the package.json, the path to the executable is set incorrectly as the scope folder is not in the path.

[4/4] Building fresh packages... error C:\\node_modules\packageA: Command failed. Exit code: 1 Command: yarn packageBUtil --verbose Arguments: Directory: C:\\node_modules\packageA Output: yarn run v1.22.19 $ C:\\node_modules\packageA\node_modules.bin\packageBUtil --verbose node:internal/modules/cjs/loader:936 throw err; ^

Error: Cannot find module 'C:\\node_modules\packageB\src\utility.js' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { code: 'MODULE_NOT_FOUND', requireStack: [] } error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

The path of the bin should be 'C:\\node_modules\@scope\packageB\src\utility.js', not 'C:\\node_modules\packageB\src\utility.js' .

If the current behavior is a bug, please provide the steps to reproduce.

All the steps described bellow are already available by cloning the following repository: https://github.com/francoisgergaud/debug-yarn-workspaces-bin/tree/main

  1. create a package with a workspace
  2. create 2 packages inside the workspaces folder, packageA and packageB. The packages must be scoped.
  3. Add an nodeJS file with the #!/usr/bin/env node shebang in packageB, add a command in the bin section of the package.json
  4. Add a dependency from packageA to packageB and call the command from packageB in the preinstall lifecycle script.
  5. run yarn from the root folder. It will fail saying the nodeJS file from packageB is not found when packageA calls it.

Please mention your node.js, yarn and operating system version.

Yarn 1.22.19, Node 16.18, Windows 10 x64 (we are stuck with yarn classic on our project).

Other:

It works when packageB is not scoped (the scope prefix is removed from the package's name, see https://github.com/francoisgergaud/debug-yarn-workspaces-bin/tree/without-scope). It also works on Linux with the scope prefix in the package's name.

francoisgergaud commented 1 year ago

I guess this is related to https://github.com/yarnpkg/yarn/issues/4564, but not 100% sure.