Skip to content

[Bug?]: Patching a package that has already been patched creates a "patch of a patch" #3851

@johndiiorio

Description

@johndiiorio

Self-service

  • I'd be willing to implement a fix

Describe the bug

Running yarn patch against a package that has already been patched has unexpected behavior, sometimes creating a "patch of a patch". This is undesired, as I would like Yarn to merge my patch changes together, if applicable.

To reproduce

I'll use lodash for the examples but I believe this applies to any package.

Issue (1):

  1. Start a new Yarn project and add a package: yarn set version berry && yarn add lodash
  2. Run yarn patch lodash. Make a change in the extracted directory, then run yarn patch-commit -s <dir> && yarn.
  3. Run yarn patch lodash again. At this point, I would expect to get the error message "Multiple candidate packages found" asking whether I'd like to patch the unpatched version or patched version of lodash, but no such message appears. The unpatched source of lodash is extracted.

Issue (2):

  1. Start a new Yarn project: yarn set version berry.
  2. Create a workspace and add a package to one of them: mkdir my-workspace && touch my-workspace/package.json
  3. Add "workspaces": ["my-workspace"] to the root package.json
  4. Add the following to the my-workspace/package.json:
{
    "name": "my-workspace",
    "dependencies": {
        "lodash": "4.17.21
    },
}
  1. Run yarn patch lodash, make a change in the extracted directory, then run yarn patch-commit -s <dir> && yarn
  2. Run yarn patch lodash again, note that unlike in (1), the "Multiple candidate packages found" error message is displayed.
  3. Run yarn patch lodash@patch:lodash@npm..., make a change in the extracted directory, then run yarn patch-commit -s <dir>
  4. Note that there are now two patches in .yarn/patches, and the package.json resolutions field implies a "patch of a patch"
"resolutions": {
    "lodash@4.17.21": "patch:lodash@npm%3A4.17.21#.yarn/patches/lodash-npm-4.17.21-6382451519::version=4.17.21&hash=b68992&locator=root-workspace-0b6124%40workspace%3A.#.yarn/patches/lodash-patch-af7531f881
}
  1. Running yarn at this point prints an error: "Patch locators must explicitly define their source".

Environment

System:
  OS: Linux 5.15 Arch Linux
  CPU: (16) x64 AMD Ryzen 7 PRO 5850U with Radeon Graphics
Binaries:
  Node: 17.1.0 - /tmp/xfs-57237fea/node
  Yarn: 3.1.1 - /tmp/xfs-57237fea/yarn

Additional context

It is possible to work around (2) by selecting the unpatched version of the package, remaking all previous patch changes, then adding in any new changes. However, I would expect that Yarn would handle this case automatically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingupholdedReal issues without formal reproduction

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions