Skip to content

Commit

Permalink
Mod command fails if bzlmod is disabled (fixes: #21303)
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 607621058
Change-Id: Id58e92d4f80e408e5bf4deabe08e8a864940de63
  • Loading branch information
SalmaSamy committed Feb 16, 2024
1 parent ec80f56 commit 645d471
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions;
import com.google.devtools.build.lib.pkgcache.PackageOptions;
import com.google.devtools.build.lib.runtime.BlazeCommand;
import com.google.devtools.build.lib.runtime.BlazeCommandResult;
Expand All @@ -82,8 +83,6 @@
import com.google.devtools.build.skyframe.SkyFunctionException;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import com.google.devtools.common.options.OptionPriority.PriorityCategory;
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.Gson;
Expand Down Expand Up @@ -120,20 +119,14 @@ public final class ModCommand implements BlazeCommand {
public static final String NAME = "mod";

@Override
public void editOptions(OptionsParser optionsParser) {
try {
optionsParser.parse(
PriorityCategory.SOFTWARE_REQUIREMENT,
"Option required by the mod command",
ImmutableList.of("--enable_bzlmod"));
} catch (OptionsParsingException e) {
// Should never happen.
throw new IllegalStateException("Unexpected exception", e);
public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult options) {
if (!options.getOptions(BuildLanguageOptions.class).enableBzlmod) {
return reportAndCreateFailureResult(
env,
"Bzlmod has to be enabled for mod command to work, run with --enable_bzlmod",
Code.MISSING_ARGUMENTS);
}
}

@Override
public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult options) {
env.getEventBus()
.post(
new NoBuildEvent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Usage: %{product} %{command} [<option> ...] tidy|<query_type> [<args> ...]

When invoked as %{product} %{command} tidy, the command will format the MODULE.bazel file and update all use_repo calls for supported module extensions.
Only works when bzlmod is enabled via --enable_bzlmod.

The command will display the external dependency graph or parts thereof, structured to display different kinds of insights depending on the query type.

Expand Down
10 changes: 10 additions & 0 deletions src/test/py/bazel/bzlmod/mod_command_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ def setUp(self):
scratchFile(self.projects_dir.joinpath('ext2', 'BUILD'))
scratchFile(self.projects_dir.joinpath('ext2', 'ext.bzl'), ext_src)

def testFailWithoutBzlmod(self):
_, _, stderr = self.RunBazel(
['mod', 'graph', '--noenable_bzlmod'], allow_failure=True
)
self.assertIn(
'ERROR: Bzlmod has to be enabled for mod command to work, run with '
"--enable_bzlmod. Type 'bazel help mod' for syntax and help.",
stderr,
)

def testGraph(self):
_, stdout, _ = self.RunBazel(['mod', 'graph'], rstrip=True)
self.assertListEqual(
Expand Down

0 comments on commit 645d471

Please sign in to comment.