Skip to content

Commit b8ac792

Browse files
haxorzCopybara-Service
authored andcommitted
Fix long-standing bug with Blaze's exit code in --keep_going mode when encountering a package loading error during target pattern parsing of patterns like "//foo:all" and "//foo/...".
The bug is that we were completely ignoring package loading errors in the SkyFunction work that went into TargetPatternPhaseValue computation, and so therefore the control flow all the way from SkyframeExecutor#loadTargetPatterns to BuildView#createErrorMessage could never hope to take such a package loading error into account. Fixes #7115 RELNOTES: In --keep_going mode, Bazel now correctly returns a non-zero exit code when encountering a package loading error during target pattern parsing of patterns like "//foo:all" and "//foo/...". PiperOrigin-RevId: 229839139
1 parent 75a422a commit b8ac792

5 files changed

Lines changed: 89 additions & 6 deletions

File tree

‎src/main/java/com/google/devtools/build/lib/skyframe/EnvironmentBackedRecursivePackageProvider.java‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.LinkedHashSet;
4040
import java.util.List;
4141
import java.util.Map;
42+
import java.util.concurrent.atomic.AtomicBoolean;
4243

4344
/**
4445
* A {@link RecursivePackageProvider} backed by an {@link Environment}. Its methods may throw {@link
@@ -52,11 +53,18 @@ public final class EnvironmentBackedRecursivePackageProvider
5253
extends AbstractRecursivePackageProvider {
5354

5455
private final Environment env;
56+
private final AtomicBoolean encounteredPackageErrors = new AtomicBoolean(false);
5557

5658
EnvironmentBackedRecursivePackageProvider(Environment env) {
5759
this.env = env;
5860
}
5961

62+
// TODO(nharmata): Audit the rest of the codebase to determine if we should be calling this method
63+
// in more places.
64+
boolean encounteredPackageErrors() {
65+
return encounteredPackageErrors.get();
66+
}
67+
6068
@Override
6169
public Package getPackage(ExtendedEventHandler eventHandler, PackageIdentifier packageName)
6270
throws NoSuchPackageException, MissingDepException, InterruptedException {
@@ -77,7 +85,11 @@ public Package getPackage(ExtendedEventHandler eventHandler, PackageIdentifier p
7785
Preconditions.checkState(env.valuesMissing(), "Should have thrown for %s", packageName);
7886
throw new MissingDepException();
7987
} catch (BuildFileContainsErrorsException e) {
80-
// Expected.
88+
// If this is a keep_going build, then the user of this RecursivePackageProvider has two
89+
// options for handling the "package in error" case. The user must either inspect the
90+
// package returned by this method, or else determine whether any errors have been seen via
91+
// the "encounteredPackageErrors" method.
92+
encounteredPackageErrors.set(true);
8193
}
8294
}
8395
return pkgValue.getPackage();
@@ -107,6 +119,7 @@ public boolean isPackage(ExtendedEventHandler eventHandler, PackageIdentifier pa
107119
return packageLookupValue.packageExists();
108120
} catch (NoSuchPackageException | InconsistentFilesystemException e) {
109121
env.getListener().handle(Event.error(e.getMessage()));
122+
encounteredPackageErrors.set(true);
110123
return false;
111124
}
112125
}

‎src/main/java/com/google/devtools/build/lib/skyframe/PackageErrorFunction.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@
3333

3434
/**
3535
* SkyFunction that throws a {@link BuildFileContainsErrorsException} for {@link Package} that
36-
* loaded, but was in error. Must only be requested when a SkyFunction wishes to ignore the errors
37-
* in a {@link Package} in keep_going mode, but to shut down the build in nokeep_going mode. Thus,
38-
* this SkyFunction should only be requested when the corresponding {@link PackageFunction} has
39-
* already been successfully called and the resulting Package contains an error.
36+
* loaded, but was in error. Must only be requested when a SkyFunction wishes to ignore the Skyframe
37+
* error from a {@link PackageValue} in keep_going mode, but to shut down the build in nokeep_going
38+
* mode. Thus, this SkyFunction should only be requested when the corresponding {@link
39+
* PackageFunction} has already been successfully called and the resulting Package contains an
40+
* error.
4041
*
4142
* <p>This SkyFunction never returns a value, only throws a {@link BuildFileNotFoundException}, and
4243
* should never return null, since all of its dependencies should already be present.

‎src/main/java/com/google/devtools/build/lib/skyframe/TargetPatternFunction.java‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ public void process(Iterable<Target> partialResult) {
8282
// The exception type here has to match the one on the BatchCallback. Since the callback
8383
// defined above never throws, the exact type here is not really relevant.
8484
RuntimeException.class);
85-
resolvedTargets = ResolvedTargets.<Target>builder().addAll(results).build();
85+
ResolvedTargets.Builder<Target> resolvedTargetsBuilder =
86+
ResolvedTargets.<Target>builder().addAll(results);
87+
if (provider.encounteredPackageErrors()) {
88+
resolvedTargetsBuilder.setError();
89+
}
90+
resolvedTargets = resolvedTargetsBuilder.build();
8691
} catch (TargetParsingException e) {
8792
env.getListener().post(new ParsingFailedEvent(patternKey.getPattern(), e.getMessage()));
8893
throw new TargetPatternFunctionException(e);
@@ -96,6 +101,9 @@ public void process(Iterable<Target> partialResult) {
96101
}
97102
Preconditions.checkNotNull(resolvedTargets, key);
98103
ResolvedTargets.Builder<Label> resolvedLabelsBuilder = ResolvedTargets.builder();
104+
if (resolvedTargets.hasError()) {
105+
resolvedLabelsBuilder.setError();
106+
}
99107
for (Target target : resolvedTargets.getTargets()) {
100108
resolvedLabelsBuilder.add(target.getLabel());
101109
}

‎src/test/java/com/google/devtools/build/lib/pkgcache/LoadingPhaseRunnerTest.java‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.devtools.build.lib.events.EventKind;
4444
import com.google.devtools.build.lib.events.ExtendedEventHandler.Postable;
4545
import com.google.devtools.build.lib.events.StoredEventHandler;
46+
import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException;
4647
import com.google.devtools.build.lib.packages.ConstantRuleVisibility;
4748
import com.google.devtools.build.lib.packages.PackageFactory;
4849
import com.google.devtools.build.lib.packages.SkylarkSemanticsOptions;
@@ -1003,6 +1004,51 @@ public void testPatternStartingWithDotDotSlash() throws Exception {
10031004
"Bad target pattern '../foo': package name component contains only '.' characters");
10041005
}
10051006

1007+
private void runTestPackageLoadingError(boolean keepGoing, String... patterns) throws Exception {
1008+
tester.addFile("bad/BUILD", "nope");
1009+
if (keepGoing) {
1010+
TargetPatternPhaseValue value = tester.loadKeepGoing(patterns);
1011+
assertThat(value.hasError()).isTrue();
1012+
tester.assertContainsWarning("Target pattern parsing failed");
1013+
} else {
1014+
TargetParsingException exn =
1015+
assertThrows(TargetParsingException.class, () -> tester.load(patterns));
1016+
assertThat(exn).hasCauseThat().isInstanceOf(BuildFileContainsErrorsException.class);
1017+
assertThat(exn).hasCauseThat().hasMessageThat().contains("Package 'bad' contains errors");
1018+
}
1019+
tester.assertContainsError("/workspace/bad/BUILD:1:1: name 'nope' is not defined");
1020+
}
1021+
1022+
@Test
1023+
public void testPackageLoadingError_KeepGoing_ExplicitTarget() throws Exception {
1024+
runTestPackageLoadingError(/*keepGoing=*/ true, "//bad:BUILD");
1025+
}
1026+
1027+
@Test
1028+
public void testPackageLoadingError_NoKeepGoing_ExplicitTarget() throws Exception {
1029+
runTestPackageLoadingError(/*keepGoing=*/ false, "//bad:BUILD");
1030+
}
1031+
1032+
@Test
1033+
public void testPackageLoadingError_KeepGoing_TargetsInPackage() throws Exception {
1034+
runTestPackageLoadingError(/*keepGoing=*/ true, "//bad:all");
1035+
}
1036+
1037+
@Test
1038+
public void testPackageLoadingError_NoKeepGoing_TargetsInPackage() throws Exception {
1039+
runTestPackageLoadingError(/*keepGoing=*/ false, "//bad:all");
1040+
}
1041+
1042+
@Test
1043+
public void testPackageLoadingError_KeepGoing_TargetsBeneathDirectory() throws Exception {
1044+
runTestPackageLoadingError(/*keepGoing=*/ true, "//bad/...");
1045+
}
1046+
1047+
@Test
1048+
public void testPackageLoadingError_NoKeepGoing_TargetsBeneathDirectory() throws Exception {
1049+
runTestPackageLoadingError(/*keepGoing=*/ false, "//bad/...");
1050+
}
1051+
10061052
private static class LoadingPhaseTester {
10071053
private final ManualClock clock = new ManualClock();
10081054
private final Path workspace;

‎src/test/shell/integration/loading_phase_test.sh‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,19 @@ function test_incompatible_disallow_load_labels_to_cross_package_boundaries() {
377377
expect_log "//$pkg/foo:BUILD"
378378
}
379379

380+
function test_package_loading_errors_in_target_parsing() {
381+
mkdir bad || fail "mkdir failed"
382+
echo "nope" > bad/BUILD || fail "echo failed"
383+
384+
for keep_going in "--keep_going" "--nokeep_going"
385+
do
386+
for target_pattern in "//bad:BUILD" "//bad:all" "//bad/..."
387+
do
388+
bazel build --nobuild "$target_pattern" >& "$TEST_log" \
389+
&& fail "Expected failure"
390+
expect_log "Build did NOT complete successfully"
391+
done
392+
done
393+
}
394+
380395
run_suite "Integration tests of ${PRODUCT_NAME} using loading/analysis phases."

0 commit comments

Comments
 (0)