Skip to content

Commit

Permalink
ModqueryExecutor result graph logic
Browse files Browse the repository at this point in the history
- `tree`, `all_paths` core functions implementations (using the same core graph traversal)

- Unit testing for `ModqueryCommand` result graph computation logic

bazelbuild#15365

PiperOrigin-RevId: 473344453
Change-Id: I626cf8a83ca42445a2ac81d4bd9c9648e94c2eea
  • Loading branch information
Googler authored and aiuto committed Oct 12, 2022
1 parent a74bfd5 commit 48f51a7
Show file tree
Hide file tree
Showing 22 changed files with 1,222 additions and 355 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/google/devtools/build/lib/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/analysis:config/build_configuration",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:common",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:inspection",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:inspection_impl",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:registry",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution",
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:resolution_impl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue;
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleInspectorFunction;
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleInspectorValue.AugmentedModule.ResolutionReason;
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleResolutionFunction;
import com.google.devtools.build.lib.bazel.bzlmod.LocalPathOverride;
import com.google.devtools.build.lib.bazel.bzlmod.ModuleFileFunction;
Expand Down Expand Up @@ -222,11 +223,23 @@ public void workspaceInit(
// - The canonical name "local_config_platform" is hardcoded in Bazel code.
// See {@link PlatformOptions}
"local_config_platform",
(RepositoryName repoName) ->
RepoSpec.builder()
new NonRegistryOverride() {
@Override
public RepoSpec getRepoSpec(RepositoryName repoName) {
return RepoSpec.builder()
.setRuleClassName("local_config_platform")
.setAttributes(ImmutableMap.of("name", repoName.getName()))
.build());
.build();
}

@Override
public ResolutionReason getResolutionReason() {
// NOTE: It is not exactly a LOCAL_PATH_OVERRIDE, but there is no inspection
// ResolutionReason for builtin modules
return ResolutionReason.LOCAL_PATH_OVERRIDE;
}
});

builder
.addSkyFunction(SkyFunctions.REPOSITORY_DIRECTORY, repositoryDelegatorFunction)
.addSkyFunction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleInspectorValue.AugmentedModule.ResolutionReason;
import com.google.devtools.build.lib.cmdline.RepositoryName;

/** Specifies that a module should be retrieved from an archive. */
Expand Down Expand Up @@ -59,4 +60,9 @@ public RepoSpec getRepoSpec(RepositoryName repoName) {
.setPatchStrip(getPatchStrip())
.build();
}

@Override
public ResolutionReason getResolutionReason() {
return ResolutionReason.ARCHIVE_OVERRIDE;
}
}
21 changes: 16 additions & 5 deletions src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ java_library(
],
deps = [
":common",
":inspection",
":module_extension",
":registry",
":repo_rule_creator",
Expand Down Expand Up @@ -220,20 +221,30 @@ java_library(
java_library(
name = "inspection",
srcs = [
"BazelModuleInspectorFunction.java",
"BazelModuleInspectorValue.java",
"ModqueryExecutor.java",
],
deps = [
":common",
":resolution",
"//src/main/java/com/google/devtools/build/lib/skyframe:sky_functions",
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/autocodec:serialization-constant",
"//src/main/java/com/google/devtools/build/lib/util/io",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//third_party:auto_value",
"//third_party:guava",
],
)

java_library(
name = "inspection_impl",
srcs = [
"BazelModuleInspectorFunction.java",
],
deps = [
":common",
":inspection",
":resolution",
"//src/main/java/com/google/devtools/build/skyframe",
"//src/main/java/com/google/devtools/build/skyframe:skyframe-objects",
"//third_party:guava",
"//third_party:jsr305",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,18 @@ public static ImmutableMap<ModuleKey, AugmentedModule> computeAugmentedGraph(
} else {
// There is no other possible override
Preconditions.checkArgument(override instanceof NonRegistryOverride);
reason = ResolutionReason.NON_REGISTRY_OVERRIDE;
reason = ((NonRegistryOverride) override).getResolutionReason();
}
} else {
reason = ResolutionReason.MINIMAL_VERSION_SELECTION;
}
}

parentBuilder.addDep(key, reason);
if (!reason.equals(ResolutionReason.ORIGINAL)) {
parentBuilder.addUnusedDep(childDep, originalKey);
}
parentBuilder.addDep(childDep, key);
parentBuilder.addDepReason(childDep, reason);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
package com.google.devtools.build.lib.bazel.bzlmod;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.skyframe.SkyFunctions;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.SerializationConstant;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.util.HashMap;
import java.util.Map;

/**
* The result of running Bazel module inspection pre-processing, containing the un-pruned and
Expand Down Expand Up @@ -60,44 +63,70 @@ public static BazelModuleInspectorValue create(
* not used in the final dep graph).
*/
@AutoValue
abstract static class AugmentedModule {
public abstract static class AugmentedModule {
/** Name of the module. Same as in {@link Module}. */
abstract String getName();
public abstract String getName();

/** Version of the module. Same as in {@link Module}. */
abstract Version getVersion();
public abstract Version getVersion();

/** {@link ModuleKey} of this module. Same as in {@link Module} */
abstract ModuleKey getKey();
public abstract ModuleKey getKey();

/**
* The set of modules in the resolved dep graph that depend on this module
* <strong>after</strong> the module resolution.
*/
abstract ImmutableSet<ModuleKey> getDependants();
public abstract ImmutableSet<ModuleKey> getDependants();

/**
* The set of modules in the complete dep graph that originally depended on this module *before*
* the module resolution (can contain unused nodes).
*/
abstract ImmutableSet<ModuleKey> getOriginalDependants();
public abstract ImmutableSet<ModuleKey> getOriginalDependants();

/**
* A map from the resolved dependencies of this module to the rules that were used for their
* resolution (can be either the original dependency, changed by the Minimal-Version Selection
* algorithm or by an override rule
* A bi-map from the repo names of the resolved dependencies of this module to their actual
* module keys. The inverse lookup is used to check how and why the dependency changed using the
* other maps.
*/
abstract ImmutableMap<ModuleKey, ResolutionReason> getDeps();
public abstract ImmutableBiMap<String, ModuleKey> getDeps();

/**
* A bi-map from the repo names of the unused dependencies of this module to their actual module
* keys. The inverse lookup is used to check how and why the dependency changed using the other
* maps.
*/
public abstract ImmutableBiMap<String, ModuleKey> getUnusedDeps();

/**
* A map from the repo name of the dependencies of this module to the rules that were used for
* their resolution (can be either the original dependency, changed by the Minimal-Version
* Selection algorithm or by an override rule.
*/
public abstract ImmutableMap<String, ResolutionReason> getDepReasons();

/** Shortcut for retrieving the union of both used and unused deps based on the unused flag. */
public ImmutableMap<ModuleKey, String> getAllDeps(boolean unused) {
if (!unused) {
return getDeps().inverse();
} else {
Map<ModuleKey, String> map = new HashMap<>();
map.putAll(getDeps().inverse());
map.putAll(getUnusedDeps().inverse());
return ImmutableMap.copyOf(map);
}
}

/**
* Flag that tell whether the module was loaded and added to the dependency graph. Modules
* overridden by {@code single_version_override} and {@link NonRegistryOverride} are not loaded
* so their {@code originalDeps} are (yet) unknown.
*/
abstract boolean isLoaded();
public abstract boolean isLoaded();

/** Flag for checking whether the module is present in the resolved dep graph. */
boolean isUsed() {
public boolean isUsed() {
return !getDependants().isEmpty();
}

Expand All @@ -121,13 +150,6 @@ public abstract static class Builder {

public abstract AugmentedModule.Builder setLoaded(boolean value);

public abstract AugmentedModule.Builder setOriginalDependants(ImmutableSet<ModuleKey> value);

public abstract AugmentedModule.Builder setDependants(ImmutableSet<ModuleKey> value);

public abstract AugmentedModule.Builder setDeps(
ImmutableMap<ModuleKey, ResolutionReason> value);

abstract ImmutableSet.Builder<ModuleKey> originalDependantsBuilder();

@CanIgnoreReturnValue
Expand All @@ -144,19 +166,35 @@ public AugmentedModule.Builder addDependant(ModuleKey depKey) {
return this;
}

abstract ImmutableMap.Builder<ModuleKey, ResolutionReason> depsBuilder();
abstract ImmutableBiMap.Builder<String, ModuleKey> depsBuilder();

@CanIgnoreReturnValue
public AugmentedModule.Builder addDep(String repoName, ModuleKey key) {
depsBuilder().put(repoName, key);
return this;
}

abstract ImmutableBiMap.Builder<String, ModuleKey> unusedDepsBuilder();

@CanIgnoreReturnValue
public AugmentedModule.Builder addUnusedDep(String repoName, ModuleKey key) {
unusedDepsBuilder().put(repoName, key);
return this;
}

abstract ImmutableMap.Builder<String, ResolutionReason> depReasonsBuilder();

@CanIgnoreReturnValue
public AugmentedModule.Builder addDep(ModuleKey depKey, ResolutionReason reason) {
depsBuilder().put(depKey, reason);
public AugmentedModule.Builder addDepReason(String repoName, ResolutionReason reason) {
depReasonsBuilder().put(repoName, reason);
return this;
}

abstract AugmentedModule build();
public abstract AugmentedModule build();
}

/** The reason why a final dependency of a module was resolved the way it was. */
enum ResolutionReason {
public enum ResolutionReason {
/** The dependency is the original dependency defined in the MODULE.bazel file. */
ORIGINAL,
/** The dependency was replaced by the Minimal-Version Selection algorithm. */
Expand All @@ -165,8 +203,10 @@ enum ResolutionReason {
SINGLE_VERSION_OVERRIDE,
/** The dependency was replaced by a {@code multiple_version_override} rule. */
MULTIPLE_VERSION_OVERRIDE,
/** The dependency was replaced by a {@link NonRegistryOverride} rule. */
NON_REGISTRY_OVERRIDE
/** The dependency was replaced by one of the {@link NonRegistryOverride} rules. */
ARCHIVE_OVERRIDE,
GIT_OVERRIDE,
LOCAL_PATH_OVERRIDE,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleInspectorValue.AugmentedModule.ResolutionReason;
import com.google.devtools.build.lib.cmdline.RepositoryName;

/** Specifies that a module should be retrieved from a Git repository. */
Expand Down Expand Up @@ -56,4 +57,9 @@ public RepoSpec getRepoSpec(RepositoryName repoName) {
.setAttributes(attrBuilder.buildOrThrow())
.build();
}

@Override
public ResolutionReason getResolutionReason() {
return ResolutionReason.GIT_OVERRIDE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.bazel.bzlmod.BazelModuleInspectorValue.AugmentedModule.ResolutionReason;
import com.google.devtools.build.lib.cmdline.RepositoryName;

/** Specifies that a module should be retrieved from a local directory. */
Expand All @@ -37,4 +38,9 @@ public RepoSpec getRepoSpec(RepositoryName repoName) {
.setAttributes(ImmutableMap.of("name", repoName.getName(), "path", getPath()))
.build();
}

@Override
public ResolutionReason getResolutionReason() {
return ResolutionReason.LOCAL_PATH_OVERRIDE;
}
}
Loading

0 comments on commit 48f51a7

Please sign in to comment.