Skip to content

Re-patch crate dependency if patch file changes#3994

Open
erenon wants to merge 2 commits intobazelbuild:mainfrom
erenon:watch-patch
Open

Re-patch crate dependency if patch file changes#3994
erenon wants to merge 2 commits intobazelbuild:mainfrom
erenon:watch-patch

Conversation

@erenon
Copy link
Copy Markdown
Contributor

@erenon erenon commented Apr 28, 2026

Expected behavior

Given a crate annotation:

crate.annotation(
    crate = "some_dependency",
    patches = ["custom.patch"],
    repositories = ["crates"],
)

When "custom.patch" changes, the next build should re-apply the patch to the dependency.

Actual behavior

The build cache is not invalidated if "custom.patch" changes in any ways. Especially problematic with RBE.

Workaround

Rename custom.patch to custom2.patch

This change

The first commit tells bazel about the used patch files, so they appear in the MODULE.bazel.lock file. This way, when the patch file changes, a splice is triggered.

The second commit computes the hash of the contents of the patch files, and specifies that hash as the "canonical_id" of the generated http_archive

A canonical ID of the file downloaded. If specified and non-empty, Bazel will not take the file from cache, unless it was added to the cache by a request with the same canonical ID. If unspecified or empty, Bazel by default uses the URLs of the file as the canonical ID. This helps catch the common mistake of updating the URLs without also updating the hash, resulting in builds that succeed locally but fail on machines without the file in the cache.

I'm not sure if this is the correct way to fix this, but at least it works.

Also, in crate_universe/extensions.bzl, repo['patches'] is always None, so I'm parsing it from the annotations. I'm not sure if this is the best way either. Maybe some other parts fails to set repo.patches, can't tell.

Tested with Bazel Build label: 9.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant