Skip to content

Commit

Permalink
[7.1.0] Also report cycles involving WORKSPACE from BzlmodRepoCycleRe…
Browse files Browse the repository at this point in the history
…porter (#21013)

Work towards #20942

On the referenced bug, this prints:
```
ERROR: Circular definition of repositories generated by module extensions and/or .bzl files:
.-> repository mapping of @@
|   WORKSPACE file
|   //python:pip.bzl
|   //python/pip_install:requirements.bzl
|   //python:defs.bzl
|   //python:py_import.bzl
|   //python:py_info.bzl
|   @@_main~internal_deps~rules_python_internal//:rules_python_config.bzl
|   @@_main~internal_deps~rules_python_internal
|   extension 'internal_deps' defined in //python/private/bzlmod:internal_deps.bzl
`-- repository mapping of @@
ERROR: Error computing the main repository mapping: cycles detected during computation of main repo mapping
```

Closes #20958.

Commit
fab6414

PiperOrigin-RevId: 601183514
Change-Id: If321005b1f6918117d0f5b5763ae2bda4d8e34f1

Co-authored-by: Fabian Meumertzheim <[email protected]>
  • Loading branch information
bazel-io and fmeum authored Jan 24, 2024
1 parent 9d12d79 commit fa0660a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/google/devtools/build/lib/skyframe/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2351,11 +2351,13 @@ java_library(
deps = [
":abstract_label_cycle_reporter",
":bzl_load_value",
":repository_mapping_value",
":sky_functions",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:module_extension",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:repo_rule_value",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/events",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/repository:request_repository_information_event",
"//src/main/java/com/google/devtools/build/lib/rules:repository/repository_directory_value",
"//src/main/java/com/google/devtools/build/skyframe",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.packages.WorkspaceFileValue;
import com.google.devtools.build.lib.repository.RequestRepositoryInformationEvent;
import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue;
import com.google.devtools.build.skyframe.CycleInfo;
Expand Down Expand Up @@ -53,6 +54,21 @@ public class BzlmodRepoCycleReporter implements CyclesReporter.SingleCycleReport
private static final Predicate<SkyKey> IS_EXTENSION_IMPL =
SkyFunctions.isSkyFunction(SkyFunctions.SINGLE_EXTENSION_EVAL);

private static final Predicate<SkyKey> IS_REPO_MAPPING =
SkyFunctions.isSkyFunction(SkyFunctions.REPOSITORY_MAPPING);

private static final Predicate<SkyKey> IS_MODULE_EXTENSION_REPO_MAPPING_ENTRIES =
SkyFunctions.isSkyFunction(SkyFunctions.MODULE_EXTENSION_REPO_MAPPING_ENTRIES);

private static final Predicate<SkyKey> IS_PACKAGE =
SkyFunctions.isSkyFunction(SkyFunctions.PACKAGE);

private static final Predicate<SkyKey> IS_EXTERNAL_PACKAGE =
SkyFunctions.isSkyFunction(SkyFunctions.EXTERNAL_PACKAGE);

private static final Predicate<SkyKey> IS_WORKSPACE_FILE =
SkyFunctions.isSkyFunction(WorkspaceFileValue.WORKSPACE_FILE);

private static void requestRepoDefinitions(
ExtendedEventHandler eventHandler, Iterable<SkyKey> repos) {
for (SkyKey repo : repos) {
Expand Down Expand Up @@ -85,21 +101,33 @@ public boolean maybeReportCycle(
// |___________________|___|
// TODO(andreisolo): Figure out how to detect and print this kind of cycles more specifically.
if (Iterables.all(
cycle,
Predicates.or(
IS_REPOSITORY_DIRECTORY,
IS_PACKAGE_LOOKUP,
IS_REPO_RULE,
IS_EXTENSION_IMPL,
IS_BZL_LOAD,
IS_CONTAINING_PACKAGE))) {
cycle,
Predicates.or(
IS_REPOSITORY_DIRECTORY,
IS_PACKAGE_LOOKUP,
IS_REPO_RULE,
IS_EXTENSION_IMPL,
IS_BZL_LOAD,
IS_CONTAINING_PACKAGE,
IS_REPO_MAPPING,
IS_MODULE_EXTENSION_REPO_MAPPING_ENTRIES,
IS_PACKAGE,
IS_EXTERNAL_PACKAGE,
IS_WORKSPACE_FILE))
&& Iterables.any(cycle, Predicates.or(IS_REPO_RULE, IS_EXTENSION_IMPL))) {
StringBuilder cycleMessage =
new StringBuilder(
"Circular definition of repositories generated by module extensions and/or .bzl"
+ " files:");
Iterable<SkyKey> repos =
Iterables.filter(
cycle, Predicates.or(IS_REPOSITORY_DIRECTORY, IS_EXTENSION_IMPL, IS_BZL_LOAD));
cycle,
Predicates.or(
IS_REPOSITORY_DIRECTORY,
IS_EXTENSION_IMPL,
IS_BZL_LOAD,
IS_REPO_MAPPING,
IS_WORKSPACE_FILE));
Function<Object, String> printer =
rawInput -> {
SkyKey input = (SkyKey) rawInput;
Expand All @@ -110,6 +138,14 @@ public boolean maybeReportCycle(
return String.format(
"extension '%s' defined in %s",
id.getExtensionName(), id.getBzlFileLabel().getCanonicalForm());
} else if (input.argument() instanceof RepositoryMappingValue.Key) {
var key = (RepositoryMappingValue.Key) input.argument();
if (key == RepositoryMappingValue.KEY_FOR_ROOT_MODULE_WITHOUT_WORKSPACE_REPOS) {
return "repository mapping of @@ without WORKSPACE repos";
}
return String.format("repository mapping of %s", key.repoName());
} else if (input.argument() instanceof WorkspaceFileValue.WorkspaceFileKey) {
return "WORKSPACE file";
} else {
Preconditions.checkArgument(input.argument() instanceof BzlLoadValue.Key);
return ((BzlLoadValue.Key) input.argument()).getLabel().toString();
Expand Down

0 comments on commit fa0660a

Please sign in to comment.