Skip to content

Commit

Permalink
Fix rule definition environment for repo rules
Browse files Browse the repository at this point in the history
See bazelbuild#21131 (comment)

Closes bazelbuild#21390.

PiperOrigin-RevId: 607821270
Change-Id: I031222ee80c4db4ca50372c549e2b522ac606cb7
  • Loading branch information
Wyverald authored and bazel-io committed Feb 17, 2024
1 parent 59ab922 commit c8f8291
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,20 @@ public StarlarkCallable repositoryRule(
}
}
builder.setConfiguredTargetFunction(implementation);
// TODO(b/291752414): If we care about the digest of repository rules, we should be using the
// transitive bzl digest of the module of the outermost stack frame, not the innermost.
BazelModuleContext moduleContext = BazelModuleContext.ofInnermostBzlOrThrow(thread);
builder.setRuleDefinitionEnvironmentLabelAndDigest(
moduleContext.label(), moduleContext.bzlTransitiveDigest());
var threadContext = BazelStarlarkContext.fromOrFail(thread);
if (threadContext instanceof BzlInitThreadContext) {
var bzlInitContext = (BzlInitThreadContext) threadContext;
builder.setRuleDefinitionEnvironmentLabelAndDigest(
bzlInitContext.getBzlFile(), bzlInitContext.getTransitiveDigest());
} else {
// TODO: this branch is wrong, but cannot be removed until we deprecate WORKSPACE because
// WORKSPACE can currently call unexported repo rules (so there's potentially no
// BzlInitThreadContext. See
// https://github.com/bazelbuild/bazel/pull/21131#discussion_r1471924084 for more details.
BazelModuleContext moduleContext = BazelModuleContext.ofInnermostBzlOrThrow(thread);
builder.setRuleDefinitionEnvironmentLabelAndDigest(
moduleContext.label(), moduleContext.bzlTransitiveDigest());
}
Label.RepoMappingRecorder repoMappingRecorder =
thread.getThreadLocal(Label.RepoMappingRecorder.class);
if (repoMappingRecorder != null) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/shell/bazel/starlark_repository_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,39 @@ function test_starlark_repository_bzl_invalidation_batch() {
bzl_invalidation_test_template --batch
}

function test_starlark_repo_bzl_invalidation_wrong_digest() {
# regression test for https://github.com/bazelbuild/bazel/pull/21131#discussion_r1471924084
create_new_workspace
cat > MODULE.bazel <<EOF
ext = use_extension("//:r.bzl", "ext")
use_repo(ext, "r")
EOF
touch BUILD
cat > r.bzl <<EOF
load(":make_repo_rule.bzl", "make_repo_rule")
def _r(rctx):
print("I'm here")
rctx.file("BUILD", "filegroup(name='r')")
r=make_repo_rule(_r)
ext=module_extension(lambda mctx: r(name='r'))
EOF
cat > make_repo_rule.bzl << EOF
def make_repo_rule(impl):
return repository_rule(impl)
EOF

bazel build @r >& $TEST_log || fail "Failed to build"
expect_log "I'm here"

cat <<EOF >>r.bzl
# Just add a comment
EOF
# the above SHOULD trigger a refetch.
bazel build @r >& $TEST_log || fail "failed to build"
expect_log "I'm here"
}

# Test invalidation based on change to the bzl files
function file_invalidation_test_template() {
local startup_flag="${1-}"
Expand Down

0 comments on commit c8f8291

Please sign in to comment.