Skip to content

Commit

Permalink
Implement shortcut semantics incompatible_load_externally
Browse files Browse the repository at this point in the history
Listing 30 different symbols on the command line is not really users friendly.
Once we finish a rules set, we can set it to the whole rules repository.

PiperOrigin-RevId: 666776777
Change-Id: I6ddd8aec4daec26a427f962f6f76e496163d1108
  • Loading branch information
comius authored and copybara-github committed Aug 23, 2024
1 parent f9ed0b0 commit cc3ba22
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import net.starlark.java.eval.Starlark;
import net.starlark.java.eval.StarlarkSemantics;
Expand Down Expand Up @@ -99,6 +100,25 @@ public AutoloadSymbols(RuleClassProvider ruleClassProvider, StarlarkSemantics se
return;
}

// Expand symbols given with @rules_foo
symbolConfiguration =
symbolConfiguration.stream()
.flatMap(
flag -> {
String prefix = "";
String flagWithoutPrefix = flag;
if (flag.startsWith("+") || flag.startsWith("-")) {
prefix = flag.substring(0, 1);
flagWithoutPrefix = flag.substring(1);
}
if (flagWithoutPrefix.startsWith("@")) {
return getAllSymbols(flagWithoutPrefix, prefix).stream();
} else {
return Stream.of(flag);
}
})
.collect(toImmutableList());

// Validates the inputs
Set<String> uniqueSymbols = new HashSet<>();
for (String symbol : symbolConfiguration) {
Expand Down Expand Up @@ -286,6 +306,13 @@ private static ImmutableList<String> filterSymbols(
.collect(toImmutableList());
}

private ImmutableList<String> getAllSymbols(String repository, String prefix) {
return AUTOLOAD_CONFIG.entrySet().stream()
.filter(entry -> entry.getValue().getLoadLabel().startsWith(repository + "//"))
.map(entry -> prefix + entry.getKey())
.collect(toImmutableList());
}

private static Map<String, Object> convertNativeStructToMap(StarlarkInfo struct) {
LinkedHashMap<String, Object> destr = new LinkedHashMap<>();
for (String field : struct.getFieldNames()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ public final class BuildLanguageOptions extends OptionsBase {
+ "If a symbol is not named in this flag then it continues to work as normal -- no"
+ " autoloading is done, nor is the Bazel-defined version suppressed. For"
+ " configuration see"
+ " https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/packages/AutoloadSymbols.java")
+ " https://github.com/bazelbuild/bazel/blob/master/src/main/java/com/google/devtools/build/lib/packages/AutoloadSymbols.java"
+ " As a shortcut also whole repository may be used, for example +@rules_python will"
+ " autoload all Python rules.")
public List<String> incompatibleAutoloadExternally;

@Option(
Expand Down
12 changes: 12 additions & 0 deletions src/test/shell/integration/load_removed_symbols_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -402,4 +402,16 @@ EOF
}


function test_whole_repo_flag() {
setup_module_dot_bazel

cat > BUILD << EOF
py_library(
name = 'py_library',
)
EOF
bazel query --incompatible_autoload_externally=+@rules_python ':py_library' --output=build >&$TEST_log 2>&1 || fail "build failed"
}


run_suite "load_removed_symbols"

0 comments on commit cc3ba22

Please sign in to comment.