Skip to content

Commit

Permalink
Autoload only when version has rules
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 682998564
Change-Id: I20316c27050c97a0040953b0fd5b163adf30851f
  • Loading branch information
comius committed Oct 9, 2024
1 parent 054f583 commit da1c25c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.bazel.bzlmod.BazelDepGraphValue;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleKey;
import com.google.devtools.build.lib.bazel.bzlmod.Version;
import com.google.devtools.build.lib.bazel.bzlmod.Version.ParseException;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.Label.RepoContext;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
Expand Down Expand Up @@ -389,14 +391,15 @@ public ImmutableMap<String, BzlLoadValue.Key> getLoadKeys(SkyFunction.Environmen
throws InterruptedException {

final RepoContext repoContext;
ImmutableMap<String, ModuleKey> highestVersions = ImmutableMap.of();
if (bzlmodEnabled) {
BazelDepGraphValue bazelDepGraphValue =
(BazelDepGraphValue) env.getValue(BazelDepGraphValue.KEY);
if (bazelDepGraphValue == null) {
return null;
}

ImmutableMap<String, ModuleKey> highestVersions =
highestVersions =
bazelDepGraphValue.getCanonicalRepoNameLookup().values().stream()
.collect(
toImmutableMap(
Expand Down Expand Up @@ -440,6 +443,20 @@ public ImmutableMap<String, BzlLoadValue.Key> getLoadKeys(SkyFunction.Environmen
continue;
}

String requiredModule = AUTOLOAD_CONFIG.get(symbol).getModuleName();
// Skip if version doesn't have the rules
if (highestVersions.containsKey(requiredModule)
&& requiredVersions.containsKey(requiredModule)) {
if (highestVersions
.get(requiredModule)
.getVersion()
.compareTo(requiredVersions.get(requiredModule))
<= 0) {
missingRepositories.add(requiredModule);
continue;
}
}

Label label = AUTOLOAD_CONFIG.get(symbol).getLabel(repoContext);
// Only load if the dependency is present
if (label.getRepository().isVisible()) {
Expand Down Expand Up @@ -542,6 +559,10 @@ public abstract static class SymbolRedirect {

public abstract ImmutableSet<String> getRdeps();

String getModuleName() throws InterruptedException {
return Label.parseCanonicalUnchecked(getLoadLabel()).getRepository().getName();
}

Label getLabel(RepoContext repoContext) throws InterruptedException {
try {
return Label.parseWithRepoContext(getLoadLabel(), repoContext);
Expand Down Expand Up @@ -590,6 +611,16 @@ private static SymbolRedirect renamedSymbolRedirect(
"bazel_tools",
"bazel_features");

private static final ImmutableMap<String, Version> requiredVersions;

static {
try {
requiredVersions = ImmutableMap.of("protobuf", Version.parse("29.0-rc1"));
} catch (ParseException e) {
throw new IllegalStateException(e);
}
}

private static final ImmutableMap<String, SymbolRedirect> AUTOLOAD_CONFIG =
ImmutableMap.<String, SymbolRedirect>builder()
.put(
Expand Down
11 changes: 5 additions & 6 deletions src/test/shell/integration/load_removed_symbols_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,14 @@ EOF
function test_missing_necessary_bzlmod_dep() {
# Intentionally not adding rules_android to MODULE.bazel
cat > BUILD << EOF
aar_import(
sh_library(
name = 'aar',
aar = 'aar.file',
deps = [],
)
EOF
bazel build --incompatible_autoload_externally=aar_import :aar >&$TEST_log 2>&1 && fail "build unexpectedly succeeded"
expect_log "name 'aar_import' is not defined"
expect_log "WARNING: Couldn't auto load rules or symbols, because no dependency on module/repository 'rules_android' found. This will result in a failure if there's a reference to those rules or symbols."
bazel build --incompatible_autoload_externally=sh_library :aar >&$TEST_log 2>&1 && fail "build unexpectedly succeeded"
expect_log "WARNING: Couldn't auto load rules or symbols, because no dependency on module/repository 'rules_sh' found. This will result in a failure if there's a reference to those rules or symbols."
}

function test_missing_unnecessary_bzmod_dep() {
Expand All @@ -109,8 +108,8 @@ filegroup(
srcs = [],
)
EOF
bazel build --incompatible_autoload_externally=aar_import :filegroup >&$TEST_log 2>&1 || fail "build failed"
expect_log "WARNING: Couldn't auto load rules or symbols, because no dependency on module/repository 'rules_android' found. This will result in a failure if there's a reference to those rules or symbols."
bazel build --incompatible_autoload_externally=sh_library :filegroup >&$TEST_log 2>&1 || fail "build failed"
expect_log "WARNING: Couldn't auto load rules or symbols, because no dependency on module/repository 'rules_sh' found. This will result in a failure if there's a reference to those rules or symbols."
}

function test_removed_rule_loaded() {
Expand Down

0 comments on commit da1c25c

Please sign in to comment.