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