Skip to content

Commit

Permalink
Flip --incompatible_windows_bashless_run_command
Browse files Browse the repository at this point in the history
    Fixes bazelbuild/bazel#8240

    RELNOTES[INC]: Windows: --incompatible_windows_bashless_run_command is now true by default, meaning "bazel run //foo:bin" will run the binary as a subprocess of the Bazel client. (When the flag is false, the binary is executed as a subprocess of Bash.)

    PiperOrigin-RevId: 266097847
  • Loading branch information
Luca Di Grazia committed Sep 4, 2022
1 parent f7a37fd commit 39d8a70
Showing 1 changed file with 14 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
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.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.devtools.build.lib.actions.Artifact;
Expand Down Expand Up @@ -57,7 +55,6 @@
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.packages.TargetUtils;
import com.google.devtools.build.lib.packages.Type;
import com.google.devtools.build.lib.pkgcache.LoadingFailedException;
import com.google.devtools.build.lib.runtime.BlazeCommand;
import com.google.devtools.build.lib.runtime.BlazeCommandResult;
Expand All @@ -68,6 +65,7 @@
import com.google.devtools.build.lib.server.CommandProtos.ExecRequest;
import com.google.devtools.build.lib.shell.CommandException;
import com.google.devtools.build.lib.shell.ShellUtils;
import com.google.devtools.build.lib.syntax.Type;
import com.google.devtools.build.lib.util.CommandDescriptionForm;
import com.google.devtools.build.lib.util.CommandFailureUtils;
import com.google.devtools.build.lib.util.ExitCode;
Expand Down Expand Up @@ -148,6 +146,11 @@ public static class RunOptions extends OptionsBase {
// Thrown when a method needs Bash but ShToolchain.getPath yields none.
private static final class NoShellFoundException extends Exception {}

@VisibleForTesting
public static final String SINGLE_TARGET_MESSAGE =
"Only a single target can be run. "
+ "Do not use wildcards that match more than one target";

@VisibleForTesting
public static final String NO_TARGET_MESSAGE = "No targets found to run";

Expand All @@ -169,13 +172,9 @@ public RunCommand(TestPolicy testPolicy) {

@VisibleForTesting // productionVisibility = Visibility.PRIVATE
protected BuildResult processRequest(final CommandEnvironment env, BuildRequest request) {
List<String> targetPatternStrings = request.getTargets();
return new BuildTool(env)
.processRequest(
request,
(Collection<Target> targets, boolean keepGoing) ->
RunCommand.this.validateTargets(
env.getReporter(), targetPatternStrings, targets, keepGoing));
return new BuildTool(env).processRequest(request,
(Collection<Target> targets, boolean keepGoing) ->
RunCommand.this.validateTargets(env.getReporter(), targets, keepGoing));
}

@Override
Expand Down Expand Up @@ -318,12 +317,7 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
if (targetsBuilt != null) {
int maxTargets = runUnder != null && runUnder.getLabel() != null ? 2 : 1;
if (targetsBuilt.size() > maxTargets) {
env.getReporter()
.handle(
Event.error(
makeErrorMessageForNotHavingASingleTarget(
targetString,
Iterables.transform(targetsBuilt, ct -> ct.getLabel().toString()))));
env.getReporter().handle(Event.error(SINGLE_TARGET_MESSAGE));
return BlazeCommandResult.exitCode(ExitCode.COMMAND_LINE_ERROR);
}
for (ConfiguredTarget target : targetsBuilt) {
Expand All @@ -341,12 +335,7 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
} else if (targetToRun == null) {
targetToRun = target;
} else {
env.getReporter()
.handle(
Event.error(
makeErrorMessageForNotHavingASingleTarget(
targetString,
Iterables.transform(targetsBuilt, ct -> ct.getLabel().toString()))));
env.getReporter().handle(Event.error(SINGLE_TARGET_MESSAGE));
return BlazeCommandResult.exitCode(ExitCode.COMMAND_LINE_ERROR);
}
}
Expand Down Expand Up @@ -628,24 +617,15 @@ private boolean writeScript(

// Make sure we are building exactly 1 binary target.
// If keepGoing, we'll build all the targets even if they are non-binary.
private void validateTargets(
Reporter reporter,
List<String> targetPatternStrings,
Collection<Target> targets,
boolean keepGoing)
private void validateTargets(Reporter reporter, Collection<Target> targets, boolean keepGoing)
throws LoadingFailedException {
Target targetToRun = null;
Target runUnderTarget = null;

boolean singleTargetWarningWasOutput = false;
int maxTargets = currentRunUnder != null && currentRunUnder.getLabel() != null ? 2 : 1;
if (targets.size() > maxTargets) {
warningOrException(
reporter,
makeErrorMessageForNotHavingASingleTarget(
targetPatternStrings.get(0),
Iterables.transform(targets, t -> t.getLabel().toString())),
keepGoing);
warningOrException(reporter, SINGLE_TARGET_MESSAGE, keepGoing);
singleTargetWarningWasOutput = true;
}
for (Target target : targets) {
Expand All @@ -662,12 +642,7 @@ private void validateTargets(
targetToRun = target;
} else {
if (!singleTargetWarningWasOutput) {
warningOrException(
reporter,
makeErrorMessageForNotHavingASingleTarget(
targetPatternStrings.get(0),
Iterables.transform(targets, t -> t.getLabel().toString())),
keepGoing);
warningOrException(reporter, SINGLE_TARGET_MESSAGE, keepGoing);
}
return;
}
Expand Down Expand Up @@ -789,19 +764,4 @@ private static boolean isAliasRule(Target target) {
Rule rule = (Rule) target;
return rule.getRuleClass().equals("alias") || rule.getRuleClass().equals("bind");
}

private String makeErrorMessageForNotHavingASingleTarget(
String targetPatternString, Iterable<String> expandedTargetNames) {
final int maxNumExpandedTargetsToIncludeInErrorMessage = 5;
boolean truncateTargetNameList = Iterables.size(expandedTargetNames) > 5;
Iterable<String> targetNamesToIncludeInErrorMessage =
truncateTargetNameList
? Iterables.limit(expandedTargetNames, maxNumExpandedTargetsToIncludeInErrorMessage)
: expandedTargetNames;
return String.format(
"Only a single target can be run. Your target pattern %s expanded to the targets %s%s",
targetPatternString,
Joiner.on(", ").join(ImmutableSortedSet.copyOf(targetNamesToIncludeInErrorMessage)),
truncateTargetNameList ? "[TRUNCATED]" : "");
}
}

0 comments on commit 39d8a70

Please sign in to comment.