Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
Janak Ramakrishnan | 112840b | 2016-12-29 21:49:56 +0000 | [diff] [blame] | 17 | import static com.google.devtools.build.skyframe.WalkableGraphUtils.exists; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 18 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableMap; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 22 | import com.google.common.eventbus.EventBus; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
| 24 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
| 27 | import com.google.devtools.build.lib.analysis.util.AnalysisMock; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.cmdline.Label; |
| 29 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.events.Reporter; |
Googler | 7b2cb35 | 2019-04-09 14:01:44 -0700 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.PackageFactory; |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.pkgcache.PackageOptions; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 34 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
| 35 | import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction; |
| 36 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
michajlo | b0b312f | 2020-03-25 12:30:22 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.testutil.SkyframeExecutorTestHelper; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 38 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 40 | import com.google.devtools.build.lib.vfs.Root; |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 41 | import com.google.devtools.build.skyframe.EvaluationContext; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 43 | import com.google.devtools.build.skyframe.SkyKey; |
| 44 | import com.google.devtools.build.skyframe.SkyValue; |
| 45 | import com.google.devtools.build.skyframe.WalkableGraph; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 46 | import com.google.devtools.common.options.Options; |
Janak Ramakrishnan | 112840b | 2016-12-29 21:49:56 +0000 | [diff] [blame] | 47 | import java.io.IOException; |
jhorvitz | dd1d841 | 2020-08-01 05:59:14 -0700 | [diff] [blame] | 48 | import java.util.Optional; |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 49 | import java.util.UUID; |
| 50 | import org.junit.Before; |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 51 | import org.junit.Test; |
| 52 | import org.junit.runner.RunWith; |
| 53 | import org.junit.runners.JUnit4; |
| 54 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 55 | /** Tests for {@link PrepareDepsOfPatternsFunction}. */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 56 | @RunWith(JUnit4.class) |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 57 | public class PrepareDepsOfPatternsFunctionSmartNegationTest extends FoundationTestCase { |
| 58 | private SkyframeExecutor skyframeExecutor; |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 59 | private static final String ADDITIONAL_IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING = |
| 60 | "config/ignored.txt"; |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 61 | |
| 62 | private static SkyKey getKeyForLabel(Label label) { |
| 63 | // Note that these tests used to look for TargetMarker SkyKeys before TargetMarker was |
| 64 | // inlined in TransitiveTraversalFunction. Because TargetMarker is now inlined, it doesn't |
| 65 | // appear in the graph. Instead, these tests now look for TransitiveTraversal keys. |
| 66 | return TransitiveTraversalValue.key(label); |
| 67 | } |
| 68 | |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 69 | @Before |
| 70 | public void setUp() throws Exception { |
| 71 | BlazeDirectories directories = |
| 72 | new BlazeDirectories( |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 73 | new ServerDirectories( |
| 74 | getScratch().dir("/install"), |
| 75 | getScratch().dir("/output"), |
| 76 | getScratch().dir("/user_root")), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 77 | rootDirectory, |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 78 | /* defaultSystemJavabase= */ null, |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 79 | AnalysisMock.get().getProductName()); |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 80 | ConfiguredRuleClassProvider ruleClassProvider = AnalysisMock.get().createRuleClassProvider(); |
Googler | 7b2cb35 | 2019-04-09 14:01:44 -0700 | [diff] [blame] | 81 | |
| 82 | PackageFactory pkgFactory = |
| 83 | AnalysisMock.get() |
| 84 | .getPackageFactoryBuilderForTesting(directories) |
| 85 | .build(ruleClassProvider, fileSystem); |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 86 | skyframeExecutor = |
Googler | 7b2cb35 | 2019-04-09 14:01:44 -0700 | [diff] [blame] | 87 | BazelSkyframeExecutorConstants.newBazelSkyframeExecutorBuilder() |
| 88 | .setPkgFactory(pkgFactory) |
| 89 | .setFileSystem(fileSystem) |
| 90 | .setDirectories(directories) |
| 91 | .setActionKeyContext(new ActionKeyContext()) |
Googler | 7b2cb35 | 2019-04-09 14:01:44 -0700 | [diff] [blame] | 92 | .setExtraSkyFunctions(AnalysisMock.get().getSkyFunctions(directories)) |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 93 | .setIgnoredPackagePrefixesFunction( |
| 94 | new IgnoredPackagePrefixesFunction( |
| 95 | PathFragment.create(ADDITIONAL_IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING))) |
Googler | 7b2cb35 | 2019-04-09 14:01:44 -0700 | [diff] [blame] | 96 | .build(); |
michajlo | b0b312f | 2020-03-25 12:30:22 -0700 | [diff] [blame] | 97 | SkyframeExecutorTestHelper.process(skyframeExecutor); |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 98 | skyframeExecutor.preparePackageLoading( |
| 99 | new PathPackageLocator( |
| 100 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 101 | ImmutableList.of(Root.fromPath(rootDirectory)), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 102 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 103 | Options.getDefaults(PackageOptions.class), |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 104 | Options.getDefaults(BuildLanguageOptions.class), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 105 | UUID.randomUUID(), |
jhorvitz | 1e803ab | 2021-05-06 10:22:28 -0700 | [diff] [blame] | 106 | ImmutableMap.of(), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 107 | new TimestampGranularityMonitor(null)); |
jhorvitz | dd1d841 | 2020-08-01 05:59:14 -0700 | [diff] [blame] | 108 | skyframeExecutor.setActionEnv(ImmutableMap.of()); |
Klaus Aehlig | 93fe20c | 2018-06-14 05:48:46 -0700 | [diff] [blame] | 109 | skyframeExecutor.injectExtraPrecomputedValues( |
| 110 | ImmutableList.of( |
| 111 | PrecomputedValue.injected( |
jhorvitz | dd1d841 | 2020-08-01 05:59:14 -0700 | [diff] [blame] | 112 | RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()), |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 113 | PrecomputedValue.injected( |
jhorvitz | dd1d841 | 2020-08-01 05:59:14 -0700 | [diff] [blame] | 114 | RepositoryDelegatorFunction.REPOSITORY_OVERRIDES, ImmutableMap.of()), |
Klaus Aehlig | 93fe20c | 2018-06-14 05:48:46 -0700 | [diff] [blame] | 115 | PrecomputedValue.injected( |
| 116 | RepositoryDelegatorFunction.DEPENDENCY_FOR_UNCONDITIONAL_FETCHING, |
| 117 | RepositoryDelegatorFunction.DONT_FETCH_UNCONDITIONALLY))); |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 118 | scratch.file(ADDITIONAL_IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING); |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 121 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 122 | public void testRecursiveEvaluationFailsOnBadBuildFile() throws Exception { |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 123 | // Given a well-formed package "@//foo" and a malformed package "@//foo/foo", |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 124 | createFooAndFooFoo(); |
| 125 | |
| 126 | // Given a target pattern sequence consisting of a recursive pattern for "//foo/...", |
| 127 | ImmutableList<String> patternSequence = ImmutableList.of("//foo/..."); |
| 128 | |
| 129 | // When PrepareDepsOfPatternsFunction completes evaluation (with no error because it was |
| 130 | // recovered from), |
| 131 | WalkableGraph walkableGraph = |
| 132 | getGraphFromPatternsEvaluation( |
| 133 | patternSequence, /*successExpected=*/ true, /*keepGoing=*/ true); |
| 134 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 135 | // Then the graph contains package values for "@//foo" and "@//foo/foo", |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 136 | assertThat(exists(PackageValue.key(PackageIdentifier.parse("@//foo")), walkableGraph)).isTrue(); |
| 137 | assertThat(exists(PackageValue.key(PackageIdentifier.parse("@//foo/foo")), walkableGraph)) |
| 138 | .isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 139 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 140 | // But the graph does not contain a value for the target "@//foo/foo:foofoo". |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 141 | assertThat(exists(getKeyForLabel(Label.create("@//foo/foo", "foofoo")), walkableGraph)) |
| 142 | .isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 145 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 146 | public void testNegativePatternBlocksPatternEvaluation() throws Exception { |
| 147 | // Given a well-formed package "//foo" and a malformed package "//foo/foo", |
| 148 | createFooAndFooFoo(); |
| 149 | |
| 150 | // Given a target pattern sequence consisting of a recursive pattern for "//foo/..." followed |
| 151 | // by a negative pattern for the malformed package, |
| 152 | ImmutableList<String> patternSequence = ImmutableList.of("//foo/...", "-//foo/foo/..."); |
| 153 | |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 154 | assertSkipsFoo(patternSequence); |
| 155 | } |
| 156 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 157 | @Test |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 158 | public void testIgnoredPatternBlocksPatternEvaluation() throws Exception { |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 159 | // Given a well-formed package "//foo" and a malformed package "//foo/foo", |
| 160 | createFooAndFooFoo(); |
| 161 | |
| 162 | // Given a target pattern sequence consisting of a recursive pattern for "//foo/...", |
| 163 | ImmutableList<String> patternSequence = ImmutableList.of("//foo/..."); |
| 164 | |
kkress | 1847a01 | 2020-06-24 12:30:11 -0700 | [diff] [blame] | 165 | // and an ignored entry for the malformed package, |
| 166 | scratch.overwriteFile(ADDITIONAL_IGNORED_PACKAGE_PREFIXES_FILE_PATH_STRING, "foo/foo"); |
Eric Fellheimer | e040fe9 | 2015-11-09 23:54:46 +0000 | [diff] [blame] | 167 | |
| 168 | assertSkipsFoo(patternSequence); |
| 169 | } |
| 170 | |
| 171 | private void assertSkipsFoo(ImmutableList<String> patternSequence) throws Exception { |
| 172 | |
| 173 | |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 174 | // When PrepareDepsOfPatternsFunction completes evaluation (successfully), |
| 175 | WalkableGraph walkableGraph = |
| 176 | getGraphFromPatternsEvaluation( |
| 177 | patternSequence, /*successExpected=*/ true, /*keepGoing=*/ true); |
| 178 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 179 | // Then the graph contains a package value for "@//foo", |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 180 | assertThat(exists(PackageValue.key(PackageIdentifier.parse("@//foo")), walkableGraph)).isTrue(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 181 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 182 | // But no package value for "@//foo/foo", |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 183 | assertThat(exists(PackageValue.key(PackageIdentifier.parse("@//foo/foo")), walkableGraph)) |
| 184 | .isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 185 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 186 | // And the graph does not contain a value for the target "@//foo/foo:foofoo". |
| 187 | Label label = Label.create("@//foo/foo", "foofoo"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 188 | assertThat(exists(getKeyForLabel(label), walkableGraph)).isFalse(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 191 | @Test |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 192 | public void testNegativeNonTBDPatternsAreSkippedWithWarnings() throws Exception { |
| 193 | // Given a target pattern sequence with a negative non-TBD pattern, |
| 194 | ImmutableList<String> patternSequence = ImmutableList.of("-//foo/bar"); |
| 195 | |
| 196 | // When PrepareDepsOfPatternsFunction completes evaluation, |
| 197 | getGraphFromPatternsEvaluation(patternSequence, /*successExpected=*/ true, /*keepGoing=*/ true); |
| 198 | |
| 199 | // Then a event is published that says that negative non-TBD patterns are skipped. |
| 200 | assertContainsEvent( |
shreyax | 432b253 | 2019-01-23 11:16:07 -0800 | [diff] [blame] | 201 | "Skipping '-//foo/bar, excludedSubdirs=[], filteringPolicy=[]': Negative target patterns of" |
| 202 | + " types other than \"targets below directory\" are not permitted."); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // Helpers: |
| 206 | |
| 207 | private WalkableGraph getGraphFromPatternsEvaluation( |
| 208 | ImmutableList<String> patternSequence, boolean successExpected, boolean keepGoing) |
| 209 | throws InterruptedException { |
janakr | a3652a3 | 2020-09-10 12:05:20 -0700 | [diff] [blame] | 210 | SkyKey independentTarget = |
| 211 | PrepareDepsOfPatternsValue.key(patternSequence, PathFragment.EMPTY_FRAGMENT); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 212 | ImmutableList<SkyKey> singletonTargetPattern = ImmutableList.of(independentTarget); |
| 213 | |
| 214 | // When PrepareDepsOfPatternsFunction completes evaluation, |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 215 | EvaluationContext evaluationContext = |
| 216 | EvaluationContext.newBuilder() |
| 217 | .setKeepGoing(keepGoing) |
| 218 | .setNumThreads(100) |
michajlo | 7a485be | 2020-07-30 11:08:46 -0700 | [diff] [blame] | 219 | .setEventHandler(new Reporter(new EventBus(), eventCollector)) |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 220 | .build(); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 221 | EvaluationResult<SkyValue> evaluationResult = |
Googler | 0592f3f | 2019-07-12 09:00:46 -0700 | [diff] [blame] | 222 | skyframeExecutor.getDriver().evaluate(singletonTargetPattern, evaluationContext); |
Han-Wen Nienhuys | 81b9083 | 2015-10-26 16:57:27 +0000 | [diff] [blame] | 223 | // The evaluation has no errors if success was expected. |
| 224 | assertThat(evaluationResult.hasError()).isNotEqualTo(successExpected); |
| 225 | return Preconditions.checkNotNull(evaluationResult.getWalkableGraph()); |
| 226 | } |
| 227 | |
| 228 | private void createFooAndFooFoo() throws IOException { |
| 229 | scratch.file( |
| 230 | "foo/BUILD", "genrule(name = 'foo',", " outs = ['out.txt'],", " cmd = 'touch $@')"); |
| 231 | scratch.file( |
| 232 | "foo/foo/BUILD", "genrule(name = 'foofoo',", " This isn't even remotely grammatical.)"); |
| 233 | } |
| 234 | } |