bazel: Bzlmod lockfile does not track repository mappings of extension `.bzl` files
Description of the bug:
When the .bzl files transitively loaded by a module extension don’t change, but their repo mapping does, a module extension locked in the lockfile can return stale results.
Which category does this issue belong to?
External Dependency
What’s the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
# MODULE.bazel
bazel_dep(name = "rules_go", version = "0.40.0")
ext = use_extension("//:ext.bzl", "ext")
ext.tag(label_str = "@rules_go//go")
use_repo(ext, "repo")
other_ext = use_extension("//:other_ext.bzl", "other_ext")
use_repo(other_ext, "other_repo")
# BUILD
load("@repo//:defs.bzl", "LABEL")
load("@other_repo//:defs.bzl", "STR")
print(LABEL)
print(STR)
# ext.bzl
def _repo_impl(ctx):
ctx.file("WORKSPACE")
ctx.file("BUILD")
ctx.file("defs.bzl", "LABEL = Label({})".format(repr(ctx.attr.label_str)))
_repo = repository_rule(
implementation=_repo_impl,
attrs={"label_str": attr.string(mandatory=True)},
)
_tag = tag_class(
attrs={"label_str": attr.string(mandatory=True)},
)
def _ext_impl(ctx):
for module in ctx.modules:
for tag in module.tags.tag:
_repo(name = "repo", label_str = tag.label_str)
ext = module_extension(
implementation=_ext_impl,
tag_classes={"tag": _tag},
)
# other_ext.bzl
load("@repo//:defs.bzl", "LABEL")
def _other_repo_impl(ctx):
ctx.file("WORKSPACE")
ctx.file("BUILD")
ctx.file("defs.bzl", "STR = {}".format(repr(ctx.attr.str)))
_other_repo = repository_rule(
implementation = _other_repo_impl,
attrs = {
"str": attr.string(),
},
)
def _other_ext_impl(_):
_other_repo(name = "other_repo", str = str(LABEL))
other_ext = module_extension(
implementation = _other_ext_impl,
)
$ bazel build //...
DEBUG: /home/fhenneke/tmp/repo-mapping-lock/BUILD:3:6: @@rules_go~0.40.0//go:go
DEBUG: /home/fhenneke/tmp/repo-mapping-lock/BUILD:4:6: @@rules_go~0.40.0//go:go
$ buildozer 'set version 0.41.0' MODULE.bazel:rules_go
$ bazel build //...
DEBUG: /home/fhenneke/tmp/repo-mapping-lock/BUILD:3:6: @@rules_go~0.41.0//go:go
DEBUG: /home/fhenneke/tmp/repo-mapping-lock/BUILD:4:6: @@rules_go~0.40.0//go:go
$ bazel build //... --lockfile_mode=off
DEBUG: /home/fhenneke/tmp/repo-mapping-lock/BUILD:3:6: @@rules_go~0.41.0//go:go
DEBUG: /home/fhenneke/tmp/repo-mapping-lock/BUILD:4:6: @@rules_go~0.41.0//go:go
$ buildozer 'set version 0.40.0' MODULE.bazel:rules_go
$ bazel build //... --lockfile_mode=off
DEBUG: /home/fhenneke/tmp/repo-mapping-lock/BUILD:3:6: @@rules_go~0.40.0//go:go
DEBUG: /home/fhenneke/tmp/repo-mapping-lock/BUILD:4:6: @@rules_go~0.40.0//go:go
Which operating system are you running Bazel on?
No response
What is the output of bazel info release?
7.0.0 and HEAD
If bazel info release returns development version or (@non-git), tell us how you built Bazel.
No response
What’s the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?
No response
Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.
This is a regression in 7.0.0 since the lockfile is now enabled by default.
Have you found anything relevant by searching the web?
The reproducer is a synthetic, simplified version of the original problem (https://bazelbuild.slack.com/archives/CDBP88Z0D/p1703062323776219), which affects rules_go.
Any other information, logs, or outputs that you want to share?
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (9 by maintainers)
Commits related to this issue
- Let module extensions track calls to `Label()`... ... that use repo mapping. This is a rather obscure case of the lockfile being stale; if the `Label()` constructor is called in an extension impl fun... — committed to bazelbuild/bazel by Wyverald 3 years ago
- Let module extensions track calls to `Label()` ... that use repo mapping. This is a rather obscure case of the lockfile being stale; if the `Label()` constructor is called in an extension impl functi... — committed to iancha1992/bazel by Wyverald 3 years ago
- [7.0.1] Let module extensions track calls to `Label()` (#20750) ... that use repo mapping. This is a rather obscure case of the lockfile being stale; if the `Label()` constructor is called in an ext... — committed to bazelbuild/bazel by iancha1992 3 years ago
- Let .bzl files record their usages of repo mapping In the same vein as https://github.com/bazelbuild/bazel/pull/20742, we record all repo mapping entries used during the load of a .bzl file too, incl... — committed to bazelbuild/bazel by Wyverald 3 years ago
- Let .bzl files record their usages of repo mapping In the same vein as https://github.com/bazelbuild/bazel/pull/20742, we record all repo mapping entries used during the load of a .bzl file too, incl... — committed to bazelbuild/bazel by Wyverald 3 years ago
- Let .bzl files record their usages of repo mapping In the same vein as https://github.com/bazelbuild/bazel/pull/20742, we record all repo mapping entries used during the load of a .bzl file too, incl... — committed to bazelbuild/bazel by Wyverald 3 years ago
- [7.0.1] Let .bzl files record their usages of repo mapping (#20848) In the same vein as https://github.com/bazelbuild/bazel/pull/20742, we record all repo mapping entries used during the load of a .... — committed to bazelbuild/bazel by Wyverald 3 years ago
- Let module extensions track calls to `Label()` ... that use repo mapping. This is a rather obscure case of the lockfile being stale; if the `Label()` constructor is called in an extension impl functi... — committed to bazelbuild/bazel by Wyverald 3 years ago
- Let .bzl files record their usages of repo mapping In the same vein as https://github.com/bazelbuild/bazel/pull/20742, we record all repo mapping entries used during the load of a .bzl file too, incl... — committed to bazelbuild/bazel by Wyverald 3 years ago
- Let module extensions track calls to `Label()` ... that use repo mapping. This is a rather obscure case of the lockfile being stale; if the `Label()` constructor is called in an extension impl functi... — committed to Silic0nS0ldier/bazel by Wyverald 3 years ago
- Let .bzl files record their usages of repo mapping In the same vein as https://github.com/bazelbuild/bazel/pull/20742, we record all repo mapping entries used during the load of a .bzl file too, incl... — committed to Silic0nS0ldier/bazel by Wyverald 3 years ago
- Make repo marker files sensitive to repo mapping changes Similar to the fix for https://github.com/bazelbuild/bazel/issues/20721, we write recorded repo mapping entries into the marker file so that r... — committed to bazelbuild/bazel by Wyverald 3 years ago
- Make repo marker files sensitive to repo mapping changes Similar to the fix for https://github.com/bazelbuild/bazel/issues/20721, we write recorded repo mapping entries into the marker file so that r... — committed to bazelbuild/bazel by Wyverald 3 years ago
- Make repo marker files sensitive to repo mapping changes Similar to the fix for https://github.com/bazelbuild/bazel/issues/20721, we write recorded repo mapping entries into the marker file so that r... — committed to bazel-io/bazel by Wyverald 3 years ago
- [7.1.0] Make repo marker files sensitive to repo mapping changes (#21172) Similar to the fix for https://github.com/bazelbuild/bazel/issues/20721, we write recorded repo mapping entries into the mark... — committed to bazelbuild/bazel by bazel-io 3 years ago
Hmm, this is actually slightly trickier than I thought. In particular, we also need to deal with repo-relative
Label()calls.Consider this scenario:
@@//:foo.bzlloads from@bar//:bar.bzl, which initially maps to@@bar~1.0//:bar.bzlbarfrom 1.0 to 2.0, and change nothing else@@//:foo.bzlnow actually loads from@@bar~2.0//:bar.bzlbar.bzlactually contains aMY_CONSTANT = Label("//:whatever"), its value will have changed by the upgrade, despite no bzl digest having changed, or any repo mapping from@@bar~1.0or@@bar~2.0having changedTo deal with this scenario, we could either consider
load()s a “use” of a repo mapping and track it just like aLabel()call, or we could store the “source repo” for every .bzl file as part of its digest. The latter approach is easier to implement but has a larger blast radius (affects BUILD-time loads as well), so I think I’ll go with the first approach.Beyond that, we also need to be careful while verifying the up-to-dateness of stored repo mapping entries. In the scenario above, if
@@bar~1.0//:bar.bzlcontained aLabel("@quux//:whatever)call, then the entry<@@bar~1.0, quux, @@quux~1.0>would be stored. After upgradingbarto 2.0, though, trying to verify this entry could cause a crash as@@bar~1.0doesn’t even exist anymore, so requesting its repo mapping just causes an exception.@bazel-io fork 7.1.0
@bazel-io fork 7.0.1
@bazel-io flag