update clean command to allow residue and ignore starlark options
work towards
Closes
package com.google.devtools.build.lib.runtime.commands;
import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Joiner;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
import com.google.common.flogger.GoogleLogger;
import com.google.devtools.build.lib.actions.ExecException;
import com.google.devtools.build.lib.analysis.NoBuildEvent;
@@ -26,14 +28,17 @@ import com.google.devtools.build.lib.runtime.BlazeCommandResult;
import com.google.devtools.build.lib.runtime.BlazeRuntime;
import com.google.devtools.build.lib.runtime.Command;
import com.google.devtools.build.lib.runtime.CommandEnvironment;
+import com.google.devtools.build.lib.runtime.StarlarkOptionsParser;
import com.google.devtools.build.lib.runtime.commands.events.CleanStartingEvent;
import com.google.devtools.build.lib.server.FailureDetails;
import com.google.devtools.build.lib.server.FailureDetails.CleanCommand.Code;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.shell.CommandException;
import com.google.devtools.build.lib.util.CommandBuilder;
+import com.google.devtools.build.lib.util.ExitCode;
import com.google.devtools.build.lib.util.InterruptedFailureDetails;
import com.google.devtools.build.lib.util.OS;
+import com.google.devtools.build.lib.util.Pair;
import com.google.devtools.build.lib.util.ProcessUtils;
import com.google.devtools.build.lib.util.ShellEscaper;
import com.google.devtools.build.lib.vfs.Path;
@@ -51,13 +56,13 @@ import java.util.logging.LogManager;
@Command(
name = "clean",
builds = true, // Does not, but people expect build options to be there
+ allowResidue = true, // Does not, but need to allow so we can ignore Starlark options.
writeCommandLog = false, // Do not create a command.log, otherwise we couldn't delete it.
options = {CleanCommand.Options.class},
help = "resource:clean.txt",
shortDescription = "Removes output files and optionally stops the server.",
// TODO(bazel-team): Remove this - we inherit a huge number of unused options.
- inherits = {BuildCommand.class}
-)
+ inherits = {BuildCommand.class})
public final class CleanCommand implements BlazeCommand {
/** An interface for special options for the clean command. */
public static class Options extends OptionsBase {
@@ -116,6 +121,24 @@ public final class CleanCommand implements BlazeCommand {
@Override
public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult options) {
+ // Assert that the only residue is starlark options and ignore them.
+ Pair<ImmutableList<String>, ImmutableList<String>> starlarkOptionsAndResidue =
+ StarlarkOptionsParser.removeStarlarkOptions(options.getResidue());
+ ImmutableList<String> removedStarlarkOptions = starlarkOptionsAndResidue.getFirst();
+ ImmutableList<String> residue = starlarkOptionsAndResidue.getSecond();
+ if (!removedStarlarkOptions.isEmpty()) {
+ env.getReporter()
+ .handle(
+ Event.warn(
+ "Blaze clean does not support starlark options. Ignoring options: "
+ + removedStarlarkOptions));
+ }
+ if (!residue.isEmpty()) {
+ String message = "Unrecognized arguments: " + Joiner.on(' ').join(residue);
+ env.getReporter().handle(Event.error(message));
+ return BlazeCommandResult.exitCode(ExitCode.COMMAND_LINE_ERROR);
+ }
+
Options cleanOptions = options.getOptions(Options.class);
boolean async = cleanOptions.async;
env.getEventBus().post(new NoBuildEvent());
