yarnpkg / berry

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

[Bug?]: yarn workspaces foreach --all also executes script against workspace root #6190

Open chenxinyanc opened 6 months ago

chenxinyanc commented 6 months ago

Self-service

Describe the bug

Suppose I'm working with a monorepo set up with yarn workspace, and there is a build script named test defined in the package.json of some workspace child packages.

Now I want to trigger tests of every workspace child packages in one go from the workspace root, so I've added the following script in the workspace root-level package.json file

"scripts": {
  "test": "yarn workspaces foreach --parallel --interlaced --verbose --all run test --run",
  "test:coverage": "yarn workspaces foreach --parallel --interlaced --verbose --all run test --run --coverage",
}

If I run yarn test:coverage at workspace root, I'll find the test script from workspace root-level package is also executed (i.e., the yarn workspaces foreach --parallel --interlaced --verbose --all run test --run, resulting the test scripts of each child packages being executed twice!

I'm not sure whether this is a bug or by design, but could you please provide some suggestions on this?

To reproduce

n.b.: The link https://yarnpkg.com/advanced/sherlock seems to be broken

Here is the repo for reproduction: https://github.com/chenxinyanc/yarn-v4-workspace-foreach-args

Environment

System:
    OS: Windows 11 10.0.26090
    CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11700 @ 2.50GHz
  Binaries:
    Node: 20.8.0 - ~\AppData\Local\Temp\xfs-cd9efe04\node.CMD
    Yarn: 4.1.1 - ~\AppData\Local\Temp\xfs-cd9efe04\yarn.CMD
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD

Additional context

No response

clemyan commented 5 months ago

The project root is indeed a workspace, so --all will include it. yarn workspace foreach --all --exclude . should work well enough, so probably not worth adding another flag for excluding current workspace.

chenxinyanc commented 5 months ago

The project root is indeed a workspace, so --all will include it. yarn workspace foreach --all --exclude . should work well enough, so probably not worth adding another flag for excluding current workspace.

Thanks for the suggestion! I think --exclude . looks more general than --exclude <root-package-name> and I that should resolve my problem here.

While I may not be able to contribute for now, may I suggest adding some documentation/notice on this potential pitfall?