Skip to content
Merged
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
29 changes: 28 additions & 1 deletion site/en/external/mod-command.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ modules can stand in for the corresponding repos.
The `<label_to_bzl_file>` part must be a repo-relative label (for example,
`//pkg/path:file.bzl`).

### Graph command options

The following options only affect the subcommands that print graphs (`graph`,
`deps`, `all_paths`, `path`, and `explain`):

Expand Down Expand Up @@ -143,7 +145,7 @@ The following options only affect the subcommands that print graphs (`graph`,
legacy platforms which cannot use Unicode.

* `--output <mode>`: Include information about the module extension usages as
part of the output graph. `<mode`> can be one of:
part of the output graph. `<mode>` can be one of:

* `text` *(default)*: A human-readable representation of the output graph
(flattened as a tree).
Expand All @@ -160,6 +162,31 @@ The following options only affect the subcommands that print graphs (`graph`,
bazel mod graph --output graph | dot -Tsvg > /tmp/graph.svg
```

### show_repo options

`show_repo` supports a different set of output formats:

* `--output <mode>`: Change how repository definitions are displayed.
`<mode>` can be one of:

* `text` *(default)*: Display repository definitions in Starlark.

* `streamed_proto`: Prints a
[length-delimited](https://protobuf.dev/programming-guides/encoding/#siz
e-limit)
stream of
[`Repository`](https://github.com/bazelbuild/bazel/blob/master/src/main/
protobuf/build.proto)
protocol buffers.

* `streamed_jsonproto`: Similar to `--output streamed_proto`, prints a
stream of [`Repository`](https://github.com/bazelbuild/bazel/blob/master
/src/main/protobuf/build.proto)
protocol buffers but in [NDJSON](https://github.com/ndjson/ndjson-spec)
format.

### Other options

Other options include:

* `--base_module <arg>` *default: `<root>`*: Specify a module relative to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,17 @@ java_library(
"//src/main/java/com/google/devtools/build/lib/packages:label_printer",
"//src/main/java/com/google/devtools/build/lib/query2/query/output",
"//src/main/java/com/google/devtools/build/lib/util:maybe_complete_set",
"//src/main/java/com/google/devtools/build/lib/util:string_encoding",
"//src/main/java/com/google/devtools/common/options",
"//src/main/java/net/starlark/java/eval",
"//src/main/protobuf:build_java_proto",
"//src/main/protobuf:failure_details_java_proto",
"//third_party:auto_value",
"//third_party:error_prone_annotations",
"//third_party:gson",
"//third_party:guava",
"//third_party:jsr305",
"@com_google_protobuf//:protobuf_java",
"@com_google_protobuf//:protobuf_java_util",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.ImmutableSortedMap.toImmutableSortedMap;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Comparator.reverseOrder;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
Expand All @@ -39,17 +41,12 @@
import com.google.devtools.build.lib.bazel.bzlmod.modcommand.ModExecutor.ResultNode.IsExpanded;
import com.google.devtools.build.lib.bazel.bzlmod.modcommand.ModExecutor.ResultNode.IsIndirect;
import com.google.devtools.build.lib.bazel.bzlmod.modcommand.ModExecutor.ResultNode.NodeMetadata;
import com.google.devtools.build.lib.packages.LabelPrinter;
import com.google.devtools.build.lib.packages.RawAttributeMapper;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.query2.query.output.BuildOutputFormatter.AttributeReader;
import com.google.devtools.build.lib.query2.query.output.BuildOutputFormatter.TargetOutputter;
import com.google.devtools.build.lib.query2.query.output.PossibleAttributeValues;
import com.google.devtools.build.lib.util.MaybeCompleteSet;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -75,19 +72,22 @@ public class ModExecutor {
private final ImmutableSetMultimap<ModuleExtensionId, String> extensionRepos;
private final Optional<MaybeCompleteSet<ModuleExtensionId>> extensionFilter;
private final ModOptions options;
private final OutputStream outputStream;
private final PrintWriter printer;
private ImmutableMap<ModuleExtensionId, ImmutableSetMultimap<String, ModuleKey>>
extensionRepoImports;

public ModExecutor(
ImmutableMap<ModuleKey, AugmentedModule> depGraph, ModOptions options, Writer writer) {
ImmutableMap<ModuleKey, AugmentedModule> depGraph,
ModOptions options,
OutputStream outputStream) {
this(
depGraph,
ImmutableTable.of(),
ImmutableSetMultimap.of(),
Optional.of(MaybeCompleteSet.completeSet()),
options,
writer);
outputStream);
}

public ModExecutor(
Expand All @@ -96,13 +96,17 @@ public ModExecutor(
ImmutableSetMultimap<ModuleExtensionId, String> extensionRepos,
Optional<MaybeCompleteSet<ModuleExtensionId>> extensionFilter,
ModOptions options,
Writer writer) {
OutputStream outputStream) {
this.depGraph = depGraph;
this.extensionUsages = extensionUsages;
this.extensionRepos = extensionRepos;
this.extensionFilter = extensionFilter;
this.options = options;
this.printer = new PrintWriter(writer);
this.outputStream = outputStream;
this.printer =
new PrintWriter(
new OutputStreamWriter(
outputStream, options.charset == ModOptions.Charset.UTF8 ? UTF_8 : US_ASCII));
// Easier lookup table for repo imports by module.
// It is updated after pruneByDepthAndLink to filter out pruned modules.
this.extensionRepoImports = computeRepoImportsTable(depGraph.keySet());
Expand Down Expand Up @@ -167,10 +171,15 @@ public void allPaths(ImmutableSet<ModuleKey> from, ImmutableSet<ModuleKey> to) {
}

public void showRepo(ImmutableMap<String, BzlmodRepoRuleValue> targetRepoRuleValues) {
RuleDisplayOutputter outputter = new RuleDisplayOutputter(printer);
var formatter = new RepoOutputFormatter(printer, outputStream, options.outputFormat);
for (Entry<String, BzlmodRepoRuleValue> e : targetRepoRuleValues.entrySet()) {
printer.printf("## %s:\n", e.getKey());
outputter.outputRule(e.getValue().getRule());
formatter.print(e.getKey(), e.getValue());
}

try {
outputStream.flush();
} catch (IOException ex) {
// Ignore IOException like PrintWriter.
}
printer.flush();
}
Expand Down Expand Up @@ -748,35 +757,4 @@ final Builder addCycle(ModuleKey value) {
abstract ResultNode build();
}
}

/**
* Uses Query's {@link TargetOutputter} to display the generating repo rule and other information.
*/
static class RuleDisplayOutputter {
private static final AttributeReader attrReader =
(rule, attr) ->
// Query's implementation copied
PossibleAttributeValues.forRuleAndAttribute(
rule, attr, /* mayTreatMultipleAsNone= */ true);
private final TargetOutputter targetOutputter;
private final PrintWriter printer;

RuleDisplayOutputter(PrintWriter printer) {
this.printer = printer;
this.targetOutputter =
new TargetOutputter(
this.printer,
(rule, attr) -> RawAttributeMapper.of(rule).isConfigurable(attr.getName()),
"\n",
LabelPrinter.legacy());
}

private void outputRule(Rule rule) {
try {
targetOutputter.outputRule(rule, attrReader, this.printer);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,21 @@ public CharsetConverter() {

/** Possible formats of the `mod` command result. */
public enum OutputFormat {
// Default
TEXT,

// For graph commands:
JSON,
GRAPH
GRAPH,

// For show_repo:
STREAMED_PROTO,
STREAMED_JSONPROTO;

@Override
public String toString() {
return Ascii.toLowerCase(this.name());
}
}

/** Converts an output format option string to a properly typed {@link OutputFormat} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static OutputFormatter getFormatter(OutputFormat format) {
case JSON -> jsonFormatter;
case GRAPH -> graphvizFormatter;
case null -> throw new IllegalArgumentException("Output format cannot be null.");
default -> throw new IllegalArgumentException("Unsupported output format: " + format);
};
}

Expand Down
Loading
Loading