Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.pkgcache; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 17 | import static org.junit.Assert.fail; |
| 18 | |
| 19 | import com.google.common.base.Joiner; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 20 | import com.google.common.base.Preconditions; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableMap; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 23 | import com.google.common.eventbus.EventBus; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
philwo | 3bcb9f6 | 2017-09-06 12:52:21 +0200 | [diff] [blame] | 27 | import com.google.devtools.build.lib.clock.BlazeClock; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.cmdline.Label; |
| 29 | import com.google.devtools.build.lib.events.Reporter; |
| 30 | import com.google.devtools.build.lib.packages.ConstantRuleVisibility; |
| 31 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 32 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
| 33 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
| 34 | import com.google.devtools.build.lib.packages.Package; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.packages.Rule; |
brandjon | 674ab86 | 2017-10-06 23:17:33 +0200 | [diff] [blame] | 36 | import com.google.devtools.build.lib.packages.SkylarkSemanticsOptions; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.packages.Target; |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.packages.util.LoadingMock; |
janakr | 5e606e6 | 2017-07-19 22:40:20 +0200 | [diff] [blame] | 39 | import com.google.devtools.build.lib.skyframe.BazelSkyframeExecutorConstants; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.skyframe.DiffAwareness; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.skyframe.SequencedSkyframeExecutor; |
| 42 | import com.google.devtools.build.lib.skyframe.SkyValueDirtinessChecker; |
| 43 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
| 44 | import com.google.devtools.build.lib.syntax.GlobList; |
| 45 | import com.google.devtools.build.lib.testutil.ManualClock; |
janakr | 150858b | 2017-07-25 00:04:53 +0200 | [diff] [blame] | 46 | import com.google.devtools.build.lib.testutil.TestConstants; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 48 | import com.google.devtools.build.lib.vfs.Dirent; |
| 49 | import com.google.devtools.build.lib.vfs.FileStatus; |
| 50 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 51 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 52 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
| 53 | import com.google.devtools.build.lib.vfs.Path; |
| 54 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 55 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 56 | import com.google.devtools.build.skyframe.SkyFunction; |
| 57 | import com.google.devtools.build.skyframe.SkyFunctionName; |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 58 | import com.google.devtools.common.options.Options; |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 59 | import com.google.devtools.common.options.OptionsClassProvider; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 60 | import java.io.FileNotFoundException; |
| 61 | import java.io.IOException; |
| 62 | import java.util.ArrayList; |
| 63 | import java.util.Collection; |
| 64 | import java.util.List; |
| 65 | import java.util.UUID; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 66 | import javax.annotation.Nullable; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 67 | import org.junit.Before; |
| 68 | import org.junit.Test; |
| 69 | import org.junit.runner.RunWith; |
| 70 | import org.junit.runners.JUnit4; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Tests for incremental loading; these cover both normal operation and diff awareness, for which a |
| 74 | * list of modified / added / removed files is available. |
| 75 | */ |
| 76 | @RunWith(JUnit4.class) |
| 77 | public class IncrementalLoadingTest { |
| 78 | protected PackageCacheTester tester; |
| 79 | |
| 80 | private Path throwOnReaddir = null; |
| 81 | private Path throwOnStat = null; |
| 82 | |
| 83 | @Before |
| 84 | public final void createTester() throws Exception { |
| 85 | ManualClock clock = new ManualClock(); |
| 86 | FileSystem fs = |
| 87 | new InMemoryFileSystem(clock) { |
| 88 | @Override |
| 89 | public Collection<Dirent> readdir(Path path, boolean followSymlinks) throws IOException { |
| 90 | if (path.equals(throwOnReaddir)) { |
| 91 | throw new FileNotFoundException(path.getPathString()); |
| 92 | } |
| 93 | return super.readdir(path, followSymlinks); |
| 94 | } |
| 95 | |
| 96 | @Nullable |
| 97 | @Override |
| 98 | public FileStatus stat(Path path, boolean followSymlinks) throws IOException { |
| 99 | if (path.equals(throwOnStat)) { |
| 100 | throw new IOException("bork " + path.getPathString()); |
| 101 | } |
| 102 | return super.stat(path, followSymlinks); |
| 103 | } |
| 104 | }; |
| 105 | tester = createTester(fs, clock); |
| 106 | } |
| 107 | |
| 108 | protected PackageCacheTester createTester(FileSystem fs, ManualClock clock) throws Exception { |
laurentlb | 2e1b904 | 2017-04-12 00:17:47 +0000 | [diff] [blame] | 109 | return new PackageCacheTester(fs, clock); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void testNoChange() throws Exception { |
| 114 | tester.addFile("base/BUILD", |
| 115 | "filegroup(name = 'hello', srcs = ['foo.txt'])"); |
| 116 | tester.sync(); |
| 117 | Target oldTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 118 | assertThat(oldTarget).isNotNull(); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 119 | |
| 120 | tester.sync(); |
| 121 | Target newTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 122 | assertThat(newTarget).isSameAs(oldTarget); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testModifyBuildFile() throws Exception { |
| 127 | tester.addFile("base/BUILD", "filegroup(name = 'hello', srcs = ['foo.txt'])"); |
| 128 | tester.sync(); |
| 129 | Target oldTarget = tester.getTarget("//base:hello"); |
| 130 | |
| 131 | tester.modifyFile("base/BUILD", "filegroup(name = 'hello', srcs = ['bar.txt'])"); |
| 132 | tester.sync(); |
| 133 | Target newTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 134 | assertThat(newTarget).isNotSameAs(oldTarget); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | @Test |
| 138 | public void testModifyNonBuildFile() throws Exception { |
| 139 | tester.addFile("base/BUILD", "filegroup(name = 'hello', srcs = ['foo.txt'])"); |
| 140 | tester.addFile("base/foo.txt", "nothing"); |
| 141 | tester.sync(); |
| 142 | Target oldTarget = tester.getTarget("//base:hello"); |
| 143 | |
| 144 | tester.modifyFile("base/foo.txt", "other"); |
| 145 | tester.sync(); |
| 146 | Target newTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 147 | assertThat(newTarget).isSameAs(oldTarget); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testRemoveNonBuildFile() throws Exception { |
| 152 | tester.addFile("base/BUILD", "filegroup(name = 'hello', srcs = ['foo.txt'])"); |
| 153 | tester.addFile("base/foo.txt", "nothing"); |
| 154 | tester.sync(); |
| 155 | Target oldTarget = tester.getTarget("//base:hello"); |
| 156 | |
| 157 | tester.removeFile("base/foo.txt"); |
| 158 | tester.sync(); |
| 159 | Target newTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 160 | assertThat(newTarget).isSameAs(oldTarget); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testModifySymlinkedFileSamePackage() throws Exception { |
| 165 | tester.addSymlink("base/BUILD", "mybuild"); |
| 166 | tester.addFile("base/mybuild", "filegroup(name = 'hello', srcs = ['foo.txt'])"); |
| 167 | tester.sync(); |
| 168 | Target oldTarget = tester.getTarget("//base:hello"); |
| 169 | tester.modifyFile("base/mybuild", "filegroup(name = 'hello', srcs = ['bar.txt'])"); |
| 170 | tester.sync(); |
| 171 | Target newTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 172 | assertThat(newTarget).isNotSameAs(oldTarget); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Test |
| 176 | public void testModifySymlinkedFileDifferentPackage() throws Exception { |
| 177 | tester.addSymlink("base/BUILD", "../other/BUILD"); |
| 178 | tester.addFile("other/BUILD", "filegroup(name = 'hello', srcs = ['foo.txt'])"); |
| 179 | tester.sync(); |
| 180 | Target oldTarget = tester.getTarget("//base:hello"); |
| 181 | |
| 182 | tester.modifyFile("other/BUILD", "filegroup(name = 'hello', srcs = ['bar.txt'])"); |
| 183 | tester.sync(); |
| 184 | Target newTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 185 | assertThat(newTarget).isNotSameAs(oldTarget); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void testBUILDSymlinkModifiedThenChanges() throws Exception { |
| 190 | // We need to ensure that the timestamps of "one" and "two" are different, because Blaze |
| 191 | // currently does not recognize changes to symlinks if the timestamps of the old and the new |
| 192 | // file pointed to by the symlink are the same. |
| 193 | tester.addFile("one", "filegroup(name='a', srcs=['1'])"); |
| 194 | tester.sync(); |
| 195 | |
| 196 | tester.addFile("two", "filegroup(name='a', srcs=['2'])"); |
| 197 | tester.addSymlink("oldlink", "one"); |
| 198 | tester.addSymlink("newlink", "one"); |
| 199 | tester.addSymlink("a/BUILD", "../oldlink"); |
| 200 | tester.sync(); |
| 201 | Target a1 = tester.getTarget("//a:a"); |
| 202 | |
| 203 | tester.modifySymlink("a/BUILD", "../newlink"); |
| 204 | tester.sync(); |
| 205 | |
| 206 | tester.getTarget("//a:a"); |
| 207 | |
| 208 | tester.modifySymlink("newlink", "two"); |
| 209 | tester.sync(); |
| 210 | |
| 211 | Target a3 = tester.getTarget("//a:a"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 212 | assertThat(a3).isNotSameAs(a1); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void testBUILDFileIsExternalSymlinkAndChanges() throws Exception { |
| 217 | tester.addFile("/nonroot/file", "filegroup(name='a', srcs=['file'])"); |
| 218 | tester.addSymlink("a/BUILD", "/nonroot/file"); |
| 219 | tester.sync(); |
| 220 | |
| 221 | Target a1 = tester.getTarget("//a:a"); |
| 222 | tester.modifyFile("/nonroot/file", "filegroup(name='a', srcs=['file2'])"); |
| 223 | tester.sync(); |
| 224 | |
| 225 | Target a2 = tester.getTarget("//a:a"); |
| 226 | tester.sync(); |
| 227 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 228 | assertThat(a2).isNotSameAs(a1); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Test |
| 232 | public void testLabelWithTwoSegmentsAndTotalInvalidation() throws Exception { |
| 233 | tester.addFile("a/BUILD", "filegroup(name='fg', srcs=['b/c'])"); |
| 234 | tester.addFile("a/b/BUILD"); |
| 235 | tester.sync(); |
| 236 | |
| 237 | Target fg1 = tester.getTarget("//a:fg"); |
| 238 | tester.everythingModified(); |
| 239 | tester.sync(); |
| 240 | |
| 241 | Target fg2 = tester.getTarget("//a:fg"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 242 | assertThat(fg2).isSameAs(fg1); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | @Test |
| 246 | public void testAddGlobFile() throws Exception { |
| 247 | tester.addFile("base/BUILD", "filegroup(name = 'hello', srcs = glob(['*.txt']))"); |
| 248 | tester.addFile("base/foo.txt", "nothing"); |
| 249 | tester.sync(); |
| 250 | Target oldTarget = tester.getTarget("//base:hello"); |
| 251 | |
| 252 | tester.addFile("base/bar.txt", "also nothing"); |
| 253 | tester.sync(); |
| 254 | Target newTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 255 | assertThat(newTarget).isNotSameAs(oldTarget); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | @Test |
| 259 | public void testRemoveGlobFile() throws Exception { |
| 260 | tester.addFile("base/BUILD", "filegroup(name = 'hello', srcs = glob(['*.txt']))"); |
| 261 | tester.addFile("base/foo.txt", "nothing"); |
| 262 | tester.addFile("base/bar.txt", "also nothing"); |
| 263 | tester.sync(); |
| 264 | Target oldTarget = tester.getTarget("//base:hello"); |
| 265 | |
| 266 | tester.removeFile("base/bar.txt"); |
| 267 | tester.sync(); |
| 268 | Target newTarget = tester.getTarget("//base:hello"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 269 | assertThat(newTarget).isNotSameAs(oldTarget); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Test |
| 273 | public void testPackageNotInLastBuildReplaced() throws Exception { |
| 274 | tester.addFile("a/BUILD", "filegroup(name='a', srcs=['bad.sh'])"); |
| 275 | tester.sync(); |
| 276 | Target a1 = tester.getTarget("//a:a"); |
| 277 | |
| 278 | tester.addFile("b/BUILD", "filegroup(name='b', srcs=['b.sh'])"); |
| 279 | tester.modifyFile("a/BUILD", "filegroup(name='a', srcs=['good.sh'])"); |
| 280 | tester.sync(); |
| 281 | tester.getTarget("//b:b"); |
| 282 | |
| 283 | tester.sync(); |
| 284 | Target a2 = tester.getTarget("//a:a"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 285 | assertThat(a2).isNotSameAs(a1); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | @Test |
| 289 | public void testBrokenSymlinkAddedThenFixed() throws Exception { |
| 290 | tester.addFile("a/BUILD", "filegroup(name='a', srcs=glob(['**']))"); |
| 291 | tester.sync(); |
| 292 | Target a1 = tester.getTarget("//a:a"); |
| 293 | |
| 294 | tester.addSymlink("a/b", "../c"); |
| 295 | tester.sync(); |
| 296 | tester.getTarget("//a:a"); |
| 297 | |
| 298 | tester.addFile("c"); |
| 299 | tester.sync(); |
| 300 | Target a3 = tester.getTarget("//a:a"); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 301 | assertThat(a3).isNotSameAs(a1); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | @Test |
| 305 | public void testBuildFileWithSyntaxError() throws Exception { |
| 306 | tester.addFile("a/BUILD", "sh_library(xyz='a')"); |
| 307 | tester.sync(); |
| 308 | try { |
| 309 | tester.getTarget("//a:a"); |
| 310 | fail(); |
| 311 | } catch (NoSuchThingException e) { |
| 312 | // Expected |
| 313 | } |
| 314 | |
| 315 | tester.modifyFile("a/BUILD", "sh_library(name='a')"); |
| 316 | tester.sync(); |
| 317 | tester.getTarget("//a:a"); |
| 318 | } |
| 319 | |
| 320 | @Test |
| 321 | public void testSymlinkedBuildFileWithSyntaxError() throws Exception { |
| 322 | tester.addFile("a/BUILD.real", "sh_library(xyz='a')"); |
| 323 | tester.addSymlink("a/BUILD", "BUILD.real"); |
| 324 | tester.sync(); |
| 325 | try { |
| 326 | tester.getTarget("//a:a"); |
| 327 | fail(); |
| 328 | } catch (NoSuchThingException e) { |
| 329 | // Expected |
| 330 | } |
| 331 | tester.modifyFile("a/BUILD.real", "sh_library(name='a')"); |
| 332 | tester.sync(); |
| 333 | tester.getTarget("//a:a"); |
| 334 | } |
| 335 | |
| 336 | @Test |
| 337 | public void testTransientErrorsInGlobbing() throws Exception { |
| 338 | Path buildFile = tester.addFile("e/BUILD", "sh_library(name = 'e', data = glob(['*.txt']))"); |
| 339 | Path parentDir = buildFile.getParentDirectory(); |
| 340 | tester.addFile("e/data.txt"); |
| 341 | throwOnReaddir = parentDir; |
| 342 | tester.sync(); |
janakr | 28adce5 | 2017-07-13 19:55:32 +0200 | [diff] [blame] | 343 | try { |
| 344 | tester.getTarget("//e:e"); |
brandjon | 674ab86 | 2017-10-06 23:17:33 +0200 | [diff] [blame] | 345 | fail("Expected exception"); |
janakr | 28adce5 | 2017-07-13 19:55:32 +0200 | [diff] [blame] | 346 | } catch (NoSuchPackageException expected) { |
| 347 | } |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 348 | throwOnReaddir = null; |
| 349 | tester.sync(); |
janakr | 28adce5 | 2017-07-13 19:55:32 +0200 | [diff] [blame] | 350 | Target target = tester.getTarget("//e:e"); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 351 | assertThat(((Rule) target).containsErrors()).isFalse(); |
janakr | 28adce5 | 2017-07-13 19:55:32 +0200 | [diff] [blame] | 352 | GlobList<?> globList = (GlobList<?>) ((Rule) target).getAttributeContainer().getAttr("data"); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 353 | assertThat(globList).containsExactly(Label.parseAbsolute("//e:data.txt")); |
| 354 | } |
| 355 | |
| 356 | @Test |
| 357 | public void testIrrelevantFileInSubdirDoesntReloadPackage() throws Exception { |
| 358 | tester.addFile("pkg/BUILD", "sh_library(name = 'pkg', srcs = glob(['**/*.sh']))"); |
| 359 | tester.addFile("pkg/pkg.sh", "#!/bin/bash"); |
| 360 | tester.addFile("pkg/bar/bar.sh", "#!/bin/bash"); |
| 361 | Package pkg = tester.getTarget("//pkg:pkg").getPackage(); |
| 362 | |
| 363 | // Write file in directory to force reload of top-level glob. |
| 364 | tester.addFile("pkg/irrelevant_file"); |
| 365 | tester.addFile("pkg/bar/irrelevant_file"); // Subglob is also reloaded. |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 366 | assertThat(tester.getTarget("//pkg:pkg").getPackage()).isSameAs(pkg); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | @Test |
| 370 | public void testMissingPackages() throws Exception { |
| 371 | tester.sync(); |
| 372 | |
| 373 | try { |
| 374 | tester.getTarget("//a:a"); |
| 375 | fail(); |
| 376 | } catch (NoSuchThingException e) { |
| 377 | // expected |
| 378 | } |
| 379 | |
| 380 | tester.addFile("a/BUILD", "sh_library(name='a')"); |
| 381 | tester.sync(); |
| 382 | tester.getTarget("//a:a"); |
| 383 | } |
| 384 | |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 385 | @Test |
| 386 | public void testChangedExternalFile() throws Exception { |
| 387 | tester.addFile("a/BUILD", |
laurentlb | 5ddd804 | 2017-11-30 12:03:31 -0800 | [diff] [blame] | 388 | "load('//a:b.bzl', 'b')", |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 389 | "b()"); |
| 390 | |
| 391 | tester.addFile("/b.bzl", |
| 392 | "def b():", |
| 393 | " pass"); |
| 394 | tester.addSymlink("a/b.bzl", "/b.bzl"); |
| 395 | tester.sync(); |
| 396 | tester.getTarget("//a:BUILD"); |
| 397 | tester.modifyFile("/b.bzl", "ERROR ERROR"); |
| 398 | tester.sync(); |
| 399 | |
| 400 | try { |
| 401 | tester.getTarget("//a:BUILD"); |
| 402 | fail(); |
| 403 | } catch (NoSuchThingException e) { |
| 404 | // expected |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 409 | static class PackageCacheTester { |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 410 | private class ManualDiffAwareness implements DiffAwareness { |
| 411 | private View lastView; |
| 412 | private View currentView; |
| 413 | |
| 414 | @Override |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 415 | public View getCurrentView(OptionsClassProvider options) { |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 416 | lastView = currentView; |
| 417 | currentView = new View() {}; |
| 418 | return currentView; |
| 419 | } |
| 420 | |
| 421 | @Override |
| 422 | public ModifiedFileSet getDiff(View oldView, View newView) { |
| 423 | if (oldView == lastView && newView == currentView) { |
| 424 | return Preconditions.checkNotNull(modifiedFileSet); |
| 425 | } else { |
| 426 | return ModifiedFileSet.EVERYTHING_MODIFIED; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | @Override |
| 431 | public String name() { |
| 432 | return "PackageCacheTester.DiffAwareness"; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void close() { |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | private class ManualDiffAwarenessFactory implements DiffAwareness.Factory { |
| 441 | @Nullable |
| 442 | @Override |
| 443 | public DiffAwareness maybeCreate(Path pathEntry) { |
| 444 | return pathEntry == workspace ? new ManualDiffAwareness() : null; |
| 445 | } |
| 446 | } |
| 447 | |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 448 | private final ManualClock clock; |
| 449 | private final Path workspace; |
| 450 | private final Path outputBase; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 451 | private final Reporter reporter = new Reporter(new EventBus()); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 452 | private final SkyframeExecutor skyframeExecutor; |
| 453 | private final List<Path> changes = new ArrayList<>(); |
| 454 | private boolean everythingModified = false; |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 455 | private ModifiedFileSet modifiedFileSet; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 456 | private final ActionKeyContext actionKeyContext = new ActionKeyContext(); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 457 | |
laurentlb | 2e1b904 | 2017-04-12 00:17:47 +0000 | [diff] [blame] | 458 | public PackageCacheTester(FileSystem fs, ManualClock clock) throws IOException { |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 459 | this.clock = clock; |
| 460 | workspace = fs.getPath("/workspace"); |
| 461 | workspace.createDirectory(); |
| 462 | outputBase = fs.getPath("/output_base"); |
| 463 | outputBase.createDirectory(); |
| 464 | addFile("WORKSPACE"); |
| 465 | |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 466 | LoadingMock loadingMock = LoadingMock.get(); |
janakr | 52d05e8 | 2017-09-22 13:27:14 -0400 | [diff] [blame] | 467 | BlazeDirectories directories = |
| 468 | new BlazeDirectories( |
| 469 | new ServerDirectories(fs.getPath("/install"), fs.getPath("/output")), |
| 470 | workspace, |
| 471 | loadingMock.getProductName()); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 472 | skyframeExecutor = |
janakr | 5e606e6 | 2017-07-19 22:40:20 +0200 | [diff] [blame] | 473 | SequencedSkyframeExecutor.create( |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 474 | loadingMock |
janakr | 52d05e8 | 2017-09-22 13:27:14 -0400 | [diff] [blame] | 475 | .getPackageFactoryBuilderForTesting(directories) |
nharmata | d922e65 | 2017-05-17 20:29:19 +0200 | [diff] [blame] | 476 | .build(loadingMock.createRuleClassProvider(), fs), |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 477 | fs, |
janakr | 52d05e8 | 2017-09-22 13:27:14 -0400 | [diff] [blame] | 478 | directories, |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 479 | actionKeyContext, |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 480 | null, /* workspaceStatusActionFactory */ |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 481 | loadingMock.createRuleClassProvider().getBuildInfoFactories(), |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 482 | ImmutableList.of(new ManualDiffAwarenessFactory()), |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 483 | ImmutableMap.<SkyFunctionName, SkyFunction>of(), |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 484 | ImmutableList.<SkyValueDirtinessChecker>of(), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame^] | 485 | BazelSkyframeExecutorConstants.HARDCODED_BLACKLISTED_PACKAGE_PREFIXES, |
| 486 | BazelSkyframeExecutorConstants.ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE, |
janakr | 5e606e6 | 2017-07-19 22:40:20 +0200 | [diff] [blame] | 487 | BazelSkyframeExecutorConstants.CROSS_REPOSITORY_LABEL_VIOLATION_STRATEGY, |
| 488 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY, |
| 489 | BazelSkyframeExecutorConstants.ACTION_ON_IO_EXCEPTION_READING_BUILD_FILE); |
janakr | 150858b | 2017-07-25 00:04:53 +0200 | [diff] [blame] | 490 | TestConstants.processSkyframeExecutorForTesting(skyframeExecutor); |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 491 | PackageCacheOptions packageCacheOptions = Options.getDefaults(PackageCacheOptions.class); |
| 492 | packageCacheOptions.defaultVisibility = ConstantRuleVisibility.PUBLIC; |
| 493 | packageCacheOptions.showLoadingProgress = true; |
| 494 | packageCacheOptions.globbingThreads = 7; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 495 | skyframeExecutor.preparePackageLoading( |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 496 | new PathPackageLocator( |
| 497 | outputBase, |
| 498 | ImmutableList.of(workspace), |
| 499 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 500 | packageCacheOptions, |
brandjon | 7dc3416 | 2017-04-27 18:34:33 +0200 | [diff] [blame] | 501 | Options.getDefaults(SkylarkSemanticsOptions.class), |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 502 | "", |
| 503 | UUID.randomUUID(), |
| 504 | ImmutableMap.<String, String>of(), |
Damien Martin-Guillerez | 777f3af | 2017-02-08 17:22:02 +0000 | [diff] [blame] | 505 | ImmutableMap.<String, String>of(), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 506 | new TimestampGranularityMonitor(BlazeClock.instance())); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | Path addFile(String fileName, String... content) throws IOException { |
| 510 | Path buildFile = workspace.getRelative(fileName); |
| 511 | Preconditions.checkState(!buildFile.exists()); |
| 512 | Path currentPath = buildFile; |
| 513 | |
| 514 | // Add the new file and all the directories that will be created by |
| 515 | // createDirectoryAndParents() |
| 516 | while (!currentPath.exists()) { |
| 517 | changes.add(currentPath); |
| 518 | currentPath = currentPath.getParentDirectory(); |
| 519 | } |
| 520 | |
| 521 | FileSystemUtils.createDirectoryAndParents(buildFile.getParentDirectory()); |
| 522 | FileSystemUtils.writeContentAsLatin1(buildFile, Joiner.on('\n').join(content)); |
| 523 | return buildFile; |
| 524 | } |
| 525 | |
| 526 | void addSymlink(String fileName, String target) throws IOException { |
| 527 | Path path = workspace.getRelative(fileName); |
| 528 | Preconditions.checkState(!path.exists()); |
| 529 | FileSystemUtils.createDirectoryAndParents(path.getParentDirectory()); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 530 | path.createSymbolicLink(PathFragment.create(target)); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 531 | changes.add(path); |
| 532 | } |
| 533 | |
| 534 | void removeFile(String fileName) throws IOException { |
| 535 | Path path = workspace.getRelative(fileName); |
| 536 | Preconditions.checkState(path.delete()); |
| 537 | changes.add(path); |
| 538 | } |
| 539 | |
| 540 | void modifyFile(String fileName, String... content) throws IOException { |
| 541 | Path path = workspace.getRelative(fileName); |
| 542 | Preconditions.checkState(path.exists()); |
| 543 | Preconditions.checkState(path.delete()); |
| 544 | FileSystemUtils.writeContentAsLatin1(path, Joiner.on('\n').join(content)); |
| 545 | changes.add(path); |
| 546 | } |
| 547 | |
| 548 | void modifySymlink(String fileName, String newTarget) throws IOException { |
| 549 | Path symlink = workspace.getRelative(fileName); |
| 550 | Preconditions.checkState(symlink.exists()); |
| 551 | symlink.delete(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 552 | symlink.createSymbolicLink(PathFragment.create(newTarget)); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 553 | changes.add(symlink); |
| 554 | } |
| 555 | |
| 556 | void everythingModified() { |
| 557 | everythingModified = true; |
| 558 | } |
| 559 | |
| 560 | private ModifiedFileSet getModifiedFileSet() { |
| 561 | if (everythingModified) { |
| 562 | everythingModified = false; |
| 563 | return ModifiedFileSet.EVERYTHING_MODIFIED; |
| 564 | } |
| 565 | |
| 566 | ModifiedFileSet.Builder builder = ModifiedFileSet.builder(); |
| 567 | for (Path path : changes) { |
| 568 | if (!path.startsWith(workspace)) { |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | PathFragment workspacePath = path.relativeTo(workspace); |
| 573 | builder.modify(workspacePath); |
| 574 | } |
| 575 | return builder.build(); |
| 576 | } |
| 577 | |
| 578 | void sync() throws InterruptedException { |
| 579 | clock.advanceMillis(1); |
| 580 | |
Lukacs Berki | de2183d | 2015-12-16 11:25:36 +0000 | [diff] [blame] | 581 | modifiedFileSet = getModifiedFileSet(); |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 582 | PackageCacheOptions packageCacheOptions = Options.getDefaults(PackageCacheOptions.class); |
| 583 | packageCacheOptions.defaultVisibility = ConstantRuleVisibility.PUBLIC; |
| 584 | packageCacheOptions.showLoadingProgress = true; |
| 585 | packageCacheOptions.globbingThreads = 7; |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 586 | skyframeExecutor.preparePackageLoading( |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 587 | new PathPackageLocator( |
| 588 | outputBase, |
| 589 | ImmutableList.of(workspace), |
| 590 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY), |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 591 | packageCacheOptions, |
brandjon | 7dc3416 | 2017-04-27 18:34:33 +0200 | [diff] [blame] | 592 | Options.getDefaults(SkylarkSemanticsOptions.class), |
Janak Ramakrishnan | 326c698 | 2016-09-27 14:58:26 +0000 | [diff] [blame] | 593 | "", |
| 594 | UUID.randomUUID(), |
| 595 | ImmutableMap.<String, String>of(), |
Damien Martin-Guillerez | 777f3af | 2017-02-08 17:22:02 +0000 | [diff] [blame] | 596 | ImmutableMap.<String, String>of(), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 597 | new TimestampGranularityMonitor(BlazeClock.instance())); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 598 | skyframeExecutor.invalidateFilesUnderPathForTesting( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 599 | new Reporter(new EventBus()), modifiedFileSet, workspace); |
| 600 | ((SequencedSkyframeExecutor) skyframeExecutor).handleDiffs(new Reporter(new EventBus())); |
Ulf Adams | 5d058c4 | 2015-12-09 16:22:01 +0000 | [diff] [blame] | 601 | |
| 602 | changes.clear(); |
| 603 | } |
| 604 | |
| 605 | Target getTarget(String targetName) |
| 606 | throws NoSuchPackageException, NoSuchTargetException, InterruptedException { |
| 607 | Label label = Label.parseAbsoluteUnchecked(targetName); |
| 608 | return skyframeExecutor.getPackageManager().getTarget(reporter, label); |
| 609 | } |
| 610 | } |
| 611 | } |