Skip to content

Commit fd60614

Browse files
brandjonlaurentlb
authored andcommitted
In repo rules, don't warn about generator_* attributes being non-canonical
A recent change added the implicit generator_* attributes ("generator_name", "generator_function", and possibly "generator_location") to all repo rules. This had the unfortunate side-effect of spamming all users of certain repo rules with a warning that the values for these attributes are non-canonical. Since these attributes are implicitly created and supplied by Bazel itself, and since they do not (or should not) affect repo rule determinism, there is no action needed on the part of the user. This change hardcodes them to be excluded from the canonical warning. Fixes #11040. Closes #11113. PiperOrigin-RevId: 306478566
1 parent 71fb56b commit fd60614

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/main/java/com/google/devtools/build/lib/bazel/repository/RepositoryResolvedEvent.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.google.common.collect.ImmutableList;
2525
import com.google.common.collect.ImmutableMap;
26+
import com.google.common.collect.ImmutableSet;
2627
import com.google.devtools.build.lib.events.ExtendedEventHandler.ResolvedEvent;
2728
import com.google.devtools.build.lib.packages.Attribute;
2829
import com.google.devtools.build.lib.packages.Rule;
@@ -244,18 +245,32 @@ public static String getRuleDefinitionInformation(Rule rule) {
244245
return buf.toString();
245246
}
246247

248+
/**
249+
* Attributes that may be defined on a repository rule without affecting its canonical
250+
* representation. These may be created implicitly by Bazel.
251+
*/
252+
private static final ImmutableSet<String> IGNORED_ATTRIBUTE_NAMES =
253+
ImmutableSet.of("generator_name", "generator_function", "generator_location");
254+
247255
/**
248256
* Compare two maps from Strings to objects, returning a pair of the map with all entries not in
249257
* the original map or in the original map, but with a different value, and the keys dropped from
250258
* the original map. However, ignore changes where a value is explicitly set to its default.
259+
*
260+
* <p>Ignores attributes listed in {@code IGNORED_ATTRIBUTE_NAMES}.
251261
*/
252262
static Pair<Map<String, Object>, List<String>> compare(
253263
Map<String, Object> orig, Map<String, Object> defaults, Map<?, ?> modified) {
254264
ImmutableMap.Builder<String, Object> valuesChanged = ImmutableMap.<String, Object>builder();
255265
for (Map.Entry<?, ?> entry : modified.entrySet()) {
256266
if (entry.getKey() instanceof String) {
257-
Object value = entry.getValue();
258267
String key = (String) entry.getKey();
268+
if (IGNORED_ATTRIBUTE_NAMES.contains(key)) {
269+
// The dict returned by the repo rule really shouldn't know about these anyway, but
270+
// for symmetry we'll ignore them if they happen to be present.
271+
continue;
272+
}
273+
Object value = entry.getValue();
259274
Object old = orig.get(key);
260275
if (old == null) {
261276
Object defaultValue = defaults.get(key);
@@ -271,6 +286,9 @@ static Pair<Map<String, Object>, List<String>> compare(
271286
}
272287
ImmutableList.Builder<String> keysDropped = ImmutableList.<String>builder();
273288
for (String key : orig.keySet()) {
289+
if (IGNORED_ATTRIBUTE_NAMES.contains(key)) {
290+
continue;
291+
}
274292
if (!modified.containsKey(key)) {
275293
keysDropped.add(key);
276294
}

src/test/shell/bazel/workspace_resolved_test.sh

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,7 @@ time_rule(name="timestamprepo")
824824
EOF
825825

826826
bazel sync --distdir=${EXTREPODIR}/test_WORKSPACE/distdir --experimental_repository_resolved_file=resolved.bzl
827+
cat resolved.bzl > /dev/null || fail "resolved.bzl should exist"
827828
bazel sync --distdir=${EXTREPODIR}/test_WORKSPACE/distdir --experimental_repository_hash_file=`pwd`/resolved.bzl \
828829
--experimental_verify_repository_rules='//:rule.bzl%time_rule' \
829830
> "${TEST_log}" 2>&1 && fail "expected failure" || :
@@ -1216,4 +1217,52 @@ EOF
12161217
expect_log " TEST_TMPDIR/.*/external/bazel_tools/tools/build_defs/repo/http.bzl:"
12171218
}
12181219

1220+
# Regression test for #11040.
1221+
#
1222+
# Test that a canonical repo warning is generated for explicitly specified
1223+
# attributes whose values differ, and that it is never generated for implicitly
1224+
# created attributes (in particular, the generator_* attributes).
1225+
test_canonical_warning() {
1226+
EXTREPODIR=`pwd`
1227+
tar xvf ${TEST_SRCDIR}/test_WORKSPACE_files/archives.tar
1228+
1229+
mkdir main
1230+
touch main/BUILD
1231+
cat > main/reporule.bzl <<EOF
1232+
def _impl(repository_ctx):
1233+
repository_ctx.file("a.txt", "A")
1234+
repository_ctx.file("BUILD.bazel", "exports_files(['a.txt'])")
1235+
# Don't include "name", test that we warn about it below.
1236+
return {"myattr": "bar"}
1237+
1238+
reporule = repository_rule(
1239+
implementation = _impl,
1240+
attrs = {
1241+
"myattr": attr.string()
1242+
})
1243+
1244+
# We need to use a macro for the generator_* attributes to be defined.
1245+
def instantiate_reporule(name, **kwargs):
1246+
reporule(name=name, **kwargs)
1247+
EOF
1248+
cat > main/WORKSPACE <<EOF
1249+
workspace(name = "main")
1250+
load("//:reporule.bzl", "instantiate_reporule")
1251+
instantiate_reporule(
1252+
name = "myrepo",
1253+
myattr = "foo"
1254+
)
1255+
EOF
1256+
1257+
cd main
1258+
# We should get a warning for "myattr" having a changed value and for "name"
1259+
# being dropped, but not for the generator_* attributes.
1260+
bazel sync --distdir=${EXTREPODIR}/test_WORKSPACE/distdir >/dev/null 2>$TEST_log
1261+
bazel clean --expunge
1262+
expect_log "Rule 'myrepo' indicated that a canonical reproducible form \
1263+
can be obtained by modifying arguments myattr = \"bar\" and dropping \
1264+
\[.*\"name\".*\]"
1265+
expect_not_log "Rule 'myrepo' indicated .*generator"
1266+
}
1267+
12191268
run_suite "workspace_resolved_test tests"

0 commit comments

Comments
 (0)