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; |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 18 | import static org.junit.Assert.fail; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 19 | |
| 20 | import com.google.common.base.Predicates; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableMap; |
| 23 | import com.google.common.collect.Maps; |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.util.BuildViewTestCase; |
philwo | 3bcb9f6 | 2017-09-06 12:52:21 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.clock.BlazeClock; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.cmdline.Label; |
| 28 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 29 | import com.google.devtools.build.lib.packages.ConstantRuleVisibility; |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 30 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
brandjon | 674ab86 | 2017-10-06 23:17:33 +0200 | [diff] [blame] | 32 | import com.google.devtools.build.lib.packages.SkylarkSemanticsOptions; |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.pkgcache.PackageCacheOptions; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
| 35 | import com.google.devtools.build.lib.skyframe.util.SkyframeExecutorTestUtils; |
| 36 | import com.google.devtools.build.lib.testutil.ManualClock; |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.vfs.Dirent; |
| 39 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 40 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 41 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 42 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
| 43 | import com.google.devtools.build.lib.vfs.Path; |
| 44 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 45 | import com.google.devtools.build.lib.vfs.Root; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 47 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 48 | import com.google.devtools.build.skyframe.ErrorInfo; |
| 49 | import com.google.devtools.build.skyframe.EvaluationResult; |
| 50 | import com.google.devtools.build.skyframe.RecordingDifferencer; |
| 51 | import com.google.devtools.build.skyframe.SkyKey; |
| 52 | import com.google.devtools.build.skyframe.SkyValue; |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 53 | import com.google.devtools.common.options.Options; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 54 | import java.io.IOException; |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 55 | import java.io.InputStream; |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 56 | import java.util.ArrayList; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 57 | import java.util.Arrays; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 58 | import java.util.Collection; |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 59 | import java.util.List; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 60 | import java.util.Map; |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 61 | import java.util.Set; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 62 | import java.util.UUID; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 63 | import javax.annotation.Nullable; |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 64 | import org.junit.Test; |
| 65 | import org.junit.runner.RunWith; |
| 66 | import org.junit.runners.JUnit4; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Unit tests of specific functionality of PackageFunction. Note that it's already tested |
| 70 | * indirectly in several other places. |
| 71 | */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 72 | @RunWith(JUnit4.class) |
Florian Weikert | cca703a | 2015-12-07 09:56:38 +0000 | [diff] [blame] | 73 | public class PackageFunctionTest extends BuildViewTestCase { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 74 | |
| 75 | private CustomInMemoryFs fs = new CustomInMemoryFs(new ManualClock()); |
| 76 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 77 | private void preparePackageLoading(Path... roots) { |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 78 | PackageCacheOptions packageCacheOptions = Options.getDefaults(PackageCacheOptions.class); |
| 79 | packageCacheOptions.defaultVisibility = ConstantRuleVisibility.PUBLIC; |
| 80 | packageCacheOptions.showLoadingProgress = true; |
| 81 | packageCacheOptions.globbingThreads = 7; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 82 | getSkyframeExecutor() |
| 83 | .preparePackageLoading( |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 84 | new PathPackageLocator( |
| 85 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 86 | Arrays.stream(roots).map(Root::fromPath).collect(ImmutableList.toImmutableList()), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 87 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 88 | packageCacheOptions, |
brandjon | 7dc3416 | 2017-04-27 18:34:33 +0200 | [diff] [blame] | 89 | Options.getDefaults(SkylarkSemanticsOptions.class), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 90 | "", |
| 91 | UUID.randomUUID(), |
| 92 | ImmutableMap.<String, String>of(), |
Damien Martin-Guillerez | 777f3af | 2017-02-08 17:22:02 +0000 | [diff] [blame] | 93 | ImmutableMap.<String, String>of(), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 94 | new TimestampGranularityMonitor(BlazeClock.instance())); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 97 | @Override |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 98 | protected FileSystem createFileSystem() { |
| 99 | return fs; |
| 100 | } |
| 101 | |
| 102 | private PackageValue validPackage(SkyKey skyKey) throws InterruptedException { |
| 103 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 104 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
| 105 | if (result.hasError()) { |
| 106 | fail(result.getError(skyKey).getException().getMessage()); |
| 107 | } |
| 108 | PackageValue value = result.get(skyKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 109 | assertThat(value.getPackage().containsErrors()).isFalse(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 110 | return value; |
| 111 | } |
| 112 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 113 | @Test |
John Cater | 9469591 | 2016-08-03 12:09:39 +0000 | [diff] [blame] | 114 | public void testValidPackage() throws Exception { |
| 115 | scratch.file("pkg/BUILD"); |
| 116 | validPackage(PackageValue.key(PackageIdentifier.parse("@//pkg"))); |
| 117 | } |
| 118 | |
| 119 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 120 | public void testPropagatesFilesystemInconsistencies() throws Exception { |
| 121 | reporter.removeHandler(failFastHandler); |
| 122 | RecordingDifferencer differencer = getSkyframeExecutor().getDifferencerForTesting(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 123 | Root pkgRoot = getSkyframeExecutor().getPathEntries().get(0); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 124 | Path fooBuildFile = scratch.file("foo/BUILD"); |
| 125 | Path fooDir = fooBuildFile.getParentDirectory(); |
| 126 | |
| 127 | // Our custom filesystem says "foo/BUILD" exists but its parent "foo" is a file. |
| 128 | FileStatus inconsistentParentFileStatus = new FileStatus() { |
| 129 | @Override |
| 130 | public boolean isFile() { |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public boolean isDirectory() { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public boolean isSymbolicLink() { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean isSpecialFile() { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public long getSize() throws IOException { |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public long getLastModifiedTime() throws IOException { |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public long getLastChangeTime() throws IOException { |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public long getNodeId() throws IOException { |
| 166 | return 0; |
| 167 | } |
| 168 | }; |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 169 | fs.stubStat(fooDir, inconsistentParentFileStatus); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 170 | RootedPath pkgRootedPath = RootedPath.toRootedPath(pkgRoot, fooDir); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 171 | SkyValue fooDirValue = FileStateValue.create(pkgRootedPath, tsgm); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 172 | differencer.inject(ImmutableMap.of(FileStateValue.key(pkgRootedPath), fooDirValue)); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 173 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 174 | String expectedMessage = "/workspace/foo/BUILD exists but its parent path /workspace/foo isn't " |
| 175 | + "an existing directory"; |
| 176 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 177 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 178 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 179 | ErrorInfo errorInfo = result.getError(skyKey); |
| 180 | String errorMessage = errorInfo.getException().getMessage(); |
| 181 | assertThat(errorMessage).contains("Inconsistent filesystem operations"); |
| 182 | assertThat(errorMessage).contains(expectedMessage); |
| 183 | } |
| 184 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 185 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 186 | public void testPropagatesFilesystemInconsistencies_Globbing() throws Exception { |
| 187 | reporter.removeHandler(failFastHandler); |
| 188 | RecordingDifferencer differencer = getSkyframeExecutor().getDifferencerForTesting(); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 189 | Root pkgRoot = getSkyframeExecutor().getPathEntries().get(0); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 190 | scratch.file("foo/BUILD", |
| 191 | "subinclude('//a:a')", |
| 192 | "sh_library(name = 'foo', srcs = glob(['bar/**/baz.sh']))"); |
| 193 | scratch.file("a/BUILD"); |
| 194 | scratch.file("a/a"); |
| 195 | Path bazFile = scratch.file("foo/bar/baz/baz.sh"); |
| 196 | Path bazDir = bazFile.getParentDirectory(); |
| 197 | Path barDir = bazDir.getParentDirectory(); |
| 198 | |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 199 | // Our custom filesystem says "foo/bar/baz" does not exist but it also says that "foo/bar" |
| 200 | // has a child directory "baz". |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 201 | fs.stubStat(bazDir, null); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 202 | RootedPath barDirRootedPath = RootedPath.toRootedPath(pkgRoot, barDir); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 203 | FileStateValue barDirFileStateValue = FileStateValue.create(barDirRootedPath, tsgm); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 204 | FileValue barDirFileValue = FileValue.value(barDirRootedPath, barDirFileStateValue, |
| 205 | barDirRootedPath, barDirFileStateValue); |
| 206 | DirectoryListingValue barDirListing = DirectoryListingValue.value(barDirRootedPath, |
| 207 | barDirFileValue, DirectoryListingStateValue.create(ImmutableList.of( |
| 208 | new Dirent("baz", Dirent.Type.DIRECTORY)))); |
| 209 | differencer.inject(ImmutableMap.of(DirectoryListingValue.key(barDirRootedPath), barDirListing)); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 210 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Nathan Harmata | c0f3d4e | 2016-10-17 15:49:34 +0000 | [diff] [blame] | 211 | String expectedMessage = "/workspace/foo/bar/baz is no longer an existing directory"; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 212 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 213 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 214 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 215 | ErrorInfo errorInfo = result.getError(skyKey); |
| 216 | String errorMessage = errorInfo.getException().getMessage(); |
| 217 | assertThat(errorMessage).contains("Inconsistent filesystem operations"); |
| 218 | assertThat(errorMessage).contains(expectedMessage); |
| 219 | } |
| 220 | |
| 221 | /** Regression test for unexpected exception type from PackageValue. */ |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 222 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 223 | public void testDiscrepancyBetweenLegacyAndSkyframePackageLoadingErrors() throws Exception { |
| 224 | reporter.removeHandler(failFastHandler); |
| 225 | Path fooBuildFile = scratch.file("foo/BUILD", |
| 226 | "sh_library(name = 'foo', srcs = glob(['bar/*.sh']))"); |
| 227 | Path fooDir = fooBuildFile.getParentDirectory(); |
| 228 | Path barDir = fooDir.getRelative("bar"); |
| 229 | scratch.file("foo/bar/baz.sh"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 230 | fs.scheduleMakeUnreadableAfterReaddir(barDir); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 231 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 232 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 233 | String expectedMessage = "Encountered error 'Directory is not readable'"; |
| 234 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 235 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 236 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 237 | ErrorInfo errorInfo = result.getError(skyKey); |
| 238 | String errorMessage = errorInfo.getException().getMessage(); |
| 239 | assertThat(errorMessage).contains("Inconsistent filesystem operations"); |
| 240 | assertThat(errorMessage).contains(expectedMessage); |
| 241 | } |
| 242 | |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 243 | @SuppressWarnings("unchecked") // Cast of srcs attribute to Iterable<Label>. |
| 244 | @Test |
| 245 | public void testGlobOrderStable() throws Exception { |
| 246 | scratch.file("foo/BUILD", "sh_library(name = 'foo', srcs = glob(['**/*.txt']))"); |
| 247 | scratch.file("foo/b.txt"); |
| 248 | scratch.file("foo/c/c.txt"); |
| 249 | preparePackageLoading(rootDirectory); |
| 250 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 251 | PackageValue value = validPackage(skyKey); |
| 252 | assertThat( |
| 253 | (Iterable<Label>) |
| 254 | value |
| 255 | .getPackage() |
| 256 | .getTarget("foo") |
| 257 | .getAssociatedRule() |
| 258 | .getAttributeContainer() |
| 259 | .getAttr("srcs")) |
| 260 | .containsExactly( |
| 261 | Label.parseAbsoluteUnchecked("//foo:b.txt"), |
| 262 | Label.parseAbsoluteUnchecked("//foo:c/c.txt")) |
| 263 | .inOrder(); |
| 264 | scratch.file("foo/d.txt"); |
| 265 | getSkyframeExecutor() |
| 266 | .invalidateFilesUnderPathForTesting( |
| 267 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 268 | ModifiedFileSet.builder().modify(PathFragment.create("foo/d.txt")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 269 | Root.fromPath(rootDirectory)); |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 270 | value = validPackage(skyKey); |
| 271 | assertThat( |
| 272 | (Iterable<Label>) |
| 273 | value |
| 274 | .getPackage() |
| 275 | .getTarget("foo") |
| 276 | .getAssociatedRule() |
| 277 | .getAttributeContainer() |
| 278 | .getAttr("srcs")) |
| 279 | .containsExactly( |
| 280 | Label.parseAbsoluteUnchecked("//foo:b.txt"), |
| 281 | Label.parseAbsoluteUnchecked("//foo:c/c.txt"), |
| 282 | Label.parseAbsoluteUnchecked("//foo:d.txt")) |
| 283 | .inOrder(); |
| 284 | } |
| 285 | |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 286 | @Test |
| 287 | public void testGlobOrderStableWithLegacyAndSkyframeComponents() throws Exception { |
| 288 | scratch.file("foo/BUILD", "sh_library(name = 'foo', srcs = glob(['*.txt']))"); |
| 289 | scratch.file("foo/b.txt"); |
| 290 | scratch.file("foo/a.config"); |
| 291 | preparePackageLoading(rootDirectory); |
| 292 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 293 | assertSrcs(validPackage(skyKey), "foo", "//foo:b.txt"); |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 294 | scratch.overwriteFile( |
| 295 | "foo/BUILD", "sh_library(name = 'foo', srcs = glob(['*.txt', '*.config']))"); |
| 296 | getSkyframeExecutor() |
| 297 | .invalidateFilesUnderPathForTesting( |
| 298 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 299 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 300 | Root.fromPath(rootDirectory)); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 301 | assertSrcs(validPackage(skyKey), "foo", "//foo:a.config", "//foo:b.txt"); |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 302 | scratch.overwriteFile( |
| 303 | "foo/BUILD", "sh_library(name = 'foo', srcs = glob(['*.txt', '*.config'])) # comment"); |
| 304 | getSkyframeExecutor() |
| 305 | .invalidateFilesUnderPathForTesting( |
| 306 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 307 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 308 | Root.fromPath(rootDirectory)); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 309 | assertSrcs(validPackage(skyKey), "foo", "//foo:a.config", "//foo:b.txt"); |
Nathan Harmata | 44e1e3a | 2016-08-23 21:22:17 +0000 | [diff] [blame] | 310 | getSkyframeExecutor().resetEvaluator(); |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 311 | PackageCacheOptions packageCacheOptions = Options.getDefaults(PackageCacheOptions.class); |
| 312 | packageCacheOptions.defaultVisibility = ConstantRuleVisibility.PUBLIC; |
| 313 | packageCacheOptions.showLoadingProgress = true; |
| 314 | packageCacheOptions.globbingThreads = 7; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 315 | getSkyframeExecutor() |
| 316 | .preparePackageLoading( |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 317 | new PathPackageLocator( |
| 318 | outputBase, |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 319 | ImmutableList.of(Root.fromPath(rootDirectory)), |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 320 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 321 | packageCacheOptions, |
brandjon | 7dc3416 | 2017-04-27 18:34:33 +0200 | [diff] [blame] | 322 | Options.getDefaults(SkylarkSemanticsOptions.class), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 323 | "", |
| 324 | UUID.randomUUID(), |
Klaus Aehlig | 03b9cfd | 2016-09-14 13:14:39 +0000 | [diff] [blame] | 325 | ImmutableMap.<String, String>of(), |
Damien Martin-Guillerez | 777f3af | 2017-02-08 17:22:02 +0000 | [diff] [blame] | 326 | ImmutableMap.<String, String>of(), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 327 | tsgm); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 328 | assertSrcs(validPackage(skyKey), "foo", "//foo:a.config", "//foo:b.txt"); |
Janak Ramakrishnan | 063b488 | 2016-07-18 20:33:28 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 331 | /** |
| 332 | * Tests that a symlink to a file outside of the package root is handled consistently. If the |
| 333 | * default behavior of Bazel was changed from {@code |
| 334 | * ExternalFileAction#DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS} to {@code |
| 335 | * ExternalFileAction#ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS} then foo/link.sh |
| 336 | * should no longer appear in the srcs of //foo:foo. However, either way the srcs should be the |
| 337 | * same independent of the evaluation being incremental or clean. |
| 338 | */ |
| 339 | @Test |
| 340 | public void testGlobWithExternalSymlink() throws Exception { |
| 341 | scratch.file( |
| 342 | "foo/BUILD", |
| 343 | "sh_library(name = 'foo', srcs = glob(['*.sh']))", |
| 344 | "sh_library(name = 'bar', srcs = glob(['link.sh']))", |
| 345 | "sh_library(name = 'baz', srcs = glob(['subdir_link/*.txt']))"); |
| 346 | scratch.file("foo/ordinary.sh"); |
| 347 | Path externalTarget = scratch.file("../ops/target.txt"); |
| 348 | FileSystemUtils.ensureSymbolicLink(scratch.resolve("foo/link.sh"), externalTarget); |
| 349 | FileSystemUtils.ensureSymbolicLink( |
| 350 | scratch.resolve("foo/subdir_link"), externalTarget.getParentDirectory()); |
| 351 | preparePackageLoading(rootDirectory); |
| 352 | SkyKey fooKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 353 | PackageValue fooValue = validPackage(fooKey); |
| 354 | assertSrcs(fooValue, "foo", "//foo:link.sh", "//foo:ordinary.sh"); |
| 355 | assertSrcs(fooValue, "bar", "//foo:link.sh"); |
| 356 | assertSrcs(fooValue, "baz", "//foo:subdir_link/target.txt"); |
| 357 | scratch.overwriteFile( |
| 358 | "foo/BUILD", |
| 359 | "sh_library(name = 'foo', srcs = glob(['*.sh'])) #comment", |
| 360 | "sh_library(name = 'bar', srcs = glob(['link.sh']))", |
| 361 | "sh_library(name = 'baz', srcs = glob(['subdir_link/*.txt']))"); |
| 362 | getSkyframeExecutor() |
| 363 | .invalidateFilesUnderPathForTesting( |
| 364 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 365 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 366 | Root.fromPath(rootDirectory)); |
Janak Ramakrishnan | ffbc28a | 2017-02-13 22:51:45 +0000 | [diff] [blame] | 367 | PackageValue fooValue2 = validPackage(fooKey); |
| 368 | assertThat(fooValue2).isNotEqualTo(fooValue); |
| 369 | assertSrcs(fooValue2, "foo", "//foo:link.sh", "//foo:ordinary.sh"); |
| 370 | assertSrcs(fooValue2, "bar", "//foo:link.sh"); |
| 371 | assertSrcs(fooValue2, "baz", "//foo:subdir_link/target.txt"); |
| 372 | } |
| 373 | |
| 374 | private static void assertSrcs(PackageValue value, String targetName, String... expected) |
| 375 | throws NoSuchTargetException { |
| 376 | List<Label> expectedLabels = new ArrayList<>(); |
| 377 | for (String item : expected) { |
| 378 | expectedLabels.add(Label.parseAbsoluteUnchecked(item)); |
| 379 | } |
| 380 | assertThat(getSrcs(value, targetName)).containsExactlyElementsIn(expectedLabels).inOrder(); |
| 381 | } |
| 382 | |
| 383 | @SuppressWarnings("unchecked") |
| 384 | private static Iterable<Label> getSrcs(PackageValue packageValue, String targetName) |
| 385 | throws NoSuchTargetException { |
| 386 | return (Iterable<Label>) |
| 387 | packageValue |
| 388 | .getPackage() |
| 389 | .getTarget(targetName) |
| 390 | .getAssociatedRule() |
| 391 | .getAttributeContainer() |
| 392 | .getAttr("srcs"); |
| 393 | } |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 394 | @Test |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 395 | public void testOneNewElementInMultipleGlob() throws Exception { |
| 396 | scratch.file( |
| 397 | "foo/BUILD", |
| 398 | "sh_library(name = 'foo', srcs = glob(['*.sh']))", |
| 399 | "sh_library(name = 'bar', srcs = glob(['*.sh', '*.txt']))"); |
| 400 | preparePackageLoading(rootDirectory); |
| 401 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 402 | PackageValue value = validPackage(skyKey); |
| 403 | scratch.file("foo/irrelevent"); |
| 404 | getSkyframeExecutor() |
| 405 | .invalidateFilesUnderPathForTesting( |
| 406 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 407 | ModifiedFileSet.builder().modify(PathFragment.create("foo/irrelevant")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 408 | Root.fromPath(rootDirectory)); |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 409 | assertThat(validPackage(skyKey)).isSameAs(value); |
| 410 | } |
| 411 | |
| 412 | @Test |
| 413 | public void testNoNewElementInMultipleGlob() throws Exception { |
| 414 | scratch.file( |
| 415 | "foo/BUILD", |
| 416 | "sh_library(name = 'foo', srcs = glob(['*.sh', '*.txt']))", |
| 417 | "sh_library(name = 'bar', srcs = glob(['*.sh', '*.txt']))"); |
| 418 | preparePackageLoading(rootDirectory); |
| 419 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 420 | PackageValue value = validPackage(skyKey); |
| 421 | scratch.file("foo/irrelevent"); |
| 422 | getSkyframeExecutor() |
| 423 | .invalidateFilesUnderPathForTesting( |
| 424 | reporter, |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 425 | ModifiedFileSet.builder().modify(PathFragment.create("foo/irrelevant")).build(), |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 426 | Root.fromPath(rootDirectory)); |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 427 | assertThat(validPackage(skyKey)).isSameAs(value); |
| 428 | } |
| 429 | |
| 430 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 431 | public void testTransitiveSkylarkDepsStoredInPackage() throws Exception { |
| 432 | scratch.file("foo/BUILD", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 433 | "load('//bar:ext.bzl', 'a')"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 434 | scratch.file("bar/BUILD"); |
| 435 | scratch.file("bar/ext.bzl", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 436 | "load('//baz:ext.bzl', 'b')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 437 | "a = b"); |
| 438 | scratch.file("baz/BUILD"); |
| 439 | scratch.file("baz/ext.bzl", |
| 440 | "b = 1"); |
| 441 | scratch.file("qux/BUILD"); |
| 442 | scratch.file("qux/ext.bzl", |
| 443 | "c = 1"); |
| 444 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 445 | preparePackageLoading(rootDirectory); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 446 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 447 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 448 | PackageValue value = validPackage(skyKey); |
| 449 | assertThat(value.getPackage().getSkylarkFileDependencies()).containsExactly( |
| 450 | Label.parseAbsolute("//bar:ext.bzl"), Label.parseAbsolute("//baz:ext.bzl")); |
| 451 | |
| 452 | scratch.overwriteFile("bar/ext.bzl", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 453 | "load('//qux:ext.bzl', 'c')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 454 | "a = c"); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 455 | getSkyframeExecutor() |
| 456 | .invalidateFilesUnderPathForTesting( |
| 457 | reporter, |
| 458 | ModifiedFileSet.builder().modify(PathFragment.create("bar/ext.bzl")).build(), |
| 459 | Root.fromPath(rootDirectory)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 460 | |
| 461 | value = validPackage(skyKey); |
| 462 | assertThat(value.getPackage().getSkylarkFileDependencies()).containsExactly( |
| 463 | Label.parseAbsolute("//bar:ext.bzl"), Label.parseAbsolute("//qux:ext.bzl")); |
| 464 | } |
| 465 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 466 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 467 | public void testNonExistingSkylarkExtension() throws Exception { |
| 468 | reporter.removeHandler(failFastHandler); |
| 469 | scratch.file("test/skylark/BUILD", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 470 | "load('//test/skylark:bad_extension.bzl', 'some_symbol')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 471 | "genrule(name = gr,", |
| 472 | " outs = ['out.txt'],", |
| 473 | " cmd = 'echo hello >@')"); |
| 474 | invalidatePackages(); |
| 475 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 476 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//test/skylark")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 477 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 478 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 479 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 480 | ErrorInfo errorInfo = result.getError(skyKey); |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 481 | String expectedMsg = "error loading package 'test/skylark': " |
| 482 | + "Extension file not found. Unable to load file '//test/skylark:bad_extension.bzl': " |
| 483 | + "file doesn't exist or isn't a file"; |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 484 | assertThat(errorInfo.getException()) |
John Field | 1ea7fc3 | 2015-12-22 19:37:19 +0000 | [diff] [blame] | 485 | .hasMessage(expectedMsg); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 488 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 489 | public void testNonExistingSkylarkExtensionFromExtension() throws Exception { |
| 490 | reporter.removeHandler(failFastHandler); |
| 491 | scratch.file("test/skylark/extension.bzl", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 492 | "load('//test/skylark:bad_extension.bzl', 'some_symbol')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 493 | "a = 'a'"); |
| 494 | scratch.file("test/skylark/BUILD", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 495 | "load('//test/skylark:extension.bzl', 'a')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 496 | "genrule(name = gr,", |
| 497 | " outs = ['out.txt'],", |
| 498 | " cmd = 'echo hello >@')"); |
| 499 | invalidatePackages(); |
| 500 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 501 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//test/skylark")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 502 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 503 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 504 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 505 | ErrorInfo errorInfo = result.getError(skyKey); |
| 506 | assertThat(errorInfo.getException()) |
| 507 | .hasMessage("error loading package 'test/skylark': Extension file not found. " |
| 508 | + "Unable to load file '//test/skylark:bad_extension.bzl': " |
| 509 | + "file doesn't exist or isn't a file"); |
| 510 | } |
| 511 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 512 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 513 | public void testSymlinkCycleWithSkylarkExtension() throws Exception { |
| 514 | reporter.removeHandler(failFastHandler); |
| 515 | Path extensionFilePath = scratch.resolve("/workspace/test/skylark/extension.bzl"); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 516 | FileSystemUtils.ensureSymbolicLink(extensionFilePath, PathFragment.create("extension.bzl")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 517 | scratch.file("test/skylark/BUILD", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 518 | "load('//test/skylark:extension.bzl', 'a')", |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 519 | "genrule(name = gr,", |
| 520 | " outs = ['out.txt'],", |
| 521 | " cmd = 'echo hello >@')"); |
| 522 | invalidatePackages(); |
| 523 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 524 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//test/skylark")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 525 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 526 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 527 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 528 | ErrorInfo errorInfo = result.getError(skyKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 529 | assertThat(errorInfo.getRootCauseOfException()).isEqualTo(skyKey); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 530 | assertThat(errorInfo.getException()) |
| 531 | .hasMessage( |
| 532 | "error loading package 'test/skylark': Encountered error while reading extension " |
| 533 | + "file 'test/skylark/extension.bzl': Symlink cycle"); |
| 534 | } |
| 535 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 536 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 537 | public void testIOErrorLookingForSubpackageForLabelIsHandled() throws Exception { |
| 538 | reporter.removeHandler(failFastHandler); |
| 539 | scratch.file("foo/BUILD", |
| 540 | "sh_library(name = 'foo', srcs = ['bar/baz.sh'])"); |
| 541 | Path barBuildFile = scratch.file("foo/bar/BUILD"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 542 | fs.stubStatError(barBuildFile, new IOException("nope")); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 543 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 544 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 545 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 546 | assertThat(result.hasError()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 547 | assertContainsEvent("nope"); |
| 548 | } |
| 549 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 550 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 551 | public void testLoadRelativePath() throws Exception { |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 552 | scratch.file("pkg/BUILD", "load(':ext.bzl', 'a')"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 553 | scratch.file("pkg/ext.bzl", "a = 1"); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 554 | validPackage(PackageValue.key(PackageIdentifier.parse("@//pkg"))); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 557 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 558 | public void testLoadAbsolutePath() throws Exception { |
| 559 | scratch.file("pkg1/BUILD"); |
| 560 | scratch.file("pkg2/BUILD", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 561 | "load('//pkg1:ext.bzl', 'a')"); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 562 | scratch.file("pkg1/ext.bzl", "a = 1"); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 563 | validPackage(PackageValue.key(PackageIdentifier.parse("@//pkg2"))); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Florian Weikert | 92b2236 | 2015-12-03 10:17:18 +0000 | [diff] [blame] | 566 | @Test |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 567 | public void testBadWorkspaceFile() throws Exception { |
| 568 | Path workspacePath = scratch.overwriteFile("WORKSPACE", "junk"); |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 569 | SkyKey skyKey = PackageValue.key(PackageIdentifier.createInMainRepo("external")); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 570 | getSkyframeExecutor() |
| 571 | .invalidate( |
| 572 | Predicates.equalTo( |
Janak Ramakrishnan | 4e2e408 | 2017-01-06 06:28:45 +0000 | [diff] [blame] | 573 | com.google.devtools.build.lib.skyframe.FileStateValue.key( |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 574 | RootedPath.toRootedPath( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 575 | Root.fromPath(workspacePath.getParentDirectory()), |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 576 | PathFragment.create(workspacePath.getBaseName()))))); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 577 | |
| 578 | reporter.removeHandler(failFastHandler); |
| 579 | EvaluationResult<PackageValue> result = |
| 580 | SkyframeExecutorTestUtils.evaluate( |
| 581 | getSkyframeExecutor(), skyKey, /*keepGoing=*/ false, reporter); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 582 | assertThat(result.hasError()).isFalse(); |
| 583 | assertThat(result.get(skyKey).getPackage().containsErrors()).isTrue(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 586 | // Regression test for the two ugly consequences of a bug where GlobFunction incorrectly matched |
| 587 | // dangling symlinks. |
| 588 | @Test |
| 589 | public void testIncrementalSkyframeHybridGlobbingOnDanglingSymlink() throws Exception { |
| 590 | Path packageDirPath = scratch.file("foo/BUILD", |
| 591 | "exports_files(glob(['*.txt']))").getParentDirectory(); |
| 592 | scratch.file("foo/existing.txt"); |
| 593 | FileSystemUtils.ensureSymbolicLink(packageDirPath.getChild("dangling.txt"), "nope"); |
| 594 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 595 | preparePackageLoading(rootDirectory); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 596 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 597 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 598 | PackageValue value = validPackage(skyKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 599 | assertThat(value.getPackage().containsErrors()).isFalse(); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 600 | assertThat(value.getPackage().getTarget("existing.txt").getName()).isEqualTo("existing.txt"); |
| 601 | try { |
| 602 | value.getPackage().getTarget("dangling.txt"); |
| 603 | fail(); |
| 604 | } catch (NoSuchTargetException expected) { |
| 605 | } |
| 606 | |
| 607 | scratch.overwriteFile("foo/BUILD", |
Laurent Le Brun | b639ca8 | 2017-01-17 11:18:23 +0000 | [diff] [blame] | 608 | "exports_files(glob(['*.txt']))", |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 609 | "#some-irrelevant-comment"); |
| 610 | |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 611 | getSkyframeExecutor() |
| 612 | .invalidateFilesUnderPathForTesting( |
| 613 | reporter, |
| 614 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
| 615 | Root.fromPath(rootDirectory)); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 616 | |
| 617 | value = validPackage(skyKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 618 | assertThat(value.getPackage().containsErrors()).isFalse(); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 619 | assertThat(value.getPackage().getTarget("existing.txt").getName()).isEqualTo("existing.txt"); |
| 620 | try { |
| 621 | value.getPackage().getTarget("dangling.txt"); |
| 622 | fail(); |
| 623 | } catch (NoSuchTargetException expected) { |
| 624 | // One consequence of the bug was that dangling symlinks were matched by globs evaluated by |
| 625 | // Skyframe globbing, meaning there would incorrectly be corresponding targets in packages |
| 626 | // that had skyframe cache hits during skyframe hybrid globbing. |
| 627 | } |
| 628 | |
| 629 | scratch.file("foo/nope"); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 630 | getSkyframeExecutor() |
| 631 | .invalidateFilesUnderPathForTesting( |
| 632 | reporter, |
| 633 | ModifiedFileSet.builder().modify(PathFragment.create("foo/nope")).build(), |
| 634 | Root.fromPath(rootDirectory)); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 635 | |
| 636 | PackageValue newValue = validPackage(skyKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 637 | assertThat(newValue.getPackage().containsErrors()).isFalse(); |
Nathan Harmata | 2022ad8 | 2016-02-22 23:04:14 +0000 | [diff] [blame] | 638 | assertThat(newValue.getPackage().getTarget("existing.txt").getName()).isEqualTo("existing.txt"); |
| 639 | // Another consequence of the bug is that change pruning would incorrectly cut off changes that |
| 640 | // caused a dangling symlink potentially matched by a glob to come into existence. |
| 641 | assertThat(newValue.getPackage().getTarget("dangling.txt").getName()).isEqualTo("dangling.txt"); |
| 642 | assertThat(newValue.getPackage()).isNotSameAs(value.getPackage()); |
| 643 | } |
| 644 | |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 645 | // Regression test for Skyframe globbing incorrectly matching the package's directory path on |
| 646 | // 'glob(['**'], exclude_directories = 0)'. We test for this directly by triggering |
| 647 | // hybrid globbing (gives coverage for both legacy globbing and skyframe globbing). |
| 648 | @Test |
| 649 | public void testRecursiveGlobNeverMatchesPackageDirectory() throws Exception { |
| 650 | scratch.file("foo/BUILD", |
| 651 | "[sh_library(name = x + '-matched') for x in glob(['**'], exclude_directories = 0)]"); |
| 652 | scratch.file("foo/bar"); |
| 653 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 654 | preparePackageLoading(rootDirectory); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 655 | |
Brian Silverman | d7d6d62 | 2016-03-17 09:53:39 +0000 | [diff] [blame] | 656 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 657 | PackageValue value = validPackage(skyKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 658 | assertThat(value.getPackage().containsErrors()).isFalse(); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 659 | assertThat(value.getPackage().getTarget("bar-matched").getName()).isEqualTo("bar-matched"); |
| 660 | try { |
| 661 | value.getPackage().getTarget("-matched"); |
| 662 | fail(); |
| 663 | } catch (NoSuchTargetException expected) { |
| 664 | } |
| 665 | |
| 666 | scratch.overwriteFile("foo/BUILD", |
| 667 | "[sh_library(name = x + '-matched') for x in glob(['**'], exclude_directories = 0)]", |
| 668 | "#some-irrelevant-comment"); |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 669 | getSkyframeExecutor() |
| 670 | .invalidateFilesUnderPathForTesting( |
| 671 | reporter, |
| 672 | ModifiedFileSet.builder().modify(PathFragment.create("foo/BUILD")).build(), |
| 673 | Root.fromPath(rootDirectory)); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 674 | |
| 675 | value = validPackage(skyKey); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 676 | assertThat(value.getPackage().containsErrors()).isFalse(); |
Nathan Harmata | 86c319e | 2016-02-25 01:12:22 +0000 | [diff] [blame] | 677 | assertThat(value.getPackage().getTarget("bar-matched").getName()).isEqualTo("bar-matched"); |
| 678 | try { |
| 679 | value.getPackage().getTarget("-matched"); |
| 680 | fail(); |
| 681 | } catch (NoSuchTargetException expected) { |
| 682 | } |
| 683 | } |
| 684 | |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 685 | @Test |
| 686 | public void testPackageLoadingErrorOnIOExceptionReadingBuildFile() throws Exception { |
| 687 | Path fooBuildFilePath = scratch.file("foo/BUILD"); |
| 688 | IOException exn = new IOException("nope"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 689 | fs.throwExceptionOnGetInputStream(fooBuildFilePath, exn); |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 690 | |
| 691 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 692 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 693 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
| 694 | assertThat(result.hasError()).isTrue(); |
| 695 | ErrorInfo errorInfo = result.getError(skyKey); |
| 696 | String errorMessage = errorInfo.getException().getMessage(); |
| 697 | assertThat(errorMessage).contains("nope"); |
| 698 | assertThat(errorInfo.getException()).isInstanceOf(NoSuchPackageException.class); |
nharmata | 351efff | 2018-03-30 15:04:38 -0700 | [diff] [blame] | 699 | assertThat(errorInfo.getException()).hasCauseThat().isInstanceOf(IOException.class); |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 700 | } |
| 701 | |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 702 | @Test |
| 703 | public void testPackageLoadingErrorOnIOExceptionReadingBzlFile() throws Exception { |
| 704 | scratch.file("foo/BUILD", "load('//foo:bzl.bzl', 'x')"); |
| 705 | Path fooBzlFilePath = scratch.file("foo/bzl.bzl"); |
| 706 | IOException exn = new IOException("nope"); |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 707 | fs.throwExceptionOnGetInputStream(fooBzlFilePath, exn); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 708 | |
| 709 | SkyKey skyKey = PackageValue.key(PackageIdentifier.parse("@//foo")); |
| 710 | EvaluationResult<PackageValue> result = SkyframeExecutorTestUtils.evaluate( |
| 711 | getSkyframeExecutor(), skyKey, /*keepGoing=*/false, reporter); |
| 712 | assertThat(result.hasError()).isTrue(); |
| 713 | ErrorInfo errorInfo = result.getError(skyKey); |
| 714 | String errorMessage = errorInfo.getException().getMessage(); |
| 715 | assertThat(errorMessage).contains("nope"); |
| 716 | assertThat(errorInfo.getException()).isInstanceOf(NoSuchPackageException.class); |
nharmata | 351efff | 2018-03-30 15:04:38 -0700 | [diff] [blame] | 717 | assertThat(errorInfo.getException()).hasCauseThat().isInstanceOf(IOException.class); |
nharmata | bea67e9 | 2017-06-16 00:26:27 +0200 | [diff] [blame] | 718 | } |
| 719 | |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 720 | private static class CustomInMemoryFs extends InMemoryFileSystem { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 721 | private abstract static class FileStatusOrException { |
| 722 | abstract FileStatus get() throws IOException; |
| 723 | |
| 724 | private static class ExceptionImpl extends FileStatusOrException { |
| 725 | private final IOException exn; |
| 726 | |
| 727 | private ExceptionImpl(IOException exn) { |
| 728 | this.exn = exn; |
| 729 | } |
| 730 | |
| 731 | @Override |
| 732 | FileStatus get() throws IOException { |
| 733 | throw exn; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | private static class FileStatusImpl extends FileStatusOrException { |
| 738 | |
| 739 | @Nullable |
| 740 | private final FileStatus fileStatus; |
| 741 | |
| 742 | private FileStatusImpl(@Nullable FileStatus fileStatus) { |
| 743 | this.fileStatus = fileStatus; |
| 744 | } |
| 745 | |
| 746 | @Override |
| 747 | @Nullable |
| 748 | FileStatus get() { |
| 749 | return fileStatus; |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 754 | private final Map<Path, FileStatusOrException> stubbedStats = Maps.newHashMap(); |
| 755 | private final Set<Path> makeUnreadableAfterReaddir = Sets.newHashSet(); |
| 756 | private final Map<Path, IOException> pathsToErrorOnGetInputStream = Maps.newHashMap(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 757 | |
| 758 | public CustomInMemoryFs(ManualClock manualClock) { |
| 759 | super(manualClock); |
| 760 | } |
| 761 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 762 | public void stubStat(Path path, @Nullable FileStatus stubbedResult) { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 763 | stubbedStats.put(path, new FileStatusOrException.FileStatusImpl(stubbedResult)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 764 | } |
| 765 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 766 | public void stubStatError(Path path, IOException stubbedResult) { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 767 | stubbedStats.put(path, new FileStatusOrException.ExceptionImpl(stubbedResult)); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 771 | public FileStatus stat(Path path, boolean followSymlinks) throws IOException { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 772 | if (stubbedStats.containsKey(path)) { |
| 773 | return stubbedStats.get(path).get(); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 774 | } |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 775 | return super.stat(path, followSymlinks); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 776 | } |
| 777 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 778 | public void scheduleMakeUnreadableAfterReaddir(Path path) { |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 779 | makeUnreadableAfterReaddir.add(path); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 783 | public Collection<Dirent> readdir(Path path, boolean followSymlinks) throws IOException { |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 784 | Collection<Dirent> result = super.readdir(path, followSymlinks); |
Googler | c804c66 | 2016-12-01 16:53:28 +0000 | [diff] [blame] | 785 | if (makeUnreadableAfterReaddir.contains(path)) { |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 786 | path.setReadable(false); |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 787 | } |
| 788 | return result; |
| 789 | } |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 790 | |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 791 | public void throwExceptionOnGetInputStream(Path path, IOException exn) { |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 792 | pathsToErrorOnGetInputStream.put(path, exn); |
| 793 | } |
| 794 | |
| 795 | @Override |
aehlig | c801c39 | 2017-12-19 07:12:25 -0800 | [diff] [blame] | 796 | protected InputStream getInputStream(Path path) throws IOException { |
nharmata | ff688bf | 2017-06-07 17:03:52 -0400 | [diff] [blame] | 797 | IOException exnToThrow = pathsToErrorOnGetInputStream.get(path); |
| 798 | if (exnToThrow != null) { |
| 799 | throw exnToThrow; |
| 800 | } |
| 801 | return super.getInputStream(path); |
| 802 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 803 | } |
Kristina Chodorow | 335f067 | 2015-11-16 23:19:13 +0000 | [diff] [blame] | 804 | } |