diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleInspectorFunction.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleInspectorFunction.java index 47a74b3a27f4e9..26d06ea70eed1c 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleInspectorFunction.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleInspectorFunction.java @@ -106,7 +106,11 @@ public static ImmutableMap 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); @@ -122,6 +126,7 @@ public static ImmutableMap computeAugmentedGraph( originalChildBuilder .setName(originalModule.getName()) .setVersion(originalModule.getVersion()) + .setRepoName(originalModule.getRepoName()) .setLoaded(true); } @@ -132,6 +137,7 @@ public static ImmutableMap computeAugmentedGraph( AugmentedModule.builder(k) .setName(module.getName()) .setVersion(module.getVersion()) + .setRepoName(module.getRepoName()) .setLoaded(true)); // originalDependants and dependants can differ because diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleInspectorValue.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleInspectorValue.java index 6b39a79d40d255..6df76aa8913ce1 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleInspectorValue.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/BazelModuleInspectorValue.java @@ -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 * after the module resolution. @@ -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); } @@ -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 originalDependantsBuilder(); diff --git a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/JsonOutputFormatter.java b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/JsonOutputFormatter.java index ab8e9da6391c18..0b102643d92e20 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/JsonOutputFormatter.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/modcommand/JsonOutputFormatter.java @@ -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 { @@ -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); diff --git a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BzlmodTestUtil.java b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BzlmodTestUtil.java index 9b10ca5ab3f4ae..5057a207a1460a 100644 --- a/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BzlmodTestUtil.java +++ b/src/test/java/com/google/devtools/build/lib/bazel/bzlmod/BzlmodTestUtil.java @@ -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; }