Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +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 | |
| 15 | package com.google.devtools.build.lib.skyframe; |
| 16 | |
| 17 | import static com.google.common.truth.Truth.assertThat; |
nharmata | 5861310 | 2018-10-11 15:10:04 -0700 | [diff] [blame] | 18 | import static com.google.devtools.build.skyframe.EvaluationResultSubjectFactory.assertThatEvaluationResult; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 19 | import static org.junit.Assert.assertThrows; |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 20 | import static org.junit.Assert.fail; |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.Mockito.doAnswer; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 23 | |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 24 | import com.google.common.base.Predicates; |
| 25 | import com.google.common.collect.ImmutableList; |
| 26 | import com.google.common.collect.ImmutableMap; |
| 27 | import com.google.common.collect.Maps; |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 28 | import com.google.common.collect.Sets; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.FileStateValue; |
brandjon | 38dafdd | 2020-08-06 09:49:13 -0700 | [diff] [blame] | 30 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
philwo | 3bcb9f6 | 2017-09-06 12:52:21 +0200 | [diff] [blame] | 32 | import com.google.devtools.build.lib.clock.BlazeClock; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.cmdline.Label; |
| 34 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
michajlo | a96b2d2 | 2020-02-26 14:54:50 -0800 | [diff] [blame] | 35 | import com.google.devtools.build.lib.events.Event; |
| 36 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
adonovan | 87b4608 | 2020-07-08 15:58:04 -0700 | [diff] [blame] | 37 | import com.google.devtools.build.lib.packages.BazelModuleContext; |
nharmata | 5861310 | 2018-10-11 15:10:04 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.packages.BuildFileNotFoundException; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.packages.ConstantRuleVisibility; |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 40 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.packages.Package; |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 43 | import com.google.devtools.build.lib.packages.PackageValidator; |
| 44 | import com.google.devtools.build.lib.packages.PackageValidator.InvalidPackageException; |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 45 | import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 46 | import com.google.devtools.build.lib.pkgcache.PackageOptions; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 48 | import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunction; |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 49 | import com.google.devtools.build.lib.server.FailureDetails.PackageLoading; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; |
| 51 | import com.google.devtools.build.lib.testutil.ManualClock; |
brandjon | 38dafdd | 2020-08-06 09:49:13 -0700 | [diff] [blame] | 52 | import com.google.devtools.build.lib.testutil.TestRuleClassProvider; |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 53 | import com.google.devtools.build.lib.util.DetailedExitCode; |
| 54 | import com.google.devtools.build.lib.util.ExitCode; |
Benjamin Peterson | 723eca6 | 2019-07-22 17:24:10 -0700 | [diff] [blame] | 55 | import com.google.devtools.build.lib.util.Pair; |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 57 | import com.google.devtools.build.lib.vfs.DigestHashFunction; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 58 | import com.google.devtools.build.lib.vfs.Dirent; |
| 59 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 60 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 61 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 62 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
| 63 | import com.google.devtools.build.lib.vfs.Path; |
| 64 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 65 | import com.google.devtools.build.lib.vfs.Root; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.vfs.RootedPath; |
nharmata | ce0335a | 2019-11-13 15:48:05 -0800 | [diff] [blame] | 67 | import com.google.devtools.build.lib.vfs.Symlinks; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 68 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 69 | import com.google.devtools.build.skyframe.ErrorInfo; |
| 70 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 71 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
| 72 | import com.google.devtools.build.skyframe.SkyKey; |
| 73 | import com.google.devtools.build.skyframe.SkyValue; |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 74 | import com.google.devtools.common.options.Options; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 75 | import java.io.IOException; |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 76 | import java.io.InputStream; |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 77 | import java.util.ArrayList; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 78 | import java.util.Arrays; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 79 | import java.util.Collection; |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 80 | import java.util.List; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 81 | import java.util.Map; |
jhorvitz | dd1d841 | 2020-08-01 05:59:14 -0700 | [diff] [blame] | 82 | import java.util.Optional; |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 83 | import java.util.Set; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 84 | import java.util.UUID; |
michajlo | 9081022 | 2020-03-02 09:36:23 -0800 | [diff] [blame] | 85 | import java.util.concurrent.atomic.AtomicInteger; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 86 | import javax.annotation.Nullable; |
adonovan | 450c7ad | 2020-09-14 13:00:21 -0700 | [diff] [blame] | 87 | import net.starlark.java.eval.Module; |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 88 | import net.starlark.java.eval.StarlarkInt; |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 89 | import org.junit.Rule; |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 90 | import org.junit.Test; |
| 91 | import org.junit.runner.RunWith; |
| 92 | import org.junit.runners.JUnit4; |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 93 | import org.mockito.Mock; |
| 94 | import org.mockito.junit.MockitoJUnit; |
| 95 | import org.mockito.junit.MockitoRule; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 96 | |
| 97 | /** |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 98 | * Unit tests of specific functionality of PackageFunction. Note that it's already tested indirectly |
| 99 | * in several other places. |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 100 | */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 101 | @RunWith(JUnit4.class) |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 102 | public class PackageFunctionTest extends BuildViewTestCase { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 103 | |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 104 | @Rule public final MockitoRule mockito = MockitoJUnit.rule(); |
| 105 | |
| 106 | @Mock private PackageValidator mockPackageValidator; |
| 107 | |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 108 | private CustomInMemoryFs fs = new CustomInMemoryFs(new ManualClock()); |
| 109 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 110 | private void preparePackageLoading(Path... roots) { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 111 | preparePackageLoadingWithCustomStarklarkSemanticsOptions( |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 112 | Options.getDefaults(BuildLanguageOptions.class), roots); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | private void preparePackageLoadingWithCustomStarklarkSemanticsOptions( |
brandjon | 6dbfa3e | 2020-10-06 19:31:08 -0700 | [diff] [blame^] | 116 | BuildLanguageOptions buildLanguageOptions, Path... roots) { |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 117 | PackageOptions packageOptions = Options.getDefaults(PackageOptions.class); |
| 118 | packageOptions.defaultVisibility = ConstantRuleVisibility.PUBLIC; |
| 119 | packageOptions.showLoadingProgress = true; |
| 120 | packageOptions.globbingThreads = 7; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 121 | getSkyframeExecutor() |
| 122 | .preparePackageLoading( |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 123 | new PathPackageLocator( |
| 124 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 125 | Arrays.stream(roots).map(Root::fromPath).collect(ImmutableList.toImmutableList()), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 126 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 127 | packageOptions, |
brandjon | 6dbfa3e | 2020-10-06 19:31:08 -0700 | [diff] [blame^] | 128 | buildLanguageOptions, |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 129 | UUID.randomUUID(), |
| 130 | ImmutableMap.<String, String>of(), |
| 131 | new TimestampGranularityMonitor(BlazeClock.instance())); |
juliexxia | 651797f | 2018-08-01 11:45:13 -0700 | [diff] [blame] | 132 | skyframeExecutor.setActionEnv(ImmutableMap.<String, String>of()); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 135 | @Override |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 136 | protected FileSystem createFileSystem() { |
| 137 | return fs; |
| 138 | } |
| 139 | |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 140 | @Override |
| 141 | protected PackageValidator getPackageValidator() { |
| 142 | return mockPackageValidator; |
| 143 | } |
| 144 | |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 145 | private Package validPackageWithoutErrors(SkyKey skyKey) throws InterruptedException { |
| 146 | return validPackageInternal(skyKey, /*checkPackageError=*/ true); |
| 147 | } |
| 148 | |
| 149 | private Package validPackage(SkyKey skyKey) throws InterruptedException { |
| 150 | return validPackageInternal(skyKey, /*checkPackageError=*/ false); |
| 151 | } |
| 152 | |
| 153 | private Package validPackageInternal(SkyKey skyKey, boolean checkPackageError) |
| 154 | throws InterruptedException { |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 155 | SkyframeExecutor skyframeExecutor = getSkyframeExecutor(); |
| 156 | skyframeExecutor.injectExtraPrecomputedValues( |
| 157 | ImmutableList.of( |
| 158 | PrecomputedValue.injected( |
jhorvitz | dd1d841 | 2020-08-01 05:59:14 -0700 | [diff] [blame] | 159 | RepositoryDelegatorFunction.RESOLVED_FILE_INSTEAD_OF_WORKSPACE, Optional.empty()))); |
Klaus Aehlig | 8eb4748 | 2018-09-17 09:14:58 -0700 | [diff] [blame] | 160 | EvaluationResult<PackageValue> result = |
| 161 | SkyframeExecutorTestUtils.evaluate( |
| 162 | skyframeExecutor, skyKey, /*keepGoing=*/ false, reporter); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 163 | if (result.hasError()) { |
| 164 | fail(result.getError(skyKey).getException().getMessage()); |
| 165 | } |
| 166 | PackageValue value = result.get(skyKey); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 167 | if (checkPackageError) { |
| 168 | assertThat(value.getPackage().containsErrors()).isFalse(); |
| 169 | } |
| 170 | return value.getPackage(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 173 | @Test |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 174 | public void testValidPackage() throws Exception { |
| 175 | scratch.file("pkg/BUILD"); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 176 | validPackageWithoutErrors(PackageValue.key(PackageIdentifier.parse("@//pkg"))); |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | @Test |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 180 | public void testInvalidPackage() throws Exception { |
| 181 | scratch.file("pkg/BUILD", "sh_library(name='foo', srcs=['foo.sh'])"); |
| 182 | scratch.file("pkg/foo.sh"); |
| 183 | |
| 184 | doAnswer( |
| 185 | inv -> { |
| 186 | Package pkg = inv.getArgument(0, Package.class); |
| 187 | if (pkg.getName().equals("pkg")) { |
michajlo | a96b2d2 | 2020-02-26 14:54:50 -0800 | [diff] [blame] | 188 | inv.getArgument(1, ExtendedEventHandler.class).handle(Event.warn("warning event")); |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 189 | throw new InvalidPackageException(pkg.getPackageIdentifier(), "no good"); |
| 190 | } |
| 191 | return null; |
| 192 | }) |
| 193 | .when(mockPackageValidator) |
michajlo | a96b2d2 | 2020-02-26 14:54:50 -0800 | [diff] [blame] | 194 | .validate(any(Package.class), any(ExtendedEventHandler.class)); |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 195 | |
| 196 | invalidatePackages(); |
| 197 | |
| 198 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 199 | EvaluationResult<PackageValue> result = |
| 200 | SkyframeExecutorTestUtils.evaluate( |
| 201 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
| 202 | assertThatEvaluationResult(result).hasError(); |
| 203 | assertThatEvaluationResult(result) |
| 204 | .hasErrorEntryForKeyThat(skyKey) |
| 205 | .hasExceptionThat() |
| 206 | .isInstanceOf(InvalidPackageException.class); |
| 207 | assertThatEvaluationResult(result) |
| 208 | .hasErrorEntryForKeyThat(skyKey) |
| 209 | .hasExceptionThat() |
| 210 | .hasMessageThat() |
| 211 | .contains("no such package 'pkg': no good"); |
michajlo | a96b2d2 | 2020-02-26 14:54:50 -0800 | [diff] [blame] | 212 | assertContainsEvent("warning event"); |
michajlo | d05a923 | 2020-02-25 16:39:00 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Test |
michajlo | 9081022 | 2020-03-02 09:36:23 -0800 | [diff] [blame] | 216 | public void testSkyframeExecutorClearedPackagesResultsInReload() throws Exception { |
| 217 | scratch.file("pkg/BUILD", "sh_library(name='foo', srcs=['foo.sh'])"); |
| 218 | scratch.file("pkg/foo.sh"); |
| 219 | |
| 220 | invalidatePackages(); |
| 221 | |
| 222 | // Use number of times the package was validated as a proxy for number of times it was loaded. |
| 223 | AtomicInteger validationCount = new AtomicInteger(); |
| 224 | doAnswer( |
| 225 | inv -> { |
| 226 | if (inv.getArgument(0, Package.class).getName().equals("pkg")) { |
| 227 | validationCount.incrementAndGet(); |
| 228 | } |
| 229 | return null; |
| 230 | }) |
| 231 | .when(mockPackageValidator) |
| 232 | .validate(any(Package.class), any(ExtendedEventHandler.class)); |
| 233 | |
| 234 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 235 | EvaluationResult<PackageValue> result1 = |
| 236 | SkyframeExecutorTestUtils.evaluate( |
| 237 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
| 238 | assertThatEvaluationResult(result1).hasNoError(); |
| 239 | |
| 240 | skyframeExecutor.clearLoadedPackages(); |
| 241 | |
| 242 | EvaluationResult<PackageValue> result2 = |
| 243 | SkyframeExecutorTestUtils.evaluate( |
| 244 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
| 245 | assertThatEvaluationResult(result2).hasNoError(); |
| 246 | |
| 247 | assertThat(validationCount.get()).isEqualTo(2); |
| 248 | } |
| 249 | |
| 250 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 251 | public void testPropagatesFilesystemInconsistencies() throws Exception { |
| 252 | reporter.removeHandler(failFastHandler); |
| 253 | RecordingDifferencer differencer = getSkyframeExecutor().getDifferencerForTesting(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 254 | Root pkgRoot = getSkyframeExecutor().getPathEntries().get(0); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 255 | Path fooBuildFile = scratch.file("foo/BUILD"); |
| 256 | Path fooDir = fooBuildFile.getParentDirectory(); |
| 257 | |
neerajen | 89188eb | 2018-07-19 13:03:43 -0700 | [diff] [blame] | 258 | // Our custom filesystem says that fooDir is neither a file nor directory nor symlink |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 259 | FileStatus inconsistentFileStatus = |
| 260 | new FileStatus() { |
| 261 | @Override |
| 262 | public boolean isFile() { |
| 263 | return false; |
| 264 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 265 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 266 | @Override |
| 267 | public boolean isDirectory() { |
| 268 | return false; |
| 269 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 270 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 271 | @Override |
| 272 | public boolean isSymbolicLink() { |
| 273 | return false; |
| 274 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 275 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 276 | @Override |
| 277 | public boolean isSpecialFile() { |
| 278 | return false; |
| 279 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 280 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 281 | @Override |
| 282 | public long getSize() throws IOException { |
| 283 | return 0; |
| 284 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 285 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 286 | @Override |
| 287 | public long getLastModifiedTime() throws IOException { |
| 288 | return 0; |
| 289 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 290 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 291 | @Override |
| 292 | public long getLastChangeTime() throws IOException { |
| 293 | return 0; |
| 294 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 295 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 296 | @Override |
| 297 | public long getNodeId() throws IOException { |
| 298 | return 0; |
| 299 | } |
| 300 | }; |
neerajen | 89188eb | 2018-07-19 13:03:43 -0700 | [diff] [blame] | 301 | |
| 302 | fs.stubStat(fooBuildFile, inconsistentFileStatus); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 303 | RootedPath pkgRootedPath = RootedPath.toRootedPath(pkgRoot, fooDir); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 304 | SkyValue fooDirValue = FileStateValue.create(pkgRootedPath, tsgm); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 305 | differencer.inject(ImmutableMap.of(FileStateValue.key(pkgRootedPath), fooDirValue)); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 306 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 307 | String expectedMessage = |
| 308 | "according to stat, existing path /workspace/foo/BUILD is neither" |
| 309 | + " a file nor directory nor symlink."; |
| 310 | EvaluationResult<PackageValue> result = |
| 311 | SkyframeExecutorTestUtils.evaluate( |
| 312 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 313 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 314 | ErrorInfo errorInfo = result.getError(skyKey); |
| 315 | String errorMessage = errorInfo.getException().getMessage(); |
| 316 | assertThat(errorMessage).contains("Inconsistent filesystem operations"); |
| 317 | assertThat(errorMessage).contains(expectedMessage); |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 318 | assertDetailedExitCode( |
| 319 | errorInfo.getException(), PackageLoading.Code.PERSISTENT_INCONSISTENT_FILESYSTEM_ERROR); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 322 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 323 | public void testPropagatesFilesystemInconsistencies_globbing() throws Exception { |
djasper | 7faa0ef | 2019-03-28 10:00:00 -0700 | [diff] [blame] | 324 | getSkyframeExecutor().turnOffSyscallCacheForTesting(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 325 | reporter.removeHandler(failFastHandler); |
| 326 | RecordingDifferencer differencer = getSkyframeExecutor().getDifferencerForTesting(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 327 | Root pkgRoot = getSkyframeExecutor().getPathEntries().get(0); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 328 | scratch.file( |
| 329 | "foo/BUILD", |
Googler | bb0d36a | 2019-09-26 13:19:28 -0700 | [diff] [blame] | 330 | "sh_library(name = 'foo', srcs = glob(['bar/**/baz.sh']))", |
| 331 | "x = 1//0" // causes 'foo' to be marked in error |
| 332 | ); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 333 | Path bazFile = scratch.file("foo/bar/baz/baz.sh"); |
| 334 | Path bazDir = bazFile.getParentDirectory(); |
| 335 | Path barDir = bazDir.getParentDirectory(); |
| 336 | |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 337 | // Our custom filesystem says "foo/bar/baz" does not exist but it also says that "foo/bar" |
| 338 | // has a child directory "baz". |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 339 | fs.stubStat(bazDir, null); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 340 | RootedPath barDirRootedPath = RootedPath.toRootedPath(pkgRoot, barDir); |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 341 | differencer.inject( |
| 342 | ImmutableMap.of( |
| 343 | DirectoryListingStateValue.key(barDirRootedPath), |
| 344 | DirectoryListingStateValue.create( |
| 345 | ImmutableList.of(new Dirent("baz", Dirent.Type.DIRECTORY))))); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 346 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Nathan Harmata | c0f3d4e | 2016-10-17 15:49:34 +0000 | [diff] [blame] | 347 | String expectedMessage = "/workspace/foo/bar/baz is no longer an existing directory"; |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 348 | EvaluationResult<PackageValue> result = |
| 349 | SkyframeExecutorTestUtils.evaluate( |
| 350 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 351 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 352 | ErrorInfo errorInfo = result.getError(skyKey); |
| 353 | String errorMessage = errorInfo.getException().getMessage(); |
| 354 | assertThat(errorMessage).contains("Inconsistent filesystem operations"); |
| 355 | assertThat(errorMessage).contains(expectedMessage); |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 356 | assertDetailedExitCode( |
| 357 | errorInfo.getException(), PackageLoading.Code.PERSISTENT_INCONSISTENT_FILESYSTEM_ERROR); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** Regression test for unexpected exception type from PackageValue. */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 361 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 362 | public void testDiscrepancyBetweenLegacyAndSkyframePackageLoadingErrors() throws Exception { |
djasper | b109a5f | 2019-02-14 01:05:34 -0800 | [diff] [blame] | 363 | // Normally, legacy globbing and skyframe globbing share a cache for `readdir` filesystem calls. |
| 364 | // In order to exercise a situation where they observe different results for filesystem calls, |
| 365 | // we disable the cache. This might happen in a real scenario, e.g. if the cache hits a limit |
| 366 | // and evicts entries. |
| 367 | getSkyframeExecutor().turnOffSyscallCacheForTesting(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 368 | reporter.removeHandler(failFastHandler); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 369 | Path fooBuildFile = |
| 370 | scratch.file("foo/BUILD", "sh_library(name = 'foo', srcs = glob(['bar/*.sh']))"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 371 | Path fooDir = fooBuildFile.getParentDirectory(); |
| 372 | Path barDir = fooDir.getRelative("bar"); |
| 373 | scratch.file("foo/bar/baz.sh"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 374 | fs.scheduleMakeUnreadableAfterReaddir(barDir); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 375 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 376 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 377 | String expectedMessage = "Encountered error 'Directory is not readable'"; |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 378 | EvaluationResult<PackageValue> result = |
| 379 | SkyframeExecutorTestUtils.evaluate( |
| 380 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 381 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 382 | ErrorInfo errorInfo = result.getError(skyKey); |
| 383 | String errorMessage = errorInfo.getException().getMessage(); |
| 384 | assertThat(errorMessage).contains("Inconsistent filesystem operations"); |
| 385 | assertThat(errorMessage).contains(expectedMessage); |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 386 | assertDetailedExitCode( |
| 387 | errorInfo.getException(), PackageLoading.Code.TRANSIENT_INCONSISTENT_FILESYSTEM_ERROR); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 390 | @SuppressWarnings("unchecked") // Cast of srcs attribute to Iterable<Label>. |
| 391 | @Test |
| 392 | public void testGlobOrderStable() throws Exception { |
| 393 | scratch.file("foo/BUILD", "sh_library(name = 'foo', srcs = glob(['**/*.txt']))"); |
| 394 | scratch.file("foo/b.txt"); |
| 395 | scratch.file("foo/c/c.txt"); |
| 396 | preparePackageLoading(rootDirectory); |
| 397 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 398 | Package pkg = validPackageWithoutErrors(skyKey); |
Googler | a40c64a | 2020-08-11 16:39:39 -0700 | [diff] [blame] | 399 | assertThat((Iterable<Label>) pkg.getTarget("foo").getAssociatedRule().getAttr("srcs")) |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 400 | .containsExactly( |
| 401 | Label.parseAbsoluteUnchecked("//foo:b.txt"), |
| 402 | Label.parseAbsoluteUnchecked("//foo:c/c.txt")) |
| 403 | .inOrder(); |
| 404 | scratch.file("foo/d.txt"); |
| 405 | getSkyframeExecutor() |
| 406 | .invalidateFilesUnderPathForTesting( |
| 407 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 408 | ModifiedFileSet.builder().modify(PathFragment.create("foo/d.txt")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 409 | Root.fromPath(rootDirectory)); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 410 | pkg = validPackageWithoutErrors(skyKey); |
Googler | a40c64a | 2020-08-11 16:39:39 -0700 | [diff] [blame] | 411 | assertThat((Iterable<Label>) pkg.getTarget("foo").getAssociatedRule().getAttr("srcs")) |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 412 | .containsExactly( |
| 413 | Label.parseAbsoluteUnchecked("//foo:b.txt"), |
| 414 | Label.parseAbsoluteUnchecked("//foo:c/c.txt"), |
| 415 | Label.parseAbsoluteUnchecked("//foo:d.txt")) |
| 416 | .inOrder(); |
| 417 | } |
| 418 | |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 419 | @Test |
| 420 | public void testGlobOrderStableWithLegacyAndSkyframeComponents() throws Exception { |
| 421 | scratch.file("foo/BUILD", "sh_library(name = 'foo', srcs = glob(['*.txt']))"); |
| 422 | scratch.file("foo/b.txt"); |
| 423 | scratch.file("foo/a.config"); |
| 424 | preparePackageLoading(rootDirectory); |
| 425 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 426 | assertSrcs(validPackageWithoutErrors(skyKey), "foo", "//foo:b.txt"); |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 427 | scratch.overwriteFile( |
| 428 | "foo/BUILD", "sh_library(name = 'foo', srcs = glob(['*.txt', '*.config']))"); |
| 429 | getSkyframeExecutor() |
| 430 | .invalidateFilesUnderPathForTesting( |
| 431 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 432 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 433 | Root.fromPath(rootDirectory)); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 434 | assertSrcs(validPackageWithoutErrors(skyKey), "foo", "//foo:a.config", "//foo:b.txt"); |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 435 | scratch.overwriteFile( |
| 436 | "foo/BUILD", "sh_library(name = 'foo', srcs = glob(['*.txt', '*.config'])) # comment"); |
| 437 | getSkyframeExecutor() |
| 438 | .invalidateFilesUnderPathForTesting( |
| 439 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 440 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 441 | Root.fromPath(rootDirectory)); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 442 | assertSrcs(validPackageWithoutErrors(skyKey), "foo", "//foo:a.config", "//foo:b.txt"); |
Nathan Harmata | 44e1e3a | 2016-08-23 21:22:17 +0000 | [diff] [blame] | 443 | getSkyframeExecutor().resetEvaluator(); |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 444 | PackageOptions packageOptions = Options.getDefaults(PackageOptions.class); |
| 445 | packageOptions.defaultVisibility = ConstantRuleVisibility.PUBLIC; |
| 446 | packageOptions.showLoadingProgress = true; |
| 447 | packageOptions.globbingThreads = 7; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 448 | getSkyframeExecutor() |
| 449 | .preparePackageLoading( |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 450 | new PathPackageLocator( |
| 451 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 452 | ImmutableList.of(Root.fromPath(rootDirectory)), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 453 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 454 | packageOptions, |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 455 | Options.getDefaults(BuildLanguageOptions.class), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 456 | UUID.randomUUID(), |
Klaus Aehlig | 03b9cfd | 2016-09-14 13:14:39 +0000 | [diff] [blame] | 457 | ImmutableMap.<String, String>of(), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 458 | tsgm); |
juliexxia | 651797f | 2018-08-01 11:45:13 -0700 | [diff] [blame] | 459 | getSkyframeExecutor().setActionEnv(ImmutableMap.<String, String>of()); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 460 | assertSrcs(validPackageWithoutErrors(skyKey), "foo", "//foo:a.config", "//foo:b.txt"); |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Benjamin Peterson | 3b39e1e | 2020-01-31 10:21:38 -0800 | [diff] [blame] | 463 | @Test |
| 464 | public void globEscapesAt() throws Exception { |
| 465 | scratch.file("foo/BUILD", "filegroup(name = 'foo', srcs = glob(['*.txt']))"); |
| 466 | scratch.file("foo/@f.txt"); |
| 467 | preparePackageLoading(rootDirectory); |
| 468 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 469 | assertSrcs(validPackageWithoutErrors(skyKey), "foo", "//foo:@f.txt"); |
| 470 | |
| 471 | scratch.overwriteFile("foo/BUILD", "filegroup(name = 'foo', srcs = glob(['*.txt'])) # comment"); |
| 472 | getSkyframeExecutor() |
| 473 | .invalidateFilesUnderPathForTesting( |
| 474 | reporter, |
| 475 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
| 476 | Root.fromPath(rootDirectory)); |
| 477 | assertSrcs(validPackageWithoutErrors(skyKey), "foo", "//foo:@f.txt"); |
| 478 | } |
| 479 | |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 480 | /** |
| 481 | * Tests that a symlink to a file outside of the package root is handled consistently. If the |
| 482 | * default behavior of Bazel was changed from {@code |
| 483 | * ExternalFileAction#DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS} to {@code |
| 484 | * ExternalFileAction#ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS} then foo/link.sh |
| 485 | * should no longer appear in the srcs of //foo:foo. However, either way the srcs should be the |
| 486 | * same independent of the evaluation being incremental or clean. |
| 487 | */ |
| 488 | @Test |
| 489 | public void testGlobWithExternalSymlink() throws Exception { |
| 490 | scratch.file( |
| 491 | "foo/BUILD", |
| 492 | "sh_library(name = 'foo', srcs = glob(['*.sh']))", |
| 493 | "sh_library(name = 'bar', srcs = glob(['link.sh']))", |
| 494 | "sh_library(name = 'baz', srcs = glob(['subdir_link/*.txt']))"); |
| 495 | scratch.file("foo/ordinary.sh"); |
| 496 | Path externalTarget = scratch.file("../ops/target.txt"); |
| 497 | FileSystemUtils.ensureSymbolicLink(scratch.resolve("foo/link.sh"), externalTarget); |
| 498 | FileSystemUtils.ensureSymbolicLink( |
| 499 | scratch.resolve("foo/subdir_link"), externalTarget.getParentDirectory()); |
| 500 | preparePackageLoading(rootDirectory); |
| 501 | SkyKey fooKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 502 | Package fooPkg = validPackageWithoutErrors(fooKey); |
| 503 | assertSrcs(fooPkg, "foo", "//foo:link.sh", "//foo:ordinary.sh"); |
| 504 | assertSrcs(fooPkg, "bar", "//foo:link.sh"); |
| 505 | assertSrcs(fooPkg, "baz", "//foo:subdir_link/target.txt"); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 506 | scratch.overwriteFile( |
| 507 | "foo/BUILD", |
| 508 | "sh_library(name = 'foo', srcs = glob(['*.sh'])) #comment", |
| 509 | "sh_library(name = 'bar', srcs = glob(['link.sh']))", |
| 510 | "sh_library(name = 'baz', srcs = glob(['subdir_link/*.txt']))"); |
| 511 | getSkyframeExecutor() |
| 512 | .invalidateFilesUnderPathForTesting( |
| 513 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 514 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 515 | Root.fromPath(rootDirectory)); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 516 | Package fooPkg2 = validPackageWithoutErrors(fooKey); |
| 517 | assertThat(fooPkg2).isNotEqualTo(fooPkg); |
| 518 | assertSrcs(fooPkg2, "foo", "//foo:link.sh", "//foo:ordinary.sh"); |
| 519 | assertSrcs(fooPkg2, "bar", "//foo:link.sh"); |
| 520 | assertSrcs(fooPkg2, "baz", "//foo:subdir_link/target.txt"); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 521 | } |
| 522 | |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 523 | private static void assertSrcs(Package pkg, String targetName, String... expected) |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 524 | throws NoSuchTargetException { |
| 525 | List<Label> expectedLabels = new ArrayList<>(); |
| 526 | for (String item : expected) { |
| 527 | expectedLabels.add(Label.parseAbsoluteUnchecked(item)); |
| 528 | } |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 529 | assertThat(getSrcs(pkg, targetName)).containsExactlyElementsIn(expectedLabels).inOrder(); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | @SuppressWarnings("unchecked") |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 533 | private static Iterable<Label> getSrcs(Package pkg, String targetName) |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 534 | throws NoSuchTargetException { |
Googler | a40c64a | 2020-08-11 16:39:39 -0700 | [diff] [blame] | 535 | return (Iterable<Label>) pkg.getTarget(targetName).getAssociatedRule().getAttr("srcs"); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 536 | } |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 537 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 538 | @Test |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 539 | public void testOneNewElementInMultipleGlob() throws Exception { |
| 540 | scratch.file( |
| 541 | "foo/BUILD", |
| 542 | "sh_library(name = 'foo', srcs = glob(['*.sh']))", |
| 543 | "sh_library(name = 'bar', srcs = glob(['*.sh', '*.txt']))"); |
| 544 | preparePackageLoading(rootDirectory); |
| 545 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 546 | Package pkg = validPackageWithoutErrors(skyKey); |
| 547 | scratch.file("foo/irrelevant"); |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 548 | getSkyframeExecutor() |
| 549 | .invalidateFilesUnderPathForTesting( |
| 550 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 551 | ModifiedFileSet.builder().modify(PathFragment.create("foo/irrelevant")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 552 | Root.fromPath(rootDirectory)); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 553 | assertThat(validPackageWithoutErrors(skyKey)).isSameInstanceAs(pkg); |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | @Test |
| 557 | public void testNoNewElementInMultipleGlob() throws Exception { |
| 558 | scratch.file( |
| 559 | "foo/BUILD", |
| 560 | "sh_library(name = 'foo', srcs = glob(['*.sh', '*.txt']))", |
| 561 | "sh_library(name = 'bar', srcs = glob(['*.sh', '*.txt']))"); |
| 562 | preparePackageLoading(rootDirectory); |
| 563 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 564 | Package pkg = validPackageWithoutErrors(skyKey); |
| 565 | scratch.file("foo/irrelevant"); |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 566 | getSkyframeExecutor() |
| 567 | .invalidateFilesUnderPathForTesting( |
| 568 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 569 | ModifiedFileSet.builder().modify(PathFragment.create("foo/irrelevant")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 570 | Root.fromPath(rootDirectory)); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 571 | assertThat(validPackageWithoutErrors(skyKey)).isSameInstanceAs(pkg); |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | @Test |
gregce | b100b1d | 2020-05-20 10:22:17 -0700 | [diff] [blame] | 575 | public void testTransitiveStarlarkDepsStoredInPackage() throws Exception { |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 576 | scratch.file("foo/BUILD", "load('//bar:ext.bzl', 'a')"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 577 | scratch.file("bar/BUILD"); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 578 | scratch.file("bar/ext.bzl", "load('//baz:ext.bzl', 'b')", "a = b"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 579 | scratch.file("baz/BUILD"); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 580 | scratch.file("baz/ext.bzl", "b = 1"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 581 | scratch.file("qux/BUILD"); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 582 | scratch.file("qux/ext.bzl", "c = 1"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 583 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 584 | preparePackageLoading(rootDirectory); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 585 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 586 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 587 | Package pkg = validPackageWithoutErrors(skyKey); |
gregce | 7ecc2d6 | 2020-04-17 15:32:47 -0700 | [diff] [blame] | 588 | assertThat(pkg.getStarlarkFileDependencies()) |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 589 | .containsExactly( |
| 590 | Label.parseAbsolute("//bar:ext.bzl", ImmutableMap.of()), |
| 591 | Label.parseAbsolute("//baz:ext.bzl", ImmutableMap.of())); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 592 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 593 | scratch.overwriteFile("bar/ext.bzl", "load('//qux:ext.bzl', 'c')", "a = c"); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 594 | getSkyframeExecutor() |
| 595 | .invalidateFilesUnderPathForTesting( |
| 596 | reporter, |
| 597 | ModifiedFileSet.builder().modify(PathFragment.create("bar/ext.bzl")).build(), |
| 598 | Root.fromPath(rootDirectory)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 599 | |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 600 | pkg = validPackageWithoutErrors(skyKey); |
gregce | 7ecc2d6 | 2020-04-17 15:32:47 -0700 | [diff] [blame] | 601 | assertThat(pkg.getStarlarkFileDependencies()) |
dannark | 90e2b4b | 2018-06-27 13:35:04 -0700 | [diff] [blame] | 602 | .containsExactly( |
| 603 | Label.parseAbsolute("//bar:ext.bzl", ImmutableMap.of()), |
| 604 | Label.parseAbsolute("//qux:ext.bzl", ImmutableMap.of())); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 607 | @Test |
gregce | b100b1d | 2020-05-20 10:22:17 -0700 | [diff] [blame] | 608 | public void testNonExistingStarlarkExtension() throws Exception { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 609 | reporter.removeHandler(failFastHandler); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 610 | scratch.file( |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 611 | "test/starlark/BUILD", |
| 612 | "load('//test/starlark:bad_extension.bzl', 'some_symbol')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 613 | "genrule(name = gr,", |
| 614 | " outs = ['out.txt'],", |
| 615 | " cmd = 'echo hello >@')"); |
| 616 | invalidatePackages(); |
| 617 | |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 618 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//test/starlark")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 619 | EvaluationResult<PackageValue> result = |
| 620 | SkyframeExecutorTestUtils.evaluate( |
| 621 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 622 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 623 | ErrorInfo errorInfo = result.getError(skyKey); |
laurentlb | dd612a8 | 2018-10-16 19:42:48 -0700 | [diff] [blame] | 624 | String expectedMsg = |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 625 | "error loading package 'test/starlark': " |
| 626 | + "cannot load '//test/starlark:bad_extension.bzl': no such file"; |
diamondm | d6df980 | 2019-03-19 06:53:43 -0700 | [diff] [blame] | 627 | assertThat(errorInfo.getException()).hasMessageThat().isEqualTo(expectedMsg); |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 628 | assertDetailedExitCode( |
| 629 | errorInfo.getException(), PackageLoading.Code.IMPORT_STARLARK_FILE_ERROR); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 632 | @Test |
gregce | b100b1d | 2020-05-20 10:22:17 -0700 | [diff] [blame] | 633 | public void testNonExistingStarlarkExtensionFromExtension() throws Exception { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 634 | reporter.removeHandler(failFastHandler); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 635 | scratch.file( |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 636 | "test/starlark/extension.bzl", |
| 637 | "load('//test/starlark:bad_extension.bzl', 'some_symbol')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 638 | "a = 'a'"); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 639 | scratch.file( |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 640 | "test/starlark/BUILD", |
| 641 | "load('//test/starlark:extension.bzl', 'a')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 642 | "genrule(name = gr,", |
| 643 | " outs = ['out.txt'],", |
| 644 | " cmd = 'echo hello >@')"); |
| 645 | invalidatePackages(); |
| 646 | |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 647 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//test/starlark")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 648 | EvaluationResult<PackageValue> result = |
| 649 | SkyframeExecutorTestUtils.evaluate( |
| 650 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 651 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 652 | ErrorInfo errorInfo = result.getError(skyKey); |
| 653 | assertThat(errorInfo.getException()) |
diamondm | d6df980 | 2019-03-19 06:53:43 -0700 | [diff] [blame] | 654 | .hasMessageThat() |
| 655 | .isEqualTo( |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 656 | "error loading package 'test/starlark': " |
| 657 | + "in /workspace/test/starlark/extension.bzl: " |
| 658 | + "cannot load '//test/starlark:bad_extension.bzl': no such file"); |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 659 | assertDetailedExitCode( |
| 660 | errorInfo.getException(), PackageLoading.Code.IMPORT_STARLARK_FILE_ERROR); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 663 | @Test |
gregce | b100b1d | 2020-05-20 10:22:17 -0700 | [diff] [blame] | 664 | public void testSymlinkCycleWithStarlarkExtension() throws Exception { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 665 | reporter.removeHandler(failFastHandler); |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 666 | Path extensionFilePath = scratch.resolve("/workspace/test/starlark/extension.bzl"); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 667 | FileSystemUtils.ensureSymbolicLink(extensionFilePath, PathFragment.create("extension.bzl")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 668 | scratch.file( |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 669 | "test/starlark/BUILD", |
| 670 | "load('//test/starlark:extension.bzl', 'a')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 671 | "genrule(name = gr,", |
| 672 | " outs = ['out.txt'],", |
| 673 | " cmd = 'echo hello >@')"); |
| 674 | invalidatePackages(); |
| 675 | |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 676 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//test/starlark")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 677 | EvaluationResult<PackageValue> result = |
| 678 | SkyframeExecutorTestUtils.evaluate( |
| 679 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 680 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 681 | ErrorInfo errorInfo = result.getError(skyKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 682 | assertThat(errorInfo.getRootCauseOfException()).isEqualTo(skyKey); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 683 | assertThat(errorInfo.getException()) |
diamondm | d6df980 | 2019-03-19 06:53:43 -0700 | [diff] [blame] | 684 | .hasMessageThat() |
| 685 | .isEqualTo( |
gregce | 0503fee | 2020-06-11 09:22:27 -0700 | [diff] [blame] | 686 | "error loading package 'test/starlark': Encountered error while reading extension " |
| 687 | + "file 'test/starlark/extension.bzl': Symlink cycle"); |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 688 | assertDetailedExitCode( |
| 689 | errorInfo.getException(), PackageLoading.Code.IMPORT_STARLARK_FILE_ERROR); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 692 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 693 | public void testIOErrorLookingForSubpackageForLabelIsHandled() throws Exception { |
| 694 | reporter.removeHandler(failFastHandler); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 695 | scratch.file("foo/BUILD", "sh_library(name = 'foo', srcs = ['bar/baz.sh'])"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 696 | Path barBuildFile = scratch.file("foo/bar/BUILD"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 697 | fs.stubStatError(barBuildFile, new IOException("nope")); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 698 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 699 | EvaluationResult<PackageValue> result = |
| 700 | SkyframeExecutorTestUtils.evaluate( |
| 701 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 702 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 703 | assertContainsEvent("nope"); |
| 704 | } |
| 705 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 706 | @Test |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 707 | public void testLoadOK() throws Exception { |
| 708 | scratch.file("p/a.bzl", "a = 1; b = 1; d = 1"); |
| 709 | scratch.file("p/subdir/a.bzl", "c = 1; e = 1"); |
| 710 | scratch.file( |
| 711 | "p/BUILD", |
| 712 | // |
| 713 | "load(':a.bzl', 'a')", |
| 714 | "load('a.bzl', 'b')", |
| 715 | "load('subdir/a.bzl', 'c')", |
| 716 | "load('//p:a.bzl', 'd')", |
| 717 | "load('//p:subdir/a.bzl', 'e')"); |
| 718 | validPackageWithoutErrors(PackageValue.key(PackageIdentifier.parse("@//p"))); |
| 719 | } |
| 720 | |
adonovan | e0bd9d3 | 2020-09-18 12:51:15 -0700 | [diff] [blame] | 721 | // See WorkspaceFileFunctionTest for tests that exercise load('@repo...'). |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 722 | |
| 723 | @Test |
| 724 | public void testLoadBadLabel() throws Exception { |
| 725 | scratch.file("p/BUILD", "load('this\tis not a label', 'a')"); |
| 726 | reporter.removeHandler(failFastHandler); |
| 727 | SkyKey key = PackageValue.key(PackageIdentifier.parse("@//p")); |
| 728 | SkyframeExecutorTestUtils.evaluate(skyframeExecutor, key, /*keepGoing=*/ false, reporter); |
| 729 | assertContainsEvent( |
| 730 | "in load statement: invalid target name 'this<?>is not a label': target names may not" |
| 731 | + " contain non-printable characters"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 734 | @Test |
Googler | 83439e6 | 2019-09-24 12:11:30 -0700 | [diff] [blame] | 735 | public void testLoadFromExternalPackage() throws Exception { |
| 736 | scratch.file("p/BUILD", "load('//external:file.bzl', 'a')"); |
| 737 | reporter.removeHandler(failFastHandler); |
| 738 | SkyKey key = PackageValue.key(PackageIdentifier.parse("@//p")); |
| 739 | SkyframeExecutorTestUtils.evaluate(skyframeExecutor, key, /*keepGoing=*/ false, reporter); |
| 740 | assertContainsEvent("Starlark files may not be loaded from the //external package"); |
| 741 | } |
| 742 | |
| 743 | @Test |
| 744 | public void testLoadWithoutBzlSuffix() throws Exception { |
| 745 | scratch.file("p/BUILD", "load('//p:file.starlark', 'a')"); |
| 746 | reporter.removeHandler(failFastHandler); |
| 747 | SkyKey key = PackageValue.key(PackageIdentifier.parse("@//p")); |
| 748 | SkyframeExecutorTestUtils.evaluate(skyframeExecutor, key, /*keepGoing=*/ false, reporter); |
| 749 | assertContainsEvent("The label must reference a file with extension '.bzl'"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 752 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 753 | public void testBadWorkspaceFile() throws Exception { |
| 754 | Path workspacePath = scratch.overwriteFile("WORKSPACE", "junk"); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 755 | SkyKey skyKey = PackageValue.key(PackageIdentifier.createInMainRepo("external")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 756 | getSkyframeExecutor() |
| 757 | .invalidate( |
| 758 | Predicates.equalTo( |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 759 | FileStateValue.key( |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 760 | RootedPath.toRootedPath( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 761 | Root.fromPath(workspacePath.getParentDirectory()), |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 762 | PathFragment.create(workspacePath.getBaseName()))))); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 763 | |
| 764 | reporter.removeHandler(failFastHandler); |
| 765 | EvaluationResult<PackageValue> result = |
| 766 | SkyframeExecutorTestUtils.evaluate( |
| 767 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 768 | assertThat(result.hasError()).isFalse(); |
| 769 | assertThat(result.get(skyKey).getPackage().containsErrors()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 772 | // Regression test for the two ugly consequences of a bug where GlobFunction incorrectly matched |
| 773 | // dangling symlinks. |
| 774 | @Test |
| 775 | public void testIncrementalSkyframeHybridGlobbingOnDanglingSymlink() throws Exception { |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 776 | Path packageDirPath = |
| 777 | scratch.file("foo/BUILD", "exports_files(glob(['*.txt']))").getParentDirectory(); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 778 | scratch.file("foo/existing.txt"); |
| 779 | FileSystemUtils.ensureSymbolicLink(packageDirPath.getChild("dangling.txt"), "nope"); |
| 780 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 781 | preparePackageLoading(rootDirectory); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 782 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 783 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 784 | Package pkg = validPackageWithoutErrors(skyKey); |
| 785 | assertThat(pkg.containsErrors()).isFalse(); |
| 786 | assertThat(pkg.getTarget("existing.txt").getName()).isEqualTo("existing.txt"); |
| 787 | assertThrows(NoSuchTargetException.class, () -> pkg.getTarget("dangling.txt")); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 788 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 789 | scratch.overwriteFile( |
| 790 | "foo/BUILD", "exports_files(glob(['*.txt']))", "#some-irrelevant-comment"); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 791 | |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 792 | getSkyframeExecutor() |
| 793 | .invalidateFilesUnderPathForTesting( |
| 794 | reporter, |
| 795 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
| 796 | Root.fromPath(rootDirectory)); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 797 | |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 798 | Package pkg2 = validPackageWithoutErrors(skyKey); |
| 799 | assertThat(pkg2.containsErrors()).isFalse(); |
| 800 | assertThat(pkg2.getTarget("existing.txt").getName()).isEqualTo("existing.txt"); |
| 801 | assertThrows(NoSuchTargetException.class, () -> pkg2.getTarget("dangling.txt")); |
jcater | 83130f4 | 2019-04-30 14:29:28 -0700 | [diff] [blame] | 802 | // One consequence of the bug was that dangling symlinks were matched by globs evaluated by |
| 803 | // Skyframe globbing, meaning there would incorrectly be corresponding targets in packages |
| 804 | // that had skyframe cache hits during skyframe hybrid globbing. |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 805 | |
| 806 | scratch.file("foo/nope"); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 807 | getSkyframeExecutor() |
| 808 | .invalidateFilesUnderPathForTesting( |
| 809 | reporter, |
| 810 | ModifiedFileSet.builder().modify(PathFragment.create("foo/nope")).build(), |
| 811 | Root.fromPath(rootDirectory)); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 812 | |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 813 | Package newPkg = validPackageWithoutErrors(skyKey); |
| 814 | assertThat(newPkg.containsErrors()).isFalse(); |
| 815 | assertThat(newPkg.getTarget("existing.txt").getName()).isEqualTo("existing.txt"); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 816 | // Another consequence of the bug is that change pruning would incorrectly cut off changes that |
| 817 | // caused a dangling symlink potentially matched by a glob to come into existence. |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 818 | assertThat(newPkg.getTarget("dangling.txt").getName()).isEqualTo("dangling.txt"); |
| 819 | assertThat(newPkg).isNotSameInstanceAs(pkg); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 822 | // Regression test for Skyframe globbing incorrectly matching the package's directory path on |
| 823 | // 'glob(['**'], exclude_directories = 0)'. We test for this directly by triggering |
| 824 | // hybrid globbing (gives coverage for both legacy globbing and skyframe globbing). |
| 825 | @Test |
| 826 | public void testRecursiveGlobNeverMatchesPackageDirectory() throws Exception { |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 827 | scratch.file( |
| 828 | "foo/BUILD", |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 829 | "[sh_library(name = x + '-matched') for x in glob(['**'], exclude_directories = 0)]"); |
| 830 | scratch.file("foo/bar"); |
| 831 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 832 | preparePackageLoading(rootDirectory); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 833 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 834 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 835 | Package pkg = validPackageWithoutErrors(skyKey); |
| 836 | assertThat(pkg.containsErrors()).isFalse(); |
| 837 | assertThat(pkg.getTarget("bar-matched").getName()).isEqualTo("bar-matched"); |
| 838 | assertThrows(NoSuchTargetException.class, () -> pkg.getTarget("-matched")); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 839 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 840 | scratch.overwriteFile( |
| 841 | "foo/BUILD", |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 842 | "[sh_library(name = x + '-matched') for x in glob(['**'], exclude_directories = 0)]", |
| 843 | "#some-irrelevant-comment"); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 844 | getSkyframeExecutor() |
| 845 | .invalidateFilesUnderPathForTesting( |
| 846 | reporter, |
| 847 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
| 848 | Root.fromPath(rootDirectory)); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 849 | |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 850 | Package pkg2 = validPackageWithoutErrors(skyKey); |
| 851 | assertThat(pkg2.containsErrors()).isFalse(); |
| 852 | assertThat(pkg2.getTarget("bar-matched").getName()).isEqualTo("bar-matched"); |
| 853 | assertThrows(NoSuchTargetException.class, () -> pkg2.getTarget("-matched")); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 854 | } |
| 855 | |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 856 | @Test |
| 857 | public void testPackageLoadingErrorOnIOExceptionReadingBuildFile() throws Exception { |
| 858 | Path fooBuildFilePath = scratch.file("foo/BUILD"); |
| 859 | IOException exn = new IOException("nope"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 860 | fs.throwExceptionOnGetInputStream(fooBuildFilePath, exn); |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 861 | |
| 862 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 863 | EvaluationResult<PackageValue> result = |
| 864 | SkyframeExecutorTestUtils.evaluate( |
| 865 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 866 | assertThat(result.hasError()).isTrue(); |
| 867 | ErrorInfo errorInfo = result.getError(skyKey); |
| 868 | String errorMessage = errorInfo.getException().getMessage(); |
| 869 | assertThat(errorMessage).contains("nope"); |
| 870 | assertThat(errorInfo.getException()).isInstanceOf(NoSuchPackageException.class); |
nharmata | 351efff | 2018-03-30 15:04:38 -0700 | [diff] [blame] | 871 | assertThat(errorInfo.getException()).hasCauseThat().isInstanceOf(IOException.class); |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 872 | assertDetailedExitCode(errorInfo.getException(), PackageLoading.Code.BUILD_FILE_MISSING); |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 873 | } |
| 874 | |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 875 | @Test |
| 876 | public void testPackageLoadingErrorOnIOExceptionReadingBzlFile() throws Exception { |
| 877 | scratch.file("foo/BUILD", "load('//foo:bzl.bzl', 'x')"); |
| 878 | Path fooBzlFilePath = scratch.file("foo/bzl.bzl"); |
| 879 | IOException exn = new IOException("nope"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 880 | fs.throwExceptionOnGetInputStream(fooBzlFilePath, exn); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 881 | |
| 882 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 883 | EvaluationResult<PackageValue> result = |
| 884 | SkyframeExecutorTestUtils.evaluate( |
| 885 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 886 | assertThat(result.hasError()).isTrue(); |
| 887 | ErrorInfo errorInfo = result.getError(skyKey); |
| 888 | String errorMessage = errorInfo.getException().getMessage(); |
| 889 | assertThat(errorMessage).contains("nope"); |
| 890 | assertThat(errorInfo.getException()).isInstanceOf(NoSuchPackageException.class); |
nharmata | 351efff | 2018-03-30 15:04:38 -0700 | [diff] [blame] | 891 | assertThat(errorInfo.getException()).hasCauseThat().isInstanceOf(IOException.class); |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 892 | assertDetailedExitCode( |
| 893 | errorInfo.getException(), PackageLoading.Code.IMPORT_STARLARK_FILE_ERROR); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 894 | } |
| 895 | |
nharmata | 5861310 | 2018-10-11 15:10:04 -0700 | [diff] [blame] | 896 | @Test |
| 897 | public void testLabelsCrossesSubpackageBoundaries() throws Exception { |
| 898 | reporter.removeHandler(failFastHandler); |
| 899 | |
| 900 | scratch.file("pkg/BUILD", "exports_files(['sub/blah'])"); |
| 901 | scratch.file("pkg/sub/BUILD"); |
| 902 | invalidatePackages(); |
| 903 | |
| 904 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 905 | EvaluationResult<PackageValue> result = |
| 906 | SkyframeExecutorTestUtils.evaluate( |
| 907 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
nharmata | 5861310 | 2018-10-11 15:10:04 -0700 | [diff] [blame] | 908 | assertThatEvaluationResult(result).hasNoError(); |
| 909 | assertThat(result.get(skyKey).getPackage().containsErrors()).isTrue(); |
laurentlb | af489f2 | 2019-07-29 08:19:03 -0700 | [diff] [blame] | 910 | assertContainsEvent("Label '//pkg:sub/blah' is invalid because 'pkg/sub' is a subpackage"); |
nharmata | 5861310 | 2018-10-11 15:10:04 -0700 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | @Test |
| 914 | public void testSymlinkCycleEncounteredWhileHandlingLabelCrossingSubpackageBoundaries() |
| 915 | throws Exception { |
| 916 | reporter.removeHandler(failFastHandler); |
| 917 | |
| 918 | scratch.file("pkg/BUILD", "exports_files(['sub/blah'])"); |
| 919 | Path subBuildFilePath = scratch.dir("pkg/sub").getChild("BUILD"); |
| 920 | FileSystemUtils.ensureSymbolicLink(subBuildFilePath, subBuildFilePath); |
| 921 | invalidatePackages(); |
| 922 | |
| 923 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 924 | EvaluationResult<PackageValue> result = |
| 925 | SkyframeExecutorTestUtils.evaluate( |
| 926 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
nharmata | 5861310 | 2018-10-11 15:10:04 -0700 | [diff] [blame] | 927 | assertThatEvaluationResult(result).hasError(); |
| 928 | assertThatEvaluationResult(result) |
| 929 | .hasErrorEntryForKeyThat(skyKey) |
| 930 | .hasExceptionThat() |
| 931 | .isInstanceOf(BuildFileNotFoundException.class); |
| 932 | assertThatEvaluationResult(result) |
| 933 | .hasErrorEntryForKeyThat(skyKey) |
| 934 | .hasExceptionThat() |
| 935 | .hasMessageThat() |
| 936 | .contains( |
| 937 | "no such package 'pkg/sub': Symlink cycle detected while trying to find BUILD file"); |
| 938 | assertContainsEvent("circular symlinks detected"); |
| 939 | } |
| 940 | |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 941 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 942 | public void testGlobAllowEmpty_paramValueMustBeBoolean() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 943 | reporter.removeHandler(failFastHandler); |
| 944 | |
| 945 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'], allow_empty = 5)"); |
| 946 | invalidatePackages(); |
| 947 | |
| 948 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 949 | validPackage(skyKey); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 950 | |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 951 | assertContainsEvent("expected boolean for argument `allow_empty`, got `5`"); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 955 | public void testGlobAllowEmpty_functionParam() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 956 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'], allow_empty=True)"); |
| 957 | invalidatePackages(); |
| 958 | |
| 959 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 960 | Package pkg = validPackage(skyKey); |
| 961 | assertThat(pkg.containsErrors()).isFalse(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 962 | assertNoEvents(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 966 | public void testGlobAllowEmpty_starlarkOption() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 967 | preparePackageLoadingWithCustomStarklarkSemanticsOptions( |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 968 | Options.parse(BuildLanguageOptions.class, "--incompatible_disallow_empty_glob=false") |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 969 | .getOptions(), |
| 970 | rootDirectory); |
| 971 | |
| 972 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'])"); |
| 973 | invalidatePackages(); |
| 974 | |
| 975 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 976 | Package pkg = validPackage(skyKey); |
| 977 | assertThat(pkg.containsErrors()).isFalse(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 978 | assertNoEvents(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 982 | public void testGlobDisallowEmpty_functionParam_wasNonEmptyAndBecomesEmpty() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 983 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'], allow_empty=False)"); |
| 984 | scratch.file("pkg/blah.foo"); |
| 985 | invalidatePackages(); |
| 986 | |
| 987 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 988 | |
| 989 | Package pkg = validPackage(skyKey); |
| 990 | assertThat(pkg.containsErrors()).isFalse(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 991 | assertNoEvents(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 992 | |
| 993 | scratch.deleteFile("pkg/blah.foo"); |
| 994 | getSkyframeExecutor() |
| 995 | .invalidateFilesUnderPathForTesting( |
| 996 | reporter, |
| 997 | ModifiedFileSet.builder().modify(PathFragment.create("pkg/blah.foo")).build(), |
| 998 | Root.fromPath(rootDirectory)); |
| 999 | |
| 1000 | reporter.removeHandler(failFastHandler); |
| 1001 | pkg = validPackage(skyKey); |
| 1002 | assertThat(pkg.containsErrors()).isTrue(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1003 | assertContainsEvent( |
laurentlb | 7679c30 | 2020-07-17 03:16:11 -0700 | [diff] [blame] | 1004 | "glob pattern '*.foo' didn't match anything, but allow_empty is set to False (the " |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1005 | + "default value of allow_empty can be set with --incompatible_disallow_empty_glob)."); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 1009 | public void testGlobDisallowEmpty_starlarkOption_wasNonEmptyAndBecomesEmpty() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1010 | preparePackageLoadingWithCustomStarklarkSemanticsOptions( |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 1011 | Options.parse(BuildLanguageOptions.class, "--incompatible_disallow_empty_glob=true") |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1012 | .getOptions(), |
| 1013 | rootDirectory); |
| 1014 | |
| 1015 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'])"); |
| 1016 | scratch.file("pkg/blah.foo"); |
| 1017 | invalidatePackages(); |
| 1018 | |
| 1019 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 1020 | |
| 1021 | Package pkg = validPackage(skyKey); |
| 1022 | assertThat(pkg.containsErrors()).isFalse(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1023 | assertNoEvents(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1024 | |
| 1025 | scratch.deleteFile("pkg/blah.foo"); |
| 1026 | getSkyframeExecutor() |
| 1027 | .invalidateFilesUnderPathForTesting( |
| 1028 | reporter, |
| 1029 | ModifiedFileSet.builder().modify(PathFragment.create("pkg/blah.foo")).build(), |
| 1030 | Root.fromPath(rootDirectory)); |
| 1031 | |
| 1032 | reporter.removeHandler(failFastHandler); |
| 1033 | pkg = validPackage(skyKey); |
| 1034 | assertThat(pkg.containsErrors()).isTrue(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1035 | assertContainsEvent( |
laurentlb | 7679c30 | 2020-07-17 03:16:11 -0700 | [diff] [blame] | 1036 | "glob pattern '*.foo' didn't match anything, but allow_empty is set to False (the " |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1037 | + "default value of allow_empty can be set with --incompatible_disallow_empty_glob)."); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 1041 | public void testGlobDisallowEmpty_functionParam_wasEmptyAndStaysEmpty() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1042 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'], allow_empty=False)"); |
| 1043 | invalidatePackages(); |
| 1044 | |
| 1045 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 1046 | reporter.removeHandler(failFastHandler); |
| 1047 | |
| 1048 | Package pkg = validPackage(skyKey); |
| 1049 | assertThat(pkg.containsErrors()).isTrue(); |
| 1050 | String expectedEventString = |
laurentlb | 7679c30 | 2020-07-17 03:16:11 -0700 | [diff] [blame] | 1051 | "glob pattern '*.foo' didn't match anything, but allow_empty is set to False (the " |
| 1052 | + "default value of allow_empty can be set with --incompatible_disallow_empty_glob)."; |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1053 | assertContainsEvent(expectedEventString); |
| 1054 | |
| 1055 | scratch.overwriteFile("pkg/BUILD", "x = " + "glob(['*.foo'], allow_empty=False) #comment"); |
| 1056 | getSkyframeExecutor() |
| 1057 | .invalidateFilesUnderPathForTesting( |
| 1058 | reporter, |
| 1059 | ModifiedFileSet.builder().modify(PathFragment.create("pkg/BUILD")).build(), |
| 1060 | Root.fromPath(rootDirectory)); |
| 1061 | |
| 1062 | pkg = validPackage(skyKey); |
| 1063 | assertThat(pkg.containsErrors()).isTrue(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1064 | assertContainsEvent(expectedEventString); |
| 1065 | } |
| 1066 | |
| 1067 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 1068 | public void testGlobDisallowEmpty_starlarkOption_wasEmptyAndStaysEmpty() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1069 | preparePackageLoadingWithCustomStarklarkSemanticsOptions( |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 1070 | Options.parse(BuildLanguageOptions.class, "--incompatible_disallow_empty_glob=true") |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1071 | .getOptions(), |
| 1072 | rootDirectory); |
| 1073 | |
| 1074 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'])"); |
| 1075 | invalidatePackages(); |
| 1076 | |
| 1077 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 1078 | reporter.removeHandler(failFastHandler); |
| 1079 | |
| 1080 | Package pkg = validPackage(skyKey); |
| 1081 | assertThat(pkg.containsErrors()).isTrue(); |
| 1082 | String expectedEventString = |
laurentlb | 7679c30 | 2020-07-17 03:16:11 -0700 | [diff] [blame] | 1083 | "glob pattern '*.foo' didn't match anything, but allow_empty is set to False (the " |
| 1084 | + "default value of allow_empty can be set with --incompatible_disallow_empty_glob)."; |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1085 | assertContainsEvent(expectedEventString); |
| 1086 | |
| 1087 | scratch.overwriteFile("pkg/BUILD", "x = " + "glob(['*.foo']) #comment"); |
| 1088 | getSkyframeExecutor() |
| 1089 | .invalidateFilesUnderPathForTesting( |
| 1090 | reporter, |
| 1091 | ModifiedFileSet.builder().modify(PathFragment.create("pkg/BUILD")).build(), |
| 1092 | Root.fromPath(rootDirectory)); |
| 1093 | |
| 1094 | pkg = validPackage(skyKey); |
| 1095 | assertThat(pkg.containsErrors()).isTrue(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1096 | assertContainsEvent(expectedEventString); |
| 1097 | } |
| 1098 | |
| 1099 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 1100 | public void testGlobDisallowEmpty_functionParam_wasEmptyDueToExcludeAndStaysEmpty() |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1101 | throws Exception { |
| 1102 | scratch.file("pkg/BUILD", "x = glob(include=['*.foo'], exclude=['blah.*'], allow_empty=False)"); |
| 1103 | scratch.file("pkg/blah.foo"); |
| 1104 | invalidatePackages(); |
| 1105 | |
| 1106 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 1107 | reporter.removeHandler(failFastHandler); |
| 1108 | |
| 1109 | Package pkg = validPackage(skyKey); |
| 1110 | assertThat(pkg.containsErrors()).isTrue(); |
| 1111 | String expectedEventString = |
laurentlb | 7679c30 | 2020-07-17 03:16:11 -0700 | [diff] [blame] | 1112 | "all files in the glob have been excluded, but allow_empty is set to False (the " |
| 1113 | + "default value of allow_empty can be set with --incompatible_disallow_empty_glob)."; |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1114 | assertContainsEvent(expectedEventString); |
| 1115 | |
| 1116 | scratch.overwriteFile( |
| 1117 | "pkg/BUILD", |
| 1118 | "x = glob(include=['*.foo'], exclude=['blah.*'], allow_empty=False) # comment"); |
| 1119 | getSkyframeExecutor() |
| 1120 | .invalidateFilesUnderPathForTesting( |
| 1121 | reporter, |
| 1122 | ModifiedFileSet.builder().modify(PathFragment.create("pkg/BUILD")).build(), |
| 1123 | Root.fromPath(rootDirectory)); |
| 1124 | |
| 1125 | pkg = validPackage(skyKey); |
| 1126 | assertThat(pkg.containsErrors()).isTrue(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1127 | assertContainsEvent(expectedEventString); |
| 1128 | } |
| 1129 | |
| 1130 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 1131 | public void testGlobDisallowEmpty_starlarkOption_wasEmptyDueToExcludeAndStaysEmpty() |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1132 | throws Exception { |
| 1133 | preparePackageLoadingWithCustomStarklarkSemanticsOptions( |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 1134 | Options.parse(BuildLanguageOptions.class, "--incompatible_disallow_empty_glob=true") |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1135 | .getOptions(), |
| 1136 | rootDirectory); |
| 1137 | |
| 1138 | scratch.file("pkg/BUILD", "x = glob(include=['*.foo'], exclude=['blah.*'])"); |
| 1139 | scratch.file("pkg/blah.foo"); |
| 1140 | invalidatePackages(); |
| 1141 | |
| 1142 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 1143 | reporter.removeHandler(failFastHandler); |
| 1144 | |
| 1145 | Package pkg = validPackage(skyKey); |
| 1146 | assertThat(pkg.containsErrors()).isTrue(); |
| 1147 | String expectedEventString = |
laurentlb | 7679c30 | 2020-07-17 03:16:11 -0700 | [diff] [blame] | 1148 | "all files in the glob have been excluded, but allow_empty is set to False (the " |
| 1149 | + "default value of allow_empty can be set with --incompatible_disallow_empty_glob)."; |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1150 | assertContainsEvent(expectedEventString); |
| 1151 | |
| 1152 | scratch.overwriteFile("pkg/BUILD", "x = glob(include=['*.foo'], exclude=['blah.*']) # comment"); |
| 1153 | getSkyframeExecutor() |
| 1154 | .invalidateFilesUnderPathForTesting( |
| 1155 | reporter, |
| 1156 | ModifiedFileSet.builder().modify(PathFragment.create("pkg/BUILD")).build(), |
| 1157 | Root.fromPath(rootDirectory)); |
| 1158 | |
| 1159 | pkg = validPackage(skyKey); |
| 1160 | assertThat(pkg.containsErrors()).isTrue(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1161 | assertContainsEvent(expectedEventString); |
| 1162 | } |
| 1163 | |
| 1164 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 1165 | public void testGlobDisallowEmpty_functionParam_wasEmptyAndBecomesNonEmpty() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1166 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'], allow_empty=False)"); |
| 1167 | invalidatePackages(); |
| 1168 | |
| 1169 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 1170 | |
| 1171 | reporter.removeHandler(failFastHandler); |
| 1172 | Package pkg = validPackage(skyKey); |
| 1173 | assertThat(pkg.containsErrors()).isTrue(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1174 | assertContainsEvent( |
laurentlb | 7679c30 | 2020-07-17 03:16:11 -0700 | [diff] [blame] | 1175 | "glob pattern '*.foo' didn't match anything, but allow_empty is set to False (the " |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1176 | + "default value of allow_empty can be set with --incompatible_disallow_empty_glob)."); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1177 | |
| 1178 | scratch.file("pkg/blah.foo"); |
| 1179 | getSkyframeExecutor() |
| 1180 | .invalidateFilesUnderPathForTesting( |
| 1181 | reporter, |
| 1182 | ModifiedFileSet.builder().modify(PathFragment.create("pkg/blah.foo")).build(), |
| 1183 | Root.fromPath(rootDirectory)); |
| 1184 | |
| 1185 | reporter.addHandler(failFastHandler); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1186 | eventCollector.clear(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1187 | pkg = validPackage(skyKey); |
| 1188 | assertThat(pkg.containsErrors()).isFalse(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1189 | assertNoEvents(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | @Test |
jmmv | bfbd95f | 2020-08-31 13:12:38 -0700 | [diff] [blame] | 1193 | public void testGlobDisallowEmpty_starlarkOption_wasEmptyAndBecomesNonEmpty() throws Exception { |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1194 | preparePackageLoadingWithCustomStarklarkSemanticsOptions( |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 1195 | Options.parse(BuildLanguageOptions.class, "--incompatible_disallow_empty_glob=true") |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1196 | .getOptions(), |
| 1197 | rootDirectory); |
| 1198 | |
| 1199 | scratch.file("pkg/BUILD", "x = " + "glob(['*.foo'])"); |
| 1200 | invalidatePackages(); |
| 1201 | |
| 1202 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 1203 | |
| 1204 | reporter.removeHandler(failFastHandler); |
| 1205 | Package pkg = validPackage(skyKey); |
| 1206 | assertThat(pkg.containsErrors()).isTrue(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1207 | |
| 1208 | assertContainsEvent( |
laurentlb | 7679c30 | 2020-07-17 03:16:11 -0700 | [diff] [blame] | 1209 | "glob pattern '*.foo' didn't match anything, but allow_empty is set to False (the " |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1210 | + "default value of allow_empty can be set with --incompatible_disallow_empty_glob)."); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1211 | |
| 1212 | scratch.file("pkg/blah.foo"); |
| 1213 | getSkyframeExecutor() |
| 1214 | .invalidateFilesUnderPathForTesting( |
| 1215 | reporter, |
| 1216 | ModifiedFileSet.builder().modify(PathFragment.create("pkg/blah.foo")).build(), |
| 1217 | Root.fromPath(rootDirectory)); |
| 1218 | |
| 1219 | reporter.addHandler(failFastHandler); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1220 | eventCollector.clear(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1221 | pkg = validPackage(skyKey); |
| 1222 | assertThat(pkg.containsErrors()).isFalse(); |
michajlo | c17b80e | 2020-07-20 10:00:28 -0700 | [diff] [blame] | 1223 | assertNoEvents(); |
nharmata | c9fbe95 | 2019-06-13 09:39:23 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Benjamin Peterson | 723eca6 | 2019-07-22 17:24:10 -0700 | [diff] [blame] | 1226 | @Test |
adonovan | 87b4608 | 2020-07-08 15:58:04 -0700 | [diff] [blame] | 1227 | public void testPackageRecordsLoadedModules() throws Exception { |
| 1228 | scratch.file("p/BUILD", "load('a.bzl', 'a'); load(':b.bzl', 'b')"); |
| 1229 | scratch.file("p/a.bzl", "load('c.bzl', 'c'); a = c"); |
| 1230 | scratch.file("p/b.bzl", "load(':c.bzl', 'c'); b = c"); |
| 1231 | scratch.file("p/c.bzl", "c = 0"); |
| 1232 | |
| 1233 | // load p |
| 1234 | preparePackageLoading(rootDirectory); |
| 1235 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//p")); |
| 1236 | Package p = validPackageWithoutErrors(skyKey); |
| 1237 | |
| 1238 | // Keys are load strings as they appear in the source (notice ":" in one of them). |
| 1239 | Map<String, Module> pLoads = p.getLoads(); |
| 1240 | assertThat(pLoads.keySet().toString()).isEqualTo("[a.bzl, :b.bzl]"); |
| 1241 | |
| 1242 | // subgraph a |
| 1243 | Module a = pLoads.get("a.bzl"); |
| 1244 | assertThat(a.toString()).isEqualTo("<module //p:a.bzl>"); |
| 1245 | Map<String, Module> aLoads = BazelModuleContext.of(a).loads(); |
| 1246 | assertThat(aLoads.keySet().toString()).isEqualTo("[c.bzl]"); |
| 1247 | Module cViaA = aLoads.get("c.bzl"); |
| 1248 | assertThat(cViaA.toString()).isEqualTo("<module //p:c.bzl>"); |
| 1249 | |
| 1250 | // subgraph b |
| 1251 | Module b = pLoads.get(":b.bzl"); |
| 1252 | assertThat(b.toString()).isEqualTo("<module //p:b.bzl>"); |
| 1253 | Map<String, Module> bLoads = BazelModuleContext.of(b).loads(); |
| 1254 | assertThat(bLoads.keySet().toString()).isEqualTo("[:c.bzl]"); |
| 1255 | Module cViaB = bLoads.get(":c.bzl"); |
| 1256 | assertThat(cViaB).isSameInstanceAs(cViaA); |
| 1257 | |
adonovan | 3ed7ed5 | 2020-09-30 12:03:28 -0700 | [diff] [blame] | 1258 | assertThat(cViaA.getGlobal("c")).isEqualTo(StarlarkInt.of(0)); |
adonovan | 87b4608 | 2020-07-08 15:58:04 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | @Test |
Benjamin Peterson | 723eca6 | 2019-07-22 17:24:10 -0700 | [diff] [blame] | 1262 | public void veryBrokenPackagePostsDoneToProgressReceiver() throws Exception { |
| 1263 | reporter.removeHandler(failFastHandler); |
| 1264 | |
| 1265 | scratch.file("pkg/BUILD", "load('//does_not:exist.bzl', 'broken'"); |
| 1266 | SkyKey key = PackageValue.key(PackageIdentifier.parse("@//pkg")); |
| 1267 | EvaluationResult<PackageValue> result = |
| 1268 | SkyframeExecutorTestUtils.evaluate(getSkyframeExecutor(), key, false, reporter); |
| 1269 | assertThatEvaluationResult(result).hasError(); |
| 1270 | assertThat(getSkyframeExecutor().getPackageProgressReceiver().progressState()) |
| 1271 | .isEqualTo(new Pair<String, String>("1 packages loaded", "")); |
| 1272 | } |
| 1273 | |
nharmata | ce0335a | 2019-11-13 15:48:05 -0800 | [diff] [blame] | 1274 | @Test |
| 1275 | public void testLegacyGlobbingEncountersSymlinkCycleAndThrowsIOException() throws Exception { |
| 1276 | reporter.removeHandler(failFastHandler); |
| 1277 | getSkyframeExecutor().turnOffSyscallCacheForTesting(); |
| 1278 | |
| 1279 | // When a package's BUILD file and the relevant filesystem state is such that legacy globbing |
| 1280 | // will encounter an IOException due to a directory symlink cycle, |
| 1281 | Path fooBUILDPath = scratch.file("foo/BUILD", "glob(['cycle/**/foo.txt'])"); |
| 1282 | Path fooCyclePath = fooBUILDPath.getParentDirectory().getChild("cycle"); |
| 1283 | FileSystemUtils.ensureSymbolicLink(fooCyclePath, fooCyclePath); |
| 1284 | IOException ioExnFromFS = |
| 1285 | assertThrows(IOException.class, () -> fooCyclePath.statIfFound(Symlinks.FOLLOW)); |
| 1286 | // And it is indeed the case that the FileSystem throws an IOException when the cycle's Path is |
| 1287 | // stat'd (following symlinks, as legacy globbing does). |
| 1288 | assertThat(ioExnFromFS).hasMessageThat().contains("Too many levels of symbolic links"); |
| 1289 | |
| 1290 | // Then, when we evaluate the PackageValue node for the Package in keepGoing mode, |
| 1291 | SkyKey pkgKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 1292 | EvaluationResult<PackageValue> result = |
| 1293 | SkyframeExecutorTestUtils.evaluate( |
| 1294 | getSkyframeExecutor(), pkgKey, /*keepGoing=*/ true, reporter); |
| 1295 | // The result is a *non-transient* Skyframe error. |
| 1296 | assertThatEvaluationResult(result).hasErrorEntryForKeyThat(pkgKey).isNotTransient(); |
| 1297 | // And that error is a NoSuchPackageException |
| 1298 | assertThatEvaluationResult(result) |
| 1299 | .hasErrorEntryForKeyThat(pkgKey) |
| 1300 | .hasExceptionThat() |
| 1301 | .isInstanceOf(NoSuchPackageException.class); |
| 1302 | // With a useful error message, |
| 1303 | assertThatEvaluationResult(result) |
| 1304 | .hasErrorEntryForKeyThat(pkgKey) |
| 1305 | .hasExceptionThat() |
| 1306 | .hasMessageThat() |
| 1307 | .contains("Symlink cycle: /workspace/foo/cycle"); |
| 1308 | // And appropriate Skyframe root cause (N.B. since we want PackageFunction to rethrow in |
| 1309 | // situations like this, we want the PackageValue node to be its own root cause). |
| 1310 | assertThatEvaluationResult(result) |
| 1311 | .hasErrorEntryForKeyThat(pkgKey) |
| 1312 | .rootCauseOfExceptionIs(pkgKey); |
| 1313 | |
| 1314 | // Then, when we modify the BUILD file so as to force package loading, |
| 1315 | scratch.overwriteFile( |
| 1316 | "foo/BUILD", "glob(['cycle/**/foo.txt']) # dummy comment to force package loading"); |
| 1317 | // But we don't make any filesystem changes that would invalidate the GlobValues, meaning that |
| 1318 | // PackageFunction will observe cache hits from Skyframe globbing, |
| 1319 | // |
| 1320 | // And we also have our filesystem blow up if the directory symlink cycle is encountered (thus, |
| 1321 | // the absence of a crash indicates the lack of legacy globbing), |
| 1322 | fs.stubStatError( |
| 1323 | fooCyclePath, |
| 1324 | new IOException() { |
| 1325 | @Override |
| 1326 | public String getMessage() { |
| 1327 | throw new IllegalStateException("should't get here!"); |
| 1328 | } |
| 1329 | }); |
| 1330 | // And we evaluate the PackageValue node for the Package in keepGoing mode, |
| 1331 | getSkyframeExecutor() |
| 1332 | .invalidateFilesUnderPathForTesting( |
| 1333 | reporter, |
| 1334 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
| 1335 | Root.fromPath(rootDirectory)); |
| 1336 | // The results are exactly the same as before, |
| 1337 | result = |
| 1338 | SkyframeExecutorTestUtils.evaluate( |
| 1339 | getSkyframeExecutor(), pkgKey, /*keepGoing=*/ true, reporter); |
| 1340 | assertThatEvaluationResult(result).hasErrorEntryForKeyThat(pkgKey).isNotTransient(); |
| 1341 | assertThatEvaluationResult(result) |
| 1342 | .hasErrorEntryForKeyThat(pkgKey) |
| 1343 | .hasExceptionThat() |
| 1344 | .isInstanceOf(NoSuchPackageException.class); |
| 1345 | assertThatEvaluationResult(result) |
| 1346 | .hasErrorEntryForKeyThat(pkgKey) |
| 1347 | .hasExceptionThat() |
| 1348 | .hasMessageThat() |
| 1349 | .contains("Symlink cycle: /workspace/foo/cycle"); |
| 1350 | assertThatEvaluationResult(result) |
| 1351 | .hasErrorEntryForKeyThat(pkgKey) |
| 1352 | .rootCauseOfExceptionIs(pkgKey); |
| 1353 | // Thus showing that clean and incremental package loading have the same semantics in the |
| 1354 | // presence of a symlink cycle encountered during glob evaluation. |
| 1355 | } |
| 1356 | |
Googler | 74178a5 | 2020-06-29 17:42:47 -0700 | [diff] [blame] | 1357 | private static void assertDetailedExitCode( |
| 1358 | Exception exception, PackageLoading.Code expectedPackageLoadingCode) { |
| 1359 | assertThat(exception).isInstanceOf(DetailedException.class); |
| 1360 | DetailedExitCode detailedExitCode = ((DetailedException) exception).getDetailedExitCode(); |
| 1361 | assertThat(detailedExitCode.getExitCode()).isEqualTo(ExitCode.BUILD_FAILURE); |
| 1362 | assertThat(detailedExitCode.getFailureDetail().getPackageLoading().getCode()) |
| 1363 | .isEqualTo(expectedPackageLoadingCode); |
| 1364 | } |
| 1365 | |
brandjon | 38dafdd | 2020-08-06 09:49:13 -0700 | [diff] [blame] | 1366 | /** |
| 1367 | * Tests of the prelude file functionality. |
| 1368 | * |
| 1369 | * <p>This is in a separate BuildViewTestCase because we override the prelude label for the test. |
| 1370 | * (The prelude label is configured differently between Bazel and Blaze.) |
| 1371 | */ |
| 1372 | @RunWith(JUnit4.class) |
| 1373 | public static class PreludeTest extends BuildViewTestCase { |
| 1374 | |
| 1375 | private final CustomInMemoryFs fs = new CustomInMemoryFs(new ManualClock()); |
| 1376 | |
| 1377 | @Override |
| 1378 | protected FileSystem createFileSystem() { |
| 1379 | return fs; |
| 1380 | } |
| 1381 | |
| 1382 | @Override |
| 1383 | protected ConfiguredRuleClassProvider createRuleClassProvider() { |
| 1384 | ConfiguredRuleClassProvider.Builder builder = new ConfiguredRuleClassProvider.Builder(); |
| 1385 | // addStandardRules() may call setPrelude(), so do it first. |
| 1386 | TestRuleClassProvider.addStandardRules(builder); |
| 1387 | builder.setPrelude("//tools/build_rules:test_prelude"); |
| 1388 | return builder.build(); |
| 1389 | } |
| 1390 | |
| 1391 | @Test |
| 1392 | public void testPreludeDefinedSymbolIsUsable() throws Exception { |
| 1393 | scratch.file("tools/build_rules/BUILD"); |
| 1394 | scratch.file( |
| 1395 | "tools/build_rules/test_prelude", // |
| 1396 | "foo = 'FOO'"); |
| 1397 | scratch.file( |
| 1398 | "pkg/BUILD", // |
| 1399 | "print(foo)"); |
| 1400 | |
| 1401 | getConfiguredTarget("//pkg:BUILD"); |
| 1402 | assertContainsEvent("FOO"); |
| 1403 | } |
| 1404 | |
| 1405 | @Test |
| 1406 | public void testPreludeAutomaticallyReexportsLoadedSymbols() throws Exception { |
| 1407 | scratch.file("tools/build_rules/BUILD"); |
| 1408 | scratch.file( |
| 1409 | "tools/build_rules/test_prelude", // |
| 1410 | "load('//util:common.bzl', 'foo')"); |
| 1411 | scratch.file("util/BUILD"); |
| 1412 | scratch.file( |
| 1413 | "util/common.bzl", // |
| 1414 | "foo = 'FOO'"); |
| 1415 | scratch.file( |
| 1416 | "pkg/BUILD", // |
| 1417 | "print(foo)"); |
| 1418 | |
| 1419 | getConfiguredTarget("//pkg:BUILD"); |
| 1420 | assertContainsEvent("FOO"); |
| 1421 | } |
| 1422 | |
| 1423 | // TODO(brandjon): Invert this test once the prelude is a module instead of a syntactic |
| 1424 | // mutation on BUILD files. |
| 1425 | @Test |
| 1426 | public void testPreludeCanExportUnderscoreSymbols() throws Exception { |
| 1427 | scratch.file("tools/build_rules/BUILD"); |
| 1428 | scratch.file( |
| 1429 | "tools/build_rules/test_prelude", // |
| 1430 | "_foo = 'FOO'"); |
| 1431 | scratch.file( |
| 1432 | "pkg/BUILD", // |
| 1433 | "print(_foo)"); |
| 1434 | |
| 1435 | getConfiguredTarget("//pkg:BUILD"); |
| 1436 | assertContainsEvent("FOO"); |
| 1437 | } |
| 1438 | |
| 1439 | @Test |
| 1440 | public void testPreludeCanShadowPredeclareds() throws Exception { |
| 1441 | scratch.file("tools/build_rules/BUILD"); |
| 1442 | scratch.file( |
| 1443 | "tools/build_rules/test_prelude", // |
| 1444 | "cc_library = 'FOO'"); |
| 1445 | scratch.file( |
| 1446 | "pkg/BUILD", // |
| 1447 | "print(cc_library)"); |
| 1448 | |
| 1449 | getConfiguredTarget("//pkg:BUILD"); |
| 1450 | assertContainsEvent("FOO"); |
| 1451 | } |
| 1452 | |
brandjon | 38dafdd | 2020-08-06 09:49:13 -0700 | [diff] [blame] | 1453 | @Test |
brandjon | 2a73a73 | 2020-08-08 06:41:14 -0700 | [diff] [blame] | 1454 | public void testPreludeSymbolCannotBeMutated() throws Exception { |
brandjon | 38dafdd | 2020-08-06 09:49:13 -0700 | [diff] [blame] | 1455 | scratch.file("tools/build_rules/BUILD"); |
| 1456 | scratch.file( |
| 1457 | "tools/build_rules/test_prelude", // |
| 1458 | "foo = ['FOO']"); |
| 1459 | scratch.file( |
| 1460 | "pkg/BUILD", // |
brandjon | 2a73a73 | 2020-08-08 06:41:14 -0700 | [diff] [blame] | 1461 | "foo.append('BAR')"); |
brandjon | 38dafdd | 2020-08-06 09:49:13 -0700 | [diff] [blame] | 1462 | |
| 1463 | reporter.removeHandler(failFastHandler); |
| 1464 | getConfiguredTarget("//pkg:BUILD"); |
brandjon | 2a73a73 | 2020-08-08 06:41:14 -0700 | [diff] [blame] | 1465 | assertContainsEvent("trying to mutate a frozen list value"); |
| 1466 | } |
| 1467 | |
| 1468 | @Test |
| 1469 | public void testPreludeCanAccessBzlDialectFeatures() throws Exception { |
| 1470 | scratch.file("tools/build_rules/BUILD"); |
| 1471 | // Test both bzl symbols and syntax (e.g. function defs). |
| 1472 | scratch.file( |
| 1473 | "tools/build_rules/test_prelude", // |
| 1474 | "def foo():", |
| 1475 | " return native.glob"); |
| 1476 | scratch.file( |
| 1477 | "pkg/BUILD", // |
| 1478 | "print(foo())"); |
| 1479 | |
| 1480 | getConfiguredTarget("//pkg:BUILD"); |
| 1481 | // Prelude can access native.glob (though only a BUILD thread can call it). |
adonovan | 800117e | 2020-09-18 10:30:06 -0700 | [diff] [blame] | 1482 | assertContainsEvent("<built-in method glob of native value>"); |
brandjon | 38dafdd | 2020-08-06 09:49:13 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | @Test |
| 1486 | public void testPreludeNeedNotBePresent() throws Exception { |
| 1487 | scratch.file( |
| 1488 | "pkg/BUILD", // |
| 1489 | "print('FOO')"); |
| 1490 | |
| 1491 | getConfiguredTarget("//pkg:BUILD"); |
| 1492 | assertContainsEvent("FOO"); |
| 1493 | } |
| 1494 | |
| 1495 | @Test |
brandjon | 2a73a73 | 2020-08-08 06:41:14 -0700 | [diff] [blame] | 1496 | public void testPreludeNeedNotBePresent_evenWhenPackageIs() throws Exception { |
| 1497 | scratch.file("tools/build_rules/BUILD"); |
| 1498 | scratch.file( |
| 1499 | "pkg/BUILD", // |
| 1500 | "print('FOO')"); |
| 1501 | |
| 1502 | getConfiguredTarget("//pkg:BUILD"); |
| 1503 | assertContainsEvent("FOO"); |
| 1504 | } |
| 1505 | |
| 1506 | @Test |
brandjon | 38dafdd | 2020-08-06 09:49:13 -0700 | [diff] [blame] | 1507 | public void testPreludeFileNotRecognizedWithoutPackage() throws Exception { |
| 1508 | scratch.file( |
| 1509 | "tools/build_rules/test_prelude", // |
| 1510 | "foo = 'FOO'"); |
| 1511 | scratch.file( |
| 1512 | "pkg/BUILD", // |
| 1513 | "print(foo)"); |
| 1514 | |
| 1515 | // The prelude file is not found without a corresponding package to contain it. BUILD files |
| 1516 | // get processed as if no prelude file is present. |
| 1517 | reporter.removeHandler(failFastHandler); |
| 1518 | getConfiguredTarget("//pkg:BUILD"); |
| 1519 | assertContainsEvent("name 'foo' is not defined"); |
| 1520 | } |
| 1521 | |
| 1522 | @Test |
| 1523 | public void testPreludeFailsWhenErrorInPreludeFile() throws Exception { |
| 1524 | scratch.file("tools/build_rules/BUILD"); |
| 1525 | scratch.file( |
| 1526 | "tools/build_rules/test_prelude", // |
| 1527 | "1//0", // <-- dynamic error |
| 1528 | "foo = 'FOO'"); |
| 1529 | scratch.file( |
| 1530 | "pkg/BUILD", // |
| 1531 | "print(foo)"); |
| 1532 | |
| 1533 | reporter.removeHandler(failFastHandler); |
| 1534 | getConfiguredTarget("//pkg:BUILD"); |
| 1535 | assertContainsEvent( |
| 1536 | "File \"/workspace/tools/build_rules/test_prelude\", line 1, column 2, in <toplevel>"); |
| 1537 | assertContainsEvent("Error: integer division by zero"); |
| 1538 | } |
| 1539 | |
| 1540 | @Test |
| 1541 | public void testPreludeWorksEvenWhenPreludePackageInError() throws Exception { |
| 1542 | scratch.file( |
| 1543 | "tools/build_rules/BUILD", // |
| 1544 | "1//0"); // <-- dynamic error |
| 1545 | scratch.file( |
| 1546 | "tools/build_rules/test_prelude", // |
| 1547 | "foo = 'FOO'"); |
| 1548 | scratch.file( |
| 1549 | "pkg/BUILD", // |
| 1550 | "print(foo)"); |
| 1551 | |
| 1552 | // Succeeds because prelude loading is only dependent on the prelude package's existence, not |
| 1553 | // its evaluation. |
| 1554 | getConfiguredTarget("//pkg:BUILD"); |
| 1555 | assertContainsEvent("FOO"); |
| 1556 | } |
| 1557 | |
| 1558 | // Another hypothetical test case we could try: Confirm that it's possible to explicitly load |
| 1559 | // the prelude file as a regular .bzl. We don't bother testing this use case because, aside from |
| 1560 | // being arguably pathological, it is currently impossible in practice: The prelude label |
| 1561 | // doesn't end with ".bzl" and isn't configurable by the user. We also want to eliminate the |
| 1562 | // prelude, so there's no intention of adding such a feature. |
| 1563 | |
| 1564 | // Another possible test case: Verify how prelude applies to WORKSPACE files. |
| 1565 | } |
| 1566 | |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1567 | private static class CustomInMemoryFs extends InMemoryFileSystem { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1568 | private abstract static class FileStatusOrException { |
| 1569 | abstract FileStatus get() throws IOException; |
| 1570 | |
| 1571 | private static class ExceptionImpl extends FileStatusOrException { |
| 1572 | private final IOException exn; |
| 1573 | |
| 1574 | private ExceptionImpl(IOException exn) { |
| 1575 | this.exn = exn; |
| 1576 | } |
| 1577 | |
| 1578 | @Override |
| 1579 | FileStatus get() throws IOException { |
| 1580 | throw exn; |
| 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | private static class FileStatusImpl extends FileStatusOrException { |
| 1585 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 1586 | @Nullable private final FileStatus fileStatus; |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1587 | |
laurentlb | c0bd210 | 2018-10-17 07:05:25 -0700 | [diff] [blame] | 1588 | private FileStatusImpl(@Nullable FileStatus fileStatus) { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1589 | this.fileStatus = fileStatus; |
| 1590 | } |
| 1591 | |
| 1592 | @Override |
| 1593 | @Nullable |
| 1594 | FileStatus get() { |
| 1595 | return fileStatus; |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1600 | private final Map<Path, FileStatusOrException> stubbedStats = Maps.newHashMap(); |
| 1601 | private final Set<Path> makeUnreadableAfterReaddir = Sets.newHashSet(); |
| 1602 | private final Map<Path, IOException> pathsToErrorOnGetInputStream = Maps.newHashMap(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1603 | |
| 1604 | public CustomInMemoryFs(ManualClock manualClock) { |
janakr | 97c0bd1 | 2020-09-08 13:19:03 -0700 | [diff] [blame] | 1605 | super(manualClock, DigestHashFunction.SHA256); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1608 | public void stubStat(Path path, @Nullable FileStatus stubbedResult) { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1609 | stubbedStats.put(path, new FileStatusOrException.FileStatusImpl(stubbedResult)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1612 | public void stubStatError(Path path, IOException stubbedResult) { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1613 | stubbedStats.put(path, new FileStatusOrException.ExceptionImpl(stubbedResult)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | @Override |
felly | a205ed8 | 2018-09-10 11:52:34 -0700 | [diff] [blame] | 1617 | public FileStatus statIfFound(Path path, boolean followSymlinks) throws IOException { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1618 | if (stubbedStats.containsKey(path)) { |
| 1619 | return stubbedStats.get(path).get(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1620 | } |
felly | a205ed8 | 2018-09-10 11:52:34 -0700 | [diff] [blame] | 1621 | return super.statIfFound(path, followSymlinks); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1624 | public void scheduleMakeUnreadableAfterReaddir(Path path) { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1625 | makeUnreadableAfterReaddir.add(path); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1629 | public Collection<Dirent> readdir(Path path, boolean followSymlinks) throws IOException { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1630 | Collection<Dirent> result = super.readdir(path, followSymlinks); |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 1631 | if (makeUnreadableAfterReaddir.contains(path)) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1632 | path.setReadable(false); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1633 | } |
| 1634 | return result; |
| 1635 | } |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 1636 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1637 | public void throwExceptionOnGetInputStream(Path path, IOException exn) { |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 1638 | pathsToErrorOnGetInputStream.put(path, exn); |
| 1639 | } |
| 1640 | |
| 1641 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 1642 | protected InputStream getInputStream(Path path) throws IOException { |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 1643 | IOException exnToThrow = pathsToErrorOnGetInputStream.get(path); |
| 1644 | if (exnToThrow != null) { |
| 1645 | throw exnToThrow; |
| 1646 | } |
| 1647 | return super.getInputStream(path); |
| 1648 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1649 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 1650 | } |