ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 1 | // Copyright 2017 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 | |
lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 17 | |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableSet; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 22 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
| 23 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 24 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.util.AnalysisMock; |
| 26 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
| 27 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 28 | import com.google.devtools.build.lib.packages.Package; |
brandjon | 674ab86 | 2017-10-06 23:17:33 +0200 | [diff] [blame] | 29 | import com.google.devtools.build.lib.packages.SkylarkSemanticsOptions; |
janakr | 5e606e6 | 2017-07-19 22:40:20 +0200 | [diff] [blame] | 30 | import com.google.devtools.build.lib.skyframe.BazelSkyframeExecutorConstants; |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 31 | import com.google.devtools.build.lib.skyframe.DiffAwareness; |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 32 | import com.google.devtools.build.lib.skyframe.SequencedSkyframeExecutor; |
| 33 | import com.google.devtools.build.lib.skyframe.SkyValueDirtinessChecker; |
| 34 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 35 | import com.google.devtools.build.lib.testutil.FoundationTestCase; |
| 36 | import com.google.devtools.build.lib.testutil.ManualClock; |
janakr | 150858b | 2017-07-25 00:04:53 +0200 | [diff] [blame] | 37 | import com.google.devtools.build.lib.testutil.TestConstants; |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 38 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 39 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 40 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 41 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
| 42 | import com.google.devtools.build.lib.vfs.Path; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 43 | import com.google.devtools.build.lib.vfs.Root; |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 44 | import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem; |
| 45 | import com.google.devtools.common.options.OptionsParser; |
| 46 | import java.nio.charset.StandardCharsets; |
| 47 | import java.util.UUID; |
| 48 | import java.util.logging.Level; |
| 49 | import java.util.logging.Logger; |
| 50 | import org.junit.Before; |
| 51 | import org.junit.Test; |
| 52 | import org.junit.runner.RunWith; |
| 53 | import org.junit.runners.JUnit4; |
| 54 | |
| 55 | /** |
| 56 | * Tests for package loading. |
| 57 | */ |
| 58 | @RunWith(JUnit4.class) |
| 59 | public class BuildFileModificationTest extends FoundationTestCase { |
| 60 | |
| 61 | private ManualClock clock = new ManualClock(); |
| 62 | private AnalysisMock analysisMock; |
| 63 | private ConfiguredRuleClassProvider ruleClassProvider; |
| 64 | private SkyframeExecutor skyframeExecutor; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 65 | private final ActionKeyContext actionKeyContext = new ActionKeyContext(); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 66 | |
| 67 | @Before |
| 68 | public final void disableLogging() throws Exception { |
| 69 | Logger.getLogger("com.google.devtools").setLevel(Level.SEVERE); |
| 70 | } |
| 71 | |
| 72 | @Before |
| 73 | public final void initializeSkyframeExecutor() throws Exception { |
| 74 | analysisMock = AnalysisMock.get(); |
| 75 | ruleClassProvider = analysisMock.createRuleClassProvider(); |
| 76 | BlazeDirectories directories = |
janakr | 3b63a4e | 2017-09-14 09:55:40 +0200 | [diff] [blame] | 77 | new BlazeDirectories( |
| 78 | new ServerDirectories(outputBase, outputBase), |
| 79 | rootDirectory, |
| 80 | analysisMock.getProductName()); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 81 | skyframeExecutor = |
janakr | 5e606e6 | 2017-07-19 22:40:20 +0200 | [diff] [blame] | 82 | SequencedSkyframeExecutor.create( |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 83 | analysisMock |
janakr | 52d05e8 | 2017-09-22 13:27:14 -0400 | [diff] [blame] | 84 | .getPackageFactoryBuilderForTesting(directories) |
nharmata | d922e65 | 2017-05-17 20:29:19 +0200 | [diff] [blame] | 85 | .build(ruleClassProvider, scratch.getFileSystem()), |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 86 | fileSystem, |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 87 | directories, |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 88 | actionKeyContext, |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 89 | null, /* workspaceStatusActionFactory */ |
| 90 | ruleClassProvider.getBuildInfoFactories(), |
| 91 | ImmutableList.<DiffAwareness.Factory>of(), |
janakr | 52d05e8 | 2017-09-22 13:27:14 -0400 | [diff] [blame] | 92 | analysisMock.getSkyFunctions(directories), |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 93 | ImmutableList.<SkyValueDirtinessChecker>of(), |
nharmata | e4eb23f | 2017-12-05 09:27:45 -0800 | [diff] [blame] | 94 | BazelSkyframeExecutorConstants.HARDCODED_BLACKLISTED_PACKAGE_PREFIXES, |
| 95 | BazelSkyframeExecutorConstants.ADDITIONAL_BLACKLISTED_PACKAGE_PREFIXES_FILE, |
janakr | 5e606e6 | 2017-07-19 22:40:20 +0200 | [diff] [blame] | 96 | BazelSkyframeExecutorConstants.CROSS_REPOSITORY_LABEL_VIOLATION_STRATEGY, |
| 97 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY, |
| 98 | BazelSkyframeExecutorConstants.ACTION_ON_IO_EXCEPTION_READING_BUILD_FILE); |
janakr | 150858b | 2017-07-25 00:04:53 +0200 | [diff] [blame] | 99 | TestConstants.processSkyframeExecutorForTesting(skyframeExecutor); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 100 | OptionsParser parser = OptionsParser.newOptionsParser( |
| 101 | PackageCacheOptions.class, SkylarkSemanticsOptions.class); |
| 102 | analysisMock.getInvocationPolicyEnforcer().enforce(parser); |
| 103 | setUpSkyframe( |
| 104 | parser.getOptions(PackageCacheOptions.class), |
| 105 | parser.getOptions(SkylarkSemanticsOptions.class)); |
| 106 | } |
| 107 | |
| 108 | private void setUpSkyframe( |
| 109 | PackageCacheOptions packageCacheOptions, |
| 110 | SkylarkSemanticsOptions skylarkSemanticsOptions) { |
John Cater | e0d1d0e | 2017-11-28 20:47:41 -0800 | [diff] [blame] | 111 | PathPackageLocator pkgLocator = |
| 112 | PathPackageLocator.create( |
| 113 | null, |
| 114 | packageCacheOptions.packagePath, |
| 115 | reporter, |
| 116 | rootDirectory, |
| 117 | rootDirectory, |
| 118 | BazelSkyframeExecutorConstants.BUILD_FILES_BY_PRIORITY); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 119 | packageCacheOptions.showLoadingProgress = true; |
| 120 | packageCacheOptions.globbingThreads = 7; |
| 121 | skyframeExecutor.preparePackageLoading( |
| 122 | pkgLocator, |
| 123 | packageCacheOptions, |
| 124 | skylarkSemanticsOptions, |
| 125 | analysisMock.getDefaultsPackageContent(), |
| 126 | UUID.randomUUID(), |
| 127 | ImmutableMap.<String, String>of(), |
| 128 | ImmutableMap.<String, String>of(), |
| 129 | new TimestampGranularityMonitor(clock)); |
| 130 | skyframeExecutor.setDeletedPackages( |
| 131 | ImmutableSet.copyOf(packageCacheOptions.getDeletedPackages())); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | protected FileSystem createFileSystem() { |
| 136 | return new InMemoryFileSystem(clock); |
| 137 | } |
| 138 | |
| 139 | private void invalidatePackages() throws InterruptedException { |
| 140 | skyframeExecutor.invalidateFilesUnderPathForTesting( |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 141 | reporter, ModifiedFileSet.EVERYTHING_MODIFIED, Root.fromPath(rootDirectory)); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private Package getPackage(String packageName) |
| 145 | throws NoSuchPackageException, InterruptedException { |
| 146 | return skyframeExecutor.getPackageManager().getPackage(reporter, |
| 147 | PackageIdentifier.createInMainRepo(packageName)); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void testCTimeChangeDetectedWithError() throws Exception { |
| 152 | reporter.removeHandler(failFastHandler); |
| 153 | Path build = scratch.file( |
| 154 | "a/BUILD", "cc_library(name='a', feet='stinky')".getBytes(StandardCharsets.ISO_8859_1)); |
| 155 | Package a1 = getPackage("a"); |
lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 156 | assertThat(a1.containsErrors()).isTrue(); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 157 | assertContainsEvent("//a:a: no such attribute 'feet'"); |
| 158 | eventCollector.clear(); |
| 159 | // writeContent updates mtime and ctime. Note that we keep the content length exactly the same. |
| 160 | clock.advanceMillis(1); |
| 161 | FileSystemUtils.writeContent( |
| 162 | build, "cc_library(name='a', srcs=['a.cc'])".getBytes(StandardCharsets.ISO_8859_1)); |
| 163 | |
| 164 | invalidatePackages(); |
| 165 | Package a2 = getPackage("a"); |
lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 166 | assertThat(a2).isNotSameAs(a1); |
| 167 | assertThat(a2.containsErrors()).isFalse(); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 168 | assertNoEvents(); |
| 169 | } |
| 170 | |
| 171 | @Test |
| 172 | public void testCTimeChangeDetected() throws Exception { |
| 173 | Path path = scratch.file( |
| 174 | "pkg/BUILD", "cc_library(name = 'foo')\n".getBytes(StandardCharsets.ISO_8859_1)); |
| 175 | Package oldPkg = getPackage("pkg"); |
| 176 | |
| 177 | // Note that the content has exactly the same length as before. |
| 178 | clock.advanceMillis(1); |
| 179 | FileSystemUtils.writeContent( |
| 180 | path, "cc_library(name = 'bar')\n".getBytes(StandardCharsets.ISO_8859_1)); |
lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 181 | assertThat(getPackage("pkg")) |
| 182 | .isSameAs(oldPkg); // Change only becomes visible after invalidatePackages. |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 183 | |
| 184 | invalidatePackages(); |
| 185 | |
| 186 | Package newPkg = getPackage("pkg"); |
lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 187 | assertThat(newPkg).isNotSameAs(oldPkg); |
| 188 | assertThat(newPkg.getTarget("bar")).isNotNull(); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void testLengthChangeDetected() throws Exception { |
| 193 | reporter.removeHandler(failFastHandler); |
| 194 | Path build = scratch.file( |
| 195 | "a/BUILD", "cc_library(name='a', srcs=['a.cc'])".getBytes(StandardCharsets.ISO_8859_1)); |
| 196 | Package a1 = getPackage("a"); |
| 197 | eventCollector.clear(); |
| 198 | // Note that we didn't advance the clock, so ctime/mtime is the same as before. |
| 199 | // However, the file contents are one byte longer. |
| 200 | FileSystemUtils.writeContent( |
| 201 | build, "cc_library(name='ab', srcs=['a.cc'])".getBytes(StandardCharsets.ISO_8859_1)); |
| 202 | |
| 203 | invalidatePackages(); |
| 204 | Package a2 = getPackage("a"); |
lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 205 | assertThat(a2).isNotSameAs(a1); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 206 | assertNoEvents(); |
| 207 | } |
| 208 | |
| 209 | @Test |
| 210 | public void testTouchedBuildFileCausesReloadAfterSync() throws Exception { |
| 211 | Path path = scratch.file("pkg/BUILD", |
| 212 | "cc_library(name = 'foo')"); |
| 213 | |
| 214 | Package oldPkg = getPackage("pkg"); |
| 215 | // Change ctime to 1. |
| 216 | clock.advanceMillis(1); |
| 217 | path.setLastModifiedTime(1001); |
lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 218 | assertThat(getPackage("pkg")).isSameAs(oldPkg); // change not yet visible |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 219 | |
| 220 | invalidatePackages(); |
| 221 | |
| 222 | Package newPkg = getPackage("pkg"); |
lberki | bf6ef0f | 2017-05-29 11:00:40 +0200 | [diff] [blame] | 223 | assertThat(newPkg).isNotSameAs(oldPkg); |
ulfjack | 4f647e8 | 2017-05-09 07:45:40 -0400 | [diff] [blame] | 224 | } |
| 225 | } |