Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bazel mod dump_repo_mapping #18990

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/bazel/bzlmod:inspection",
"//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/bazel/bzlmod:resolution",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/query2/query/output",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ public enum ModSubcommand {
PATH(true),
EXPLAIN(true),
SHOW_REPO(false),
SHOW_EXTENSION(false);
SHOW_EXTENSION(false),
DUMP_REPO_MAPPING(false);

/** Whether this subcommand produces a graph output. */
private final boolean isGraph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ java_library(
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/eval",
"//src/main/protobuf:failure_details_java_proto",
"//third_party:gson",
"//third_party:guava",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
import com.google.devtools.common.options.OptionsParser;
import com.google.devtools.common.options.OptionsParsingException;
import com.google.devtools.common.options.OptionsParsingResult;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
Expand Down Expand Up @@ -106,6 +109,28 @@ public void editOptions(OptionsParser optionsParser) {

@Override
public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult options) {
ModOptions modOptions = options.getOptions(ModOptions.class);
Preconditions.checkArgument(modOptions != null);

if (options.getResidue().isEmpty()) {
String errorMessage =
String.format(
"No subcommand specified, choose one of : %s.", ModSubcommand.printValues());
return reportAndCreateFailureResult(env, errorMessage, Code.MOD_COMMAND_UNKNOWN);
}

// The first element in the residue must be the subcommand, and then comes a list of arguments.
String subcommandStr = options.getResidue().get(0);
ModSubcommand subcommand;
try {
subcommand = new ModSubcommandConverter().convert(subcommandStr);
} catch (OptionsParsingException e) {
String errorMessage =
String.format("Invalid subcommand, choose one from : %s.", ModSubcommand.printValues());
return reportAndCreateFailureResult(env, errorMessage, Code.MOD_COMMAND_UNKNOWN);
}
List<String> args = options.getResidue().subList(1, options.getResidue().size());

BazelDepGraphValue depGraphValue;
BazelModuleInspectorValue moduleInspector;

Expand All @@ -121,10 +146,12 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
try {
env.syncPackageLoading(options);

var keys = ImmutableSet.<SkyKey>builder().add(BazelDepGraphValue.KEY);
if (!subcommand.equals(ModSubcommand.DUMP_REPO_MAPPING)) {
keys.add(BazelModuleInspectorValue.KEY);
}
EvaluationResult<SkyValue> evaluationResult =
skyframeExecutor.prepareAndGet(
ImmutableSet.of(BazelDepGraphValue.KEY, BazelModuleInspectorValue.KEY),
evaluationContext);
skyframeExecutor.prepareAndGet(keys.build(), evaluationContext);

if (evaluationResult.hasError()) {
Exception e = evaluationResult.getError().getException();
Expand All @@ -150,27 +177,14 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
return BlazeCommandResult.detailedExitCode(e.getDetailedExitCode());
}

ModOptions modOptions = options.getOptions(ModOptions.class);
Preconditions.checkArgument(modOptions != null);

if (options.getResidue().isEmpty()) {
String errorMessage =
String.format(
"No subcommand specified, choose one of : %s.", ModSubcommand.printValues());
return reportAndCreateFailureResult(env, errorMessage, Code.MOD_COMMAND_UNKNOWN);
}

// The first element in the residue must be the subcommand, and then comes a list of arguments.
String subcommandStr = options.getResidue().get(0);
ModSubcommand subcommand;
try {
subcommand = new ModSubcommandConverter().convert(subcommandStr);
} catch (OptionsParsingException e) {
String errorMessage =
String.format("Invalid subcommand, choose one from : %s.", ModSubcommand.printValues());
return reportAndCreateFailureResult(env, errorMessage, Code.MOD_COMMAND_UNKNOWN);
if (subcommand.equals(ModSubcommand.DUMP_REPO_MAPPING)) {
dumpRepoMapping(
depGraphValue,
new OutputStreamWriter(
env.getReporter().getOutErr().getOutputStream(),
modOptions.charset == UTF8 ? UTF_8 : US_ASCII));
return BlazeCommandResult.success();
}
List<String> args = options.getResidue().subList(1, options.getResidue().size());

// Extract and check the --base_module argument first to use it when parsing the other args.
// Can only be a TargetModule or a repoName relative to the ROOT.
Expand Down Expand Up @@ -434,6 +448,8 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
case SHOW_EXTENSION:
modExecutor.showExtension(argsAsExtensions, usageKeys);
break;
default:
throw new IllegalStateException("Unexpected subcommand: " + subcommand);
}

return BlazeCommandResult.success();
Expand Down Expand Up @@ -494,4 +510,38 @@ private static BlazeCommandResult createFailureResult(String message, Code detai
.setMessage(message)
.build()));
}

public static void dumpRepoMapping(BazelDepGraphValue depGraphValue, Writer writer) {
try (JsonWriter jsonWriter = new JsonWriter(writer)) {
jsonWriter.beginObject();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall looks good! just one minor thing -- IMO JsonWriter is a bit too low-level for this; using JsonObject, JsonArray etc would be much more readable (and I doubt memory usage of bazel mod dump_repo_mapping is of any concern). See JsonOutputFormatter for an example.


jsonWriter.name("module_repos");
jsonWriter.beginObject();
for (ModuleKey modules : depGraphValue.getDepGraph().keySet()) {
jsonWriter.name(modules.getCanonicalRepoName().getName()).beginObject();
for (Entry<String, RepositoryName> e :
depGraphValue.getFullRepoMapping(modules).entries().entrySet()) {
jsonWriter.name(e.getKey()).value(e.getValue().getName());
}
jsonWriter.endObject();
}
jsonWriter.endObject();

jsonWriter.name("extension_repo_prefixes");
jsonWriter.beginObject();
for (Entry<ModuleExtensionId, String> e :
depGraphValue.getExtensionUniqueNames().entrySet()) {
ModuleKey owningModule =
depGraphValue
.getCanonicalRepoNameLookup()
.get(e.getKey().getBzlFileLabel().getRepository());
jsonWriter.name(e.getValue() + "~").value(owningModule.getCanonicalRepoName().getName());
}
jsonWriter.endObject();

jsonWriter.endObject();
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
Loading