Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.1.0] Fix rule definition environment for repo rules #21397

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading