Skip to content

Commit 321fe3b

Browse files
philsccopybara-github
authored andcommitted
Prevent --repo_env from triggering unnecessary fetches
It looks like `--repo_env` entries get added to every repository rule's marker data. That is regardless of whether the repository rule declared that it uses the environment variable. This causes unnecessary fetches of the external repositories. This patch aims to fix that by making repository rules only depend on `--repo_env` values that they specify in their `environ` attributes. I also took this opportunity to fix up all instances of `bazel-genfiles` in `starlark_repository_test.sh` to `bazel-bin`. Closes #13126. PiperOrigin-RevId: 361813865
1 parent e7a0a71 commit 321fe3b

File tree

2 files changed

+77
-26
lines changed

2 files changed

+77
-26
lines changed

src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,13 @@ protected Map<String, String> declareEnvironmentDependencies(
331331
return null;
332332
}
333333

334+
// Only depend on --repo_env values that are specified in the "environ" attribute.
334335
Map<String, String> repoEnv = new LinkedHashMap<String, String>(environ);
335-
for (Map.Entry<String, String> value : repoEnvOverride.entrySet()) {
336-
repoEnv.put(value.getKey(), value.getValue());
336+
for (String key : keys) {
337+
String value = repoEnvOverride.get(key);
338+
if (value != null) {
339+
repoEnv.put(key, value);
340+
}
337341
}
338342

339343
// Add the dependencies to the marker file
@@ -362,9 +366,13 @@ protected boolean verifyEnvironMarkerData(Map<String, String> markerData, Enviro
362366
return false;
363367
}
364368

369+
// Only depend on --repo_env values that are specified in the "environ" attribute.
365370
Map<String, String> repoEnv = new LinkedHashMap<>(environ);
366-
for (Map.Entry<String, String> value : repoEnvOverride.entrySet()) {
367-
repoEnv.put(value.getKey(), value.getValue());
371+
for (String key : keys) {
372+
String value = repoEnvOverride.get(key);
373+
if (value != null) {
374+
repoEnv.put(key, value);
375+
}
368376
}
369377

370378
// Verify that all environment variable in the marker file are also in keys

src/test/shell/bazel/starlark_repository_test.sh

Lines changed: 65 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,15 @@ EOF
120120
bazel build //zoo:ball-pit1 >& $TEST_log || fail "Failed to build"
121121
expect_log "bleh"
122122
expect_log "Tra-la!" # Invalidation
123-
cat bazel-genfiles/zoo/ball-pit1.txt >$TEST_log
123+
cat bazel-bin/zoo/ball-pit1.txt >$TEST_log
124124
expect_log "Tra-la!"
125125

126126
bazel build //zoo:ball-pit1 >& $TEST_log || fail "Failed to build"
127127
expect_not_log "Tra-la!" # No invalidation
128128

129129
bazel build //zoo:ball-pit2 >& $TEST_log || fail "Failed to build"
130130
expect_not_log "Tra-la!" # No invalidation
131-
cat bazel-genfiles/zoo/ball-pit2.txt >$TEST_log
131+
cat bazel-bin/zoo/ball-pit2.txt >$TEST_log
132132
expect_log "Tra-la!"
133133

134134
# Test invalidation of the WORKSPACE file
@@ -154,15 +154,15 @@ EOF
154154
bazel build //zoo:ball-pit1 >& $TEST_log || fail "Failed to build"
155155
expect_log "blah"
156156
expect_log "Tra-la-la!" # Invalidation
157-
cat bazel-genfiles/zoo/ball-pit1.txt >$TEST_log
157+
cat bazel-bin/zoo/ball-pit1.txt >$TEST_log
158158
expect_log "Tra-la-la!"
159159

160160
bazel build //zoo:ball-pit1 >& $TEST_log || fail "Failed to build"
161161
expect_not_log "Tra-la-la!" # No invalidation
162162

163163
bazel build //zoo:ball-pit2 >& $TEST_log || fail "Failed to build"
164164
expect_not_log "Tra-la-la!" # No invalidation
165-
cat bazel-genfiles/zoo/ball-pit2.txt >$TEST_log
165+
cat bazel-bin/zoo/ball-pit2.txt >$TEST_log
166166
expect_log "Tra-la-la!"
167167
}
168168

@@ -355,7 +355,7 @@ EOF
355355
bazel build @foo//:bar >& $TEST_log || fail "Failed to build"
356356
expect_log "foo"
357357
expect_not_log "Workspace name in .*/WORKSPACE (.*) does not match the name given in the repository's definition (@foo)"
358-
cat bazel-genfiles/external/foo/bar.txt >$TEST_log
358+
cat bazel-bin/external/foo/bar.txt >$TEST_log
359359
expect_log "foo"
360360
}
361361

@@ -892,8 +892,8 @@ build:bar --repo_env=FOO=bar
892892
EOF
893893

894894
bazel build --config=foo //:repoenv //:unrelated
895-
cp `bazel info bazel-genfiles 2>/dev/null`/repoenv.txt repoenv1.txt
896-
cp `bazel info bazel-genfiles 2> /dev/null`/unrelated.txt unrelated1.txt
895+
cp `bazel info bazel-bin 2>/dev/null`/repoenv.txt repoenv1.txt
896+
cp `bazel info bazel-bin 2> /dev/null`/unrelated.txt unrelated1.txt
897897
echo; cat repoenv1.txt; echo; cat unrelated1.txt; echo
898898

899899
grep -q 'FOO=foo' repoenv1.txt \
@@ -904,8 +904,8 @@ EOF
904904
FOO=CHANGED bazel build --config=foo //:repoenv //:unrelated
905905
# nothing should change, as actions don't see FOO and for repositories
906906
# the value is fixed by --repo_env
907-
cp `bazel info bazel-genfiles 2>/dev/null`/repoenv.txt repoenv2.txt
908-
cp `bazel info bazel-genfiles 2> /dev/null`/unrelated.txt unrelated2.txt
907+
cp `bazel info bazel-bin 2>/dev/null`/repoenv.txt repoenv2.txt
908+
cp `bazel info bazel-bin 2> /dev/null`/unrelated.txt unrelated2.txt
909909
echo; cat repoenv2.txt; echo; cat unrelated2.txt; echo
910910

911911
diff repoenv1.txt repoenv2.txt \
@@ -916,8 +916,8 @@ EOF
916916
bazel build --config=bar //:repoenv //:unrelated
917917
# The new config should be picked up, but the unrelated target should
918918
# not be rerun
919-
cp `bazel info bazel-genfiles 3>/dev/null`/repoenv.txt repoenv3.txt
920-
cp `bazel info bazel-genfiles 3> /dev/null`/unrelated.txt unrelated3.txt
919+
cp `bazel info bazel-bin 3>/dev/null`/repoenv.txt repoenv3.txt
920+
cp `bazel info bazel-bin 3> /dev/null`/unrelated.txt unrelated3.txt
921921
echo; cat repoenv3.txt; echo; cat unrelated3.txt; echo
922922

923923
grep -q 'FOO=bar' repoenv3.txt \
@@ -926,6 +926,49 @@ EOF
926926
|| fail "Expected unrelated action to not be rerun"
927927
}
928928

929+
function test_repo_env_inverse() {
930+
# This test makes sure that a repository rule that has no dependencies on
931+
# environment variables does _not_ get refetched when --repo_env changes.
932+
setup_starlark_repository
933+
934+
cat > test.bzl <<'EOF'
935+
def _impl(ctx):
936+
# Record a time stamp to verify that the rule is not rerun.
937+
ctx.execute(["bash", "-c", "date +%s >> env.txt"])
938+
ctx.file("BUILD", 'exports_files(["env.txt"])')
939+
940+
repo = repository_rule(
941+
implementation = _impl,
942+
)
943+
EOF
944+
cat > BUILD <<'EOF'
945+
genrule(
946+
name = "repoenv",
947+
outs = ["repoenv.txt"],
948+
srcs = ["@foo//:env.txt"],
949+
cmd = "cp $< $@",
950+
)
951+
EOF
952+
cat > .bazelrc <<EOF
953+
build:foo --repo_env=FOO=foo
954+
build:bar --repo_env=FOO=bar
955+
EOF
956+
957+
bazel build --config=foo //:repoenv
958+
cp `bazel info bazel-bin 2>/dev/null`/repoenv.txt repoenv1.txt
959+
echo; cat repoenv1.txt; echo;
960+
961+
sleep 2 # ensure any rerun will have a different time stamp
962+
963+
bazel build --config=bar //:repoenv
964+
# The new config should not trigger a rerun of repoenv.
965+
cp `bazel info bazel-bin 2>/dev/null`/repoenv.txt repoenv2.txt
966+
echo; cat repoenv2.txt; echo;
967+
968+
diff repoenv1.txt repoenv2.txt \
969+
|| fail "Expected repository to not change"
970+
}
971+
929972
function test_repo_env_invalidation() {
930973
# regression test for https://github.com/bazelbuild/bazel/issues/8869
931974
WRKDIR=$(mktemp -d "${TEST_TMPDIR}/testXXXXXX")
@@ -962,18 +1005,18 @@ genrule(
9621005
EOF
9631006

9641007
bazel build //:repotime
965-
cp `bazel info bazel-genfiles 2>/dev/null`/repotime.txt time1.txt
1008+
cp `bazel info bazel-bin 2>/dev/null`/repotime.txt time1.txt
9661009

9671010
sleep 2;
9681011
bazel build --repo_env=foo=bar //:repotime
969-
cp `bazel info bazel-genfiles 2>/dev/null`/repotime.txt time2.txt
1012+
cp `bazel info bazel-bin 2>/dev/null`/repotime.txt time2.txt
9701013
diff time1.txt time2.txt && fail "Expected repo to be refetched" || :
9711014

9721015
bazel shutdown
9731016
sleep 2;
9741017

9751018
bazel build --repo_env=foo=bar //:repotime
976-
cp `bazel info bazel-genfiles 2>/dev/null`/repotime.txt time3.txt
1019+
cp `bazel info bazel-bin 2>/dev/null`/repotime.txt time3.txt
9771020
diff time2.txt time3.txt || fail "Expected repo to not be refetched"
9781021
}
9791022

@@ -1387,9 +1430,9 @@ EOF
13871430
echo "initial" > reference.txt.shadow
13881431

13891432
bazel build //:source //:configure
1390-
grep 'initial' `bazel info bazel-genfiles`/source.txt \
1433+
grep 'initial' `bazel info bazel-bin`/source.txt \
13911434
|| fail '//:source not generated properly'
1392-
grep 'initial' `bazel info bazel-genfiles`/configure.txt \
1435+
grep 'initial' `bazel info bazel-bin`/configure.txt \
13931436
|| fail '//:configure not generated properly'
13941437

13951438
echo "new value" > reference.txt.shadow
@@ -1401,9 +1444,9 @@ EOF
14011444
&& fail "Expected 'source' not to be synced" || :
14021445

14031446
bazel build //:source //:configure
1404-
grep -q 'initial' `bazel info bazel-genfiles`/source.txt \
1447+
grep -q 'initial' `bazel info bazel-bin`/source.txt \
14051448
|| fail '//:source did not keep its old value'
1406-
grep -q 'new value' `bazel info bazel-genfiles`/configure.txt \
1449+
grep -q 'new value' `bazel info bazel-bin`/configure.txt \
14071450
|| fail '//:configure not synced properly'
14081451
}
14091452

@@ -1752,9 +1795,9 @@ load("@netrc//:data.bzl", "netrc")
17521795
) for name in ["login", "password"]]
17531796
EOF
17541797
bazel build //:login //:password
1755-
grep 'myusername' `bazel info bazel-genfiles`/login.txt \
1798+
grep 'myusername' `bazel info bazel-bin`/login.txt \
17561799
|| fail "Username not parsed correctly"
1757-
grep 'mysecret' `bazel info bazel-genfiles`/password.txt \
1800+
grep 'mysecret' `bazel info bazel-bin`/password.txt \
17581801
|| fail "Password not parsed correctly"
17591802

17601803
# Also check the precise value of parsed file
@@ -1789,7 +1832,7 @@ genrule(
17891832
)
17901833
EOF
17911834
bazel build //:check_expected
1792-
grep 'OK' `bazel info bazel-genfiles`/check_expected.txt \
1835+
grep 'OK' `bazel info bazel-bin`/check_expected.txt \
17931836
|| fail "Parsed dict not equal to expected value"
17941837
}
17951838

@@ -1895,7 +1938,7 @@ genrule(
18951938
)
18961939
EOF
18971940
bazel build //:check_expected
1898-
grep 'OK' `bazel info bazel-genfiles`/check_expected.txt \
1941+
grep 'OK' `bazel info bazel-bin`/check_expected.txt \
18991942
|| fail "Authentication merged incorrectly"
19001943
}
19011944

0 commit comments

Comments
 (0)