yarnpkg / berry

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

[Bug?]: constraints + `workspaces focus` cause erroneous failures #5992

Open seansfkelley opened 10 months ago

seansfkelley commented 10 months ago

Self-service

Describe the bug

If I have a constraint on e.g. the presence of a script, workspaces focus will cause that constraint to fail because as part of its implementation it mangles the package manifests:

https://github.com/yarnpkg/berry/blob/db6210f48355d2986e965f90009b22f18d3b6342/packages/plugin-workspace-tools/sources/commands/focus.ts#L83-L96C6

To reproduce

(The link to Sherlock is broken, and when I dug up an old version it appears to also be broken.)

I put a reproduction up at https://github.com/seansfkelley/yarn-repro

The important parts are:

[root package.json]
{
  "name": "yarn-repro",
  "workspaces": {
    "packages": [
      "packages/*"
    ]
  },
  "packageManager": "yarn@4.0.2"
}

[yarn.config.cjs]
module.exports = {
  constraints({ Yarn }) {
    for (const w of Yarn.workspaces({ ident: "a" })) {
      w.set("scripts.start", "exit 0");
    }
  },
};

[packages/a/package.json]
{
  "name": "a",
  "scripts": {
    "start": "exit 0"
  }
}

[packages/b/package.json]
{
  "name": "b"
}

[.yarnrc.yml]
enableConstraintsChecks: true

Then reproduce the issue with yarn workspaces focus b.

Environment

System:
    OS: macOS 13.6
    CPU: (12) arm64 Apple M2 Pro
  Binaries:
    Node: 20.9.0 - /private/var/folders/wg/zn78dyk51h5bvz0crq8x8w0h0000gp/T/xfs-3399e843/node
    Yarn: 4.0.2 - /private/var/folders/wg/zn78dyk51h5bvz0crq8x8w0h0000gp/T/xfs-3399e843/yarn
    npm: 10.1.0 - ~/.volta/tools/image/node/20.9.0/bin/npm

Additional context

No response

seansfkelley commented 10 months ago

At first blush I would expect constraints to be checked, since workspaces focus is kind-of an install, even if it's not literally install. But I think an argument could be made that it shouldn't, in which case, the resolution could be to punch a hole in installWithNewReport to permit skipping constraint checks. This sounds easier than figuring out how to restore the mangled manifests in time to allow the constraints to be checked normally.