Skip to content

Commit

Permalink
Include apparent names of deps in bazel mod JSON output
Browse files Browse the repository at this point in the history
This allows IDEs to query for the direct dependencies of the root module as well as how they can refer to them from the point of view of the root module.

Also always emit `name` and `version` so that consumers don't have to know how to parse module keys.

Work towards bazelbuild#22691

Closes bazelbuild#23787.

PiperOrigin-RevId: 693453084
Change-Id: Ie3fd5e89301d8e83d0eaa686188634923853f01a
  • Loading branch information
fmeum authored and bazel-io committed Nov 6, 2024
1 parent 54e7b4d commit 0b8bc48
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ public static ImmutableMap<ModuleKey, AugmentedModule> computeAugmentedGraph(
AugmentedModule.Builder parentBuilder =
depGraphAugmentBuilder
.computeIfAbsent(
parentKey, k -> AugmentedModule.builder(k).setName(parentModule.getName()))
parentKey,
k ->
AugmentedModule.builder(k)
.setName(parentModule.getName())
.setRepoName(parentModule.getRepoName()))
.setVersion(parentModule.getVersion())
.setLoaded(true);

Expand All @@ -122,6 +126,7 @@ public static ImmutableMap<ModuleKey, AugmentedModule> computeAugmentedGraph(
originalChildBuilder
.setName(originalModule.getName())
.setVersion(originalModule.getVersion())
.setRepoName(originalModule.getRepoName())
.setLoaded(true);
}

Expand All @@ -132,6 +137,7 @@ public static ImmutableMap<ModuleKey, AugmentedModule> computeAugmentedGraph(
AugmentedModule.builder(k)
.setName(module.getName())
.setVersion(module.getVersion())
.setRepoName(module.getRepoName())
.setLoaded(true));

// originalDependants and dependants can differ because
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public abstract static class AugmentedModule {
/** {@link ModuleKey} of this module. Same as in {@link Module} */
public abstract ModuleKey getKey();

/** The apparent name used by the module to refer to its own repository. */
public abstract String getRepoName();

/**
* The set of modules in the resolved dep graph that depend on this module
* <strong>after</strong> the module resolution.
Expand Down Expand Up @@ -154,6 +157,7 @@ public static AugmentedModule.Builder builder(ModuleKey key) {
return new AutoValue_BazelModuleInspectorValue_AugmentedModule.Builder()
.setName(key.name())
.setVersion(key.version())
.setRepoName(key.name())
.setKey(key)
.setLoaded(false);
}
Expand All @@ -167,6 +171,8 @@ public abstract static class Builder {

public abstract AugmentedModule.Builder setKey(ModuleKey value);

public abstract AugmentedModule.Builder setRepoName(String value);

public abstract AugmentedModule.Builder setLoaded(boolean value);

abstract ImmutableSet.Builder<ModuleKey> originalDependantsBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Nullable;

/** Outputs graph-based results of {@link ModExecutor} in JSON format. */
public class JsonOutputFormatter extends OutputFormatter {
Expand Down Expand Up @@ -89,17 +90,22 @@ private JsonObject printExtension(

// Depth-first traversal to display modules (while explicitly detecting cycles)
JsonObject printModule(
ModuleKey key, ModuleKey parent, IsExpanded expanded, IsIndirect indirect) {
ModuleKey key, @Nullable ModuleKey parent, IsExpanded expanded, IsIndirect indirect) {
ResultNode node = result.get(key);
AugmentedModule module = depGraph.get(key);
JsonObject json = new JsonObject();
json.addProperty("key", printKey(key));
if (!key.name().equals(module.getName())) {
json.addProperty("name", module.getName());
}
if (!key.version().equals(module.getVersion())) {
json.addProperty("version", module.getVersion().toString());
json.addProperty("name", module.getName());
json.addProperty("version", module.getVersion().toString());
String apparentName;
if (parent != null) {
// The apparent repository name under which parent refers to key.
apparentName = depGraph.get(parent).getDeps().inverse().get(key);
} else {
// The apparent repository name under which key refers to itself.
apparentName = module.getRepoName();
}
json.addProperty("apparentName", apparentName);

if (indirect == IsIndirect.FALSE && options.verbose && parent != null) {
Explanation explanation = getExtraResolutionExplanation(key, parent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ public static AugmentedModuleBuilder buildAugmentedModule(
AugmentedModuleBuilder myBuilder = new AugmentedModuleBuilder();
myBuilder.key = key;
myBuilder.builder =
AugmentedModule.builder(key).setName(name).setVersion(version).setLoaded(loaded);
AugmentedModule.builder(key)
.setName(name)
.setVersion(version)
.setRepoName(name)
.setLoaded(loaded);
return myBuilder;
}

Expand Down

0 comments on commit 0b8bc48

Please sign in to comment.