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