Skip to content

Commit 2c03231

Browse files
committed
Address review comment
1 parent d9aaf42 commit 2c03231

File tree

2 files changed

+50
-11
lines changed

2 files changed

+50
-11
lines changed

src/main/java/com/google/devtools/build/lib/skyframe/BzlLoadFunction.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import java.util.LinkedHashSet;
6666
import java.util.List;
6767
import java.util.Map;
68-
import java.util.Optional;
6968
import java.util.concurrent.atomic.AtomicBoolean;
7069
import java.util.function.Consumer;
7170
import javax.annotation.Nullable;
@@ -763,7 +762,7 @@ private BzlLoadValue computeInternalWithCompiledBzl(
763762
if (repoMapping == null) {
764763
return null;
765764
}
766-
Optional<RepositoryMapping> mainRepoMapping =
765+
RepositoryMapping mainRepoMapping =
767766
getMainRepositoryMapping(key, builtins.starlarkSemantics, env);
768767
if (mainRepoMapping == null) {
769768
return null;
@@ -846,7 +845,7 @@ private BzlLoadValue computeInternalWithCompiledBzl(
846845
BazelModuleContext.create(
847846
label,
848847
repoMapping,
849-
mainRepoMapping.orElse(null),
848+
mainRepoMapping,
850849
prog.getFilename(),
851850
ImmutableList.copyOf(loadMap.values()),
852851
transitiveDigest);
@@ -980,30 +979,29 @@ private static RepositoryMapping getRepositoryMapping(
980979
}
981980

982981
@Nullable
983-
private static Optional<RepositoryMapping> getMainRepositoryMapping(
982+
private static RepositoryMapping getMainRepositoryMapping(
984983
BzlLoadValue.Key key, StarlarkSemantics starlarkSemantics, Environment env)
985984
throws InterruptedException {
986-
if (!starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD)) {
987-
return Optional.empty();
988-
}
985+
boolean bzlmod = starlarkSemantics.getBool(BuildLanguageOptions.ENABLE_BZLMOD);
989986
RepositoryMappingValue.Key repoMappingKey;
990987
if (key instanceof BzlLoadValue.KeyForBuild) {
991988
repoMappingKey = RepositoryMappingValue.key(RepositoryName.MAIN);
992989
} else if ((key instanceof BzlLoadValue.KeyForBzlmod
993990
&& !(key instanceof BzlLoadValue.KeyForBzlmodBootstrap))
994-
|| key instanceof BzlLoadValue.KeyForWorkspace) {
991+
|| (bzlmod && key instanceof BzlLoadValue.KeyForWorkspace)) {
995992
// Since the main repo mapping requires evaluating WORKSPACE, but WORKSPACE can load from
996993
// extension repos, requesting the full main repo mapping would cause a cycle.
997994
repoMappingKey = RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS;
998995
} else {
999-
// Builtins and Bzlmod bootstrap repo rules don't need the main repo mapping.
1000-
return Optional.empty();
996+
// For builtins, @bazel_tools, and legacy WORKSPACE, the key's local repo mapping can be used
997+
// as the main repo mapping.
998+
return getRepositoryMapping(key, starlarkSemantics, env);
1001999
}
10021000
var mainRepositoryMappingValue = (RepositoryMappingValue) env.getValue(repoMappingKey);
10031001
if (mainRepositoryMappingValue == null) {
10041002
return null;
10051003
}
1006-
return Optional.of(mainRepositoryMappingValue.getRepositoryMapping());
1004+
return mainRepositoryMappingValue.getRepositoryMapping();
10071005
}
10081006

10091007
/**

src/test/shell/bazel/starlark_repository_test.sh

+41
Original file line numberDiff line numberDiff line change
@@ -3272,4 +3272,45 @@ EOF
32723272
true
32733273
}
32743274

3275+
function test_legacy_label_print() {
3276+
WRKDIR=$(mktemp -d "${TEST_TMPDIR}/testXXXXXX")
3277+
cd "${WRKDIR}"
3278+
cat > WORKSPACE <<'EOF'
3279+
load("//:my_repository_rule.bzl", "my_repository_rule")
3280+
3281+
my_repository_rule(
3282+
name = "my_first_repo",
3283+
)
3284+
3285+
my_repository_rule(
3286+
name = "my_second_repo",
3287+
)
3288+
EOF
3289+
cat > my_repository_rule.bzl <<'EOF'
3290+
def _my_repository_rule_impl(rctx):
3291+
print("main repo:", Label("@@//:foo"), str(Label("@@//:foo")))
3292+
print("my_first_repo:", Label("@my_first_repo//:foo"), str(Label("@my_first_repo//:foo")))
3293+
print("my_second_repo:", Label("@my_first_repo//:foo"), str(Label("@my_first_repo//:foo")))
3294+
rctx.file("WORKSPACE")
3295+
rctx.file("BUILD", "filegroup(name='foo',visibility=['//visibility:public'])")
3296+
3297+
my_repository_rule = repository_rule(
3298+
implementation = _my_repository_rule_impl,
3299+
)
3300+
EOF
3301+
cat > BUILD <<'EOF'
3302+
filegroup(
3303+
name = "foo",
3304+
srcs = [
3305+
"@my_first_repo//:foo",
3306+
"@my_second_repo//:foo",
3307+
],
3308+
)
3309+
EOF
3310+
bazel build --noenable_bzlmod //:foo >& $TEST_log || fail "expected bazel to succeed"
3311+
expect_log "main repo: //:foo @//:foo"
3312+
expect_log "my_first_repo: @my_first_repo//:foo @my_first_repo//:foo"
3313+
expect_log "my_second_repo: @my_first_repo//:foo @my_first_repo//:foo"
3314+
}
3315+
32753316
run_suite "local repository tests"

0 commit comments

Comments
 (0)