Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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.skyframe; |
| 15 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 17 | import com.google.common.collect.Sets; |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 18 | import com.google.common.flogger.GoogleLogger; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
brandjon | b282530 | 2021-03-16 12:35:36 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider.BundledFileSystem; |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.cmdline.LabelConstants; |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 22 | import com.google.devtools.build.lib.cmdline.RepositoryName; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; |
| 24 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Damien Martin-Guillerez | 847a418 | 2016-02-10 12:44:25 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.rules.repository.RepositoryFunction; |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 27 | import com.google.devtools.build.lib.util.Pair; |
janakr | 2143cf8 | 2020-05-06 16:20:37 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.util.TestType; |
Ulf Adams | ef7e045 | 2015-12-21 09:26:43 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.vfs.Path; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.vfs.RootedPath; |
| 31 | import com.google.devtools.build.skyframe.SkyFunction; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 32 | import java.io.IOException; |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 33 | import java.util.Set; |
Nathan Harmata | fee1928 | 2017-02-14 20:47:50 +0000 | [diff] [blame] | 34 | import java.util.concurrent.atomic.AtomicInteger; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | import java.util.concurrent.atomic.AtomicReference; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 37 | /** Common utilities for dealing with paths outside the package roots. */ |
Nathan Harmata | 029de3d | 2015-07-27 18:08:09 +0000 | [diff] [blame] | 38 | public class ExternalFilesHelper { |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 39 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
Nathan Harmata | fee1928 | 2017-02-14 20:47:50 +0000 | [diff] [blame] | 40 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | private final AtomicReference<PathPackageLocator> pkgLocator; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 42 | private final ExternalFileAction externalFileAction; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 43 | private final BlazeDirectories directories; |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 44 | private final int maxNumExternalFilesToLog; |
Nathan Harmata | fee1928 | 2017-02-14 20:47:50 +0000 | [diff] [blame] | 45 | private final AtomicInteger numExternalFilesLogged = new AtomicInteger(0); |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 46 | private static final int MAX_EXTERNAL_FILES_TO_TRACK = 2500; |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 47 | |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 48 | // These variables are set to true from multiple threads, but only read in the main thread. |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 49 | // So volatility or an AtomicBoolean is not needed. |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 50 | private boolean anyOutputFilesSeen = false; |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 51 | private boolean tooManyNonOutputExternalFilesSeen = false; |
| 52 | private boolean anyFilesInExternalReposSeen = false; |
Janak Ramakrishnan | 8343106 | 2015-12-08 18:42:16 +0000 | [diff] [blame] | 53 | |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 54 | // This is a set of external files that are not in external repositories. |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 55 | private Set<RootedPath> nonOutputExternalFilesSeen = Sets.newConcurrentHashSet(); |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 56 | |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 57 | private ExternalFilesHelper( |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 58 | AtomicReference<PathPackageLocator> pkgLocator, |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 59 | ExternalFileAction externalFileAction, |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 60 | BlazeDirectories directories, |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 61 | int maxNumExternalFilesToLog) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | this.pkgLocator = pkgLocator; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 63 | this.externalFileAction = externalFileAction; |
Kristina Chodorow | 5a2936f | 2016-04-22 17:02:19 +0000 | [diff] [blame] | 64 | this.directories = directories; |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 65 | this.maxNumExternalFilesToLog = maxNumExternalFilesToLog; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 66 | } |
| 67 | |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 68 | public static ExternalFilesHelper create( |
| 69 | AtomicReference<PathPackageLocator> pkgLocator, |
| 70 | ExternalFileAction externalFileAction, |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 71 | BlazeDirectories directories) { |
janakr | 2143cf8 | 2020-05-06 16:20:37 -0700 | [diff] [blame] | 72 | return TestType.isInTest() |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 73 | ? createForTesting(pkgLocator, externalFileAction, directories) |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 74 | : new ExternalFilesHelper( |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 75 | pkgLocator, externalFileAction, directories, /*maxNumExternalFilesToLog=*/ 100); |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public static ExternalFilesHelper createForTesting( |
| 79 | AtomicReference<PathPackageLocator> pkgLocator, |
| 80 | ExternalFileAction externalFileAction, |
| 81 | BlazeDirectories directories) { |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 82 | return new ExternalFilesHelper( |
| 83 | pkgLocator, |
| 84 | externalFileAction, |
| 85 | directories, |
| 86 | // These log lines are mostly spam during unit and integration tests. |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 87 | /*maxNumExternalFilesToLog=*/ 0); |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 91 | /** |
| 92 | * The action to take when an external path is encountered. See {@link FileType} for the |
| 93 | * definition of "external". |
| 94 | */ |
| 95 | public enum ExternalFileAction { |
| 96 | /** |
| 97 | * For paths of type {@link FileType#EXTERNAL_REPO}, introduce a Skyframe dependency on the |
| 98 | * 'external' package. |
| 99 | * |
| 100 | * <p>This is the default for Bazel, since it's required for correctness of the external |
| 101 | * repositories feature. |
| 102 | */ |
| 103 | DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, |
Nathan Harmata | 0c7a42a | 2016-10-13 18:17:48 +0000 | [diff] [blame] | 104 | |
| 105 | /** |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 106 | * For paths of type {@link FileType#EXTERNAL} or {@link FileType#OUTPUT}, assume the path does |
| 107 | * not exist and will never exist. |
| 108 | */ |
| 109 | ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS, |
| 110 | } |
| 111 | |
| 112 | /** Classification of a path encountered by Bazel. */ |
| 113 | public enum FileType { |
brandjon | b282530 | 2021-03-16 12:35:36 -0700 | [diff] [blame] | 114 | /** |
| 115 | * A path to a file located inside of Bazel, e.g. the bundled builtins_bzl root (for {@code |
| 116 | * --experimental_builtins_bzl_root=%bundled%}) that live in an InMemoryFileSystem. These files |
| 117 | * must not change throughout the lifetime of the server. |
| 118 | */ |
| 119 | BUNDLED, |
| 120 | |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 121 | /** A path inside the package roots. */ |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 122 | INTERNAL, |
| 123 | |
| 124 | /** |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 125 | * A non {@link #EXTERNAL_REPO} path outside the package roots about which we may make no other |
| 126 | * assumptions. |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 127 | */ |
| 128 | EXTERNAL, |
| 129 | |
| 130 | /** |
| 131 | * A path in Bazel's output tree that's a proper output of an action (*not* a source file in an |
| 132 | * external repository). Such files are theoretically mutable, but certain Bazel flags may tell |
| 133 | * Bazel to assume these paths are immutable. |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 134 | * |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 135 | * <p>Note that {@link ExternalFilesHelper#maybeHandleExternalFile} is only used for {@link |
jcater | 02ce2a6 | 2020-04-07 08:06:00 -0700 | [diff] [blame] | 136 | * com.google.devtools.build.lib.actions.FileStateValue} and {@link DirectoryListingStateValue}, |
| 137 | * and also note that output files do not normally have corresponding {@link |
| 138 | * com.google.devtools.build.lib.actions.FileValue} instances (and thus also {@link |
| 139 | * com.google.devtools.build.lib.actions.FileStateValue} instances) in the Skyframe graph |
| 140 | * ({@link ArtifactFunction} only uses {@link com.google.devtools.build.lib.actions.FileValue}s |
| 141 | * for source files). But {@link com.google.devtools.build.lib.actions.FileStateValue}s for |
| 142 | * output files can still make their way into the Skyframe graph if e.g. a source file is a |
| 143 | * symlink to an output file. |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 144 | */ |
| 145 | // TODO(nharmata): Consider an alternative design where we have an OutputFileDiffAwareness. This |
| 146 | // could work but would first require that we clean up all RootedPath usage. |
| 147 | OUTPUT, |
| 148 | |
| 149 | /** |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 150 | * A path in the part of Bazel's output tree that contains (/ symlinks to) to external |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 151 | * repositories. Every such path under the external repository is generated by the execution of |
| 152 | * the corresponding repository rule, so these paths should not be cached by Skyframe before the |
| 153 | * RepositoryDirectoryValue is computed. |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 154 | */ |
| 155 | EXTERNAL_REPO, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 158 | /** |
| 159 | * Thrown by {@link #maybeHandleExternalFile} when an applicable path is processed (see |
| 160 | * {@link ExternalFileAction#ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS}. |
| 161 | */ |
| 162 | static class NonexistentImmutableExternalFileException extends Exception { |
| 163 | } |
| 164 | |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 165 | static class ExternalFilesKnowledge { |
| 166 | final boolean anyOutputFilesSeen; |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 167 | final Set<RootedPath> nonOutputExternalFilesSeen; |
| 168 | final boolean anyFilesInExternalReposSeen; |
| 169 | final boolean tooManyNonOutputExternalFilesSeen; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 170 | |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 171 | private ExternalFilesKnowledge( |
| 172 | boolean anyOutputFilesSeen, |
| 173 | Set<RootedPath> nonOutputExternalFilesSeen, |
| 174 | boolean anyFilesInExternalReposSeen, |
| 175 | boolean tooManyNonOutputExternalFilesSeen) { |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 176 | this.anyOutputFilesSeen = anyOutputFilesSeen; |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 177 | this.nonOutputExternalFilesSeen = nonOutputExternalFilesSeen; |
| 178 | this.anyFilesInExternalReposSeen = anyFilesInExternalReposSeen; |
| 179 | this.tooManyNonOutputExternalFilesSeen = tooManyNonOutputExternalFilesSeen; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | @ThreadCompatible |
| 184 | ExternalFilesKnowledge getExternalFilesKnowledge() { |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 185 | return new ExternalFilesKnowledge( |
| 186 | anyOutputFilesSeen, |
| 187 | nonOutputExternalFilesSeen, |
| 188 | anyFilesInExternalReposSeen, |
| 189 | tooManyNonOutputExternalFilesSeen); |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @ThreadCompatible |
| 193 | void setExternalFilesKnowledge(ExternalFilesKnowledge externalFilesKnowledge) { |
| 194 | anyOutputFilesSeen = externalFilesKnowledge.anyOutputFilesSeen; |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 195 | nonOutputExternalFilesSeen = externalFilesKnowledge.nonOutputExternalFilesSeen; |
| 196 | anyFilesInExternalReposSeen = externalFilesKnowledge.anyFilesInExternalReposSeen; |
| 197 | tooManyNonOutputExternalFilesSeen = externalFilesKnowledge.tooManyNonOutputExternalFilesSeen; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | ExternalFilesHelper cloneWithFreshExternalFilesKnowledge() { |
nharmata | 3fb7d34 | 2018-02-23 11:37:51 -0800 | [diff] [blame] | 201 | return new ExternalFilesHelper( |
Googler | cbf8159 | 2022-06-21 05:00:30 -0700 | [diff] [blame] | 202 | pkgLocator, externalFileAction, directories, maxNumExternalFilesToLog); |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 205 | public FileType getAndNoteFileType(RootedPath rootedPath) { |
| 206 | return getFileTypeAndRepository(rootedPath).getFirst(); |
| 207 | } |
| 208 | |
| 209 | private Pair<FileType, RepositoryName> getFileTypeAndRepository(RootedPath rootedPath) { |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 210 | FileType fileType = detectFileType(rootedPath); |
steinman | 04dff44 | 2021-09-27 08:18:05 -0700 | [diff] [blame] | 211 | if (FileType.EXTERNAL == fileType) { |
| 212 | if (nonOutputExternalFilesSeen.size() >= MAX_EXTERNAL_FILES_TO_TRACK) { |
| 213 | tooManyNonOutputExternalFilesSeen = true; |
| 214 | } else { |
| 215 | nonOutputExternalFilesSeen.add(rootedPath); |
| 216 | } |
| 217 | } |
| 218 | if (FileType.EXTERNAL_REPO == fileType) { |
| 219 | anyFilesInExternalReposSeen = true; |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 220 | } |
| 221 | if (FileType.OUTPUT == fileType) { |
| 222 | anyOutputFilesSeen = true; |
| 223 | } |
| 224 | return Pair.of(fileType, null); |
| 225 | } |
| 226 | |
brandjon | b282530 | 2021-03-16 12:35:36 -0700 | [diff] [blame] | 227 | /** |
| 228 | * Returns the classification of a path, and updates this instance's state regarding what kinds of |
| 229 | * paths have been seen. |
| 230 | */ |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 231 | private FileType detectFileType(RootedPath rootedPath) { |
brandjon | b282530 | 2021-03-16 12:35:36 -0700 | [diff] [blame] | 232 | if (rootedPath.getRoot().getFileSystem() instanceof BundledFileSystem) { |
| 233 | return FileType.BUNDLED; |
| 234 | } |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 235 | PathPackageLocator packageLocator = pkgLocator.get(); |
| 236 | if (packageLocator.getPathEntries().contains(rootedPath.getRoot())) { |
| 237 | return FileType.INTERNAL; |
| 238 | } |
| 239 | // The outputBase may be null if we're not actually running a build. |
| 240 | Path outputBase = packageLocator.getOutputBase(); |
| 241 | if (outputBase == null) { |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 242 | return FileType.EXTERNAL; |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 243 | } |
| 244 | if (rootedPath.asPath().startsWith(outputBase)) { |
lberki | 015f586 | 2020-02-20 10:08:29 -0800 | [diff] [blame] | 245 | Path externalRepoDir = outputBase.getRelative(LabelConstants.EXTERNAL_REPOSITORY_LOCATION); |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 246 | if (rootedPath.asPath().startsWith(externalRepoDir)) { |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 247 | return FileType.EXTERNAL_REPO; |
| 248 | } else { |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 249 | return FileType.OUTPUT; |
| 250 | } |
| 251 | } |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 252 | return FileType.EXTERNAL; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 253 | } |
| 254 | |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 255 | /** |
Laszlo Csomor | dda67a2 | 2019-02-20 23:37:05 -0800 | [diff] [blame] | 256 | * If this instance is configured with {@link |
| 257 | * ExternalFileAction#DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS} and {@code rootedPath} isn't |
| 258 | * under a package root then this adds a dependency on the //external package. If the action is |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 259 | * {@link ExternalFileAction#ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS}, it will throw |
| 260 | * a {@link NonexistentImmutableExternalFileException} instead. |
Michajlo Matijkiw | db11094 | 2015-03-31 23:41:02 +0000 | [diff] [blame] | 261 | */ |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 262 | @ThreadSafe |
wyv | 9732d23 | 2021-05-20 02:57:24 -0700 | [diff] [blame] | 263 | FileType maybeHandleExternalFile(RootedPath rootedPath, SkyFunction.Environment env) |
Nathan Harmata | d4f7594 | 2016-10-18 08:55:17 +0000 | [diff] [blame] | 264 | throws NonexistentImmutableExternalFileException, IOException, InterruptedException { |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 265 | Pair<FileType, RepositoryName> pair = getFileTypeAndRepository(rootedPath); |
| 266 | |
| 267 | FileType fileType = Preconditions.checkNotNull(pair.getFirst()); |
| 268 | switch (fileType) { |
brandjon | b282530 | 2021-03-16 12:35:36 -0700 | [diff] [blame] | 269 | case BUNDLED: |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 270 | case INTERNAL: |
| 271 | break; |
| 272 | case EXTERNAL: |
| 273 | if (numExternalFilesLogged.incrementAndGet() < maxNumExternalFilesToLog) { |
janakr | c3bcb98 | 2020-04-14 06:50:08 -0700 | [diff] [blame] | 274 | logger.atInfo().log("Encountered an external path %s", rootedPath); |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 275 | } |
| 276 | // fall through |
| 277 | case OUTPUT: |
| 278 | if (externalFileAction |
| 279 | == ExternalFileAction.ASSUME_NON_EXISTENT_AND_IMMUTABLE_FOR_EXTERNAL_PATHS) { |
| 280 | throw new NonexistentImmutableExternalFileException(); |
| 281 | } |
| 282 | break; |
| 283 | case EXTERNAL_REPO: |
| 284 | Preconditions.checkState( |
| 285 | externalFileAction == ExternalFileAction.DEPEND_ON_EXTERNAL_PKG_FOR_EXTERNAL_REPO_PATHS, |
| 286 | externalFileAction); |
wyv | 9732d23 | 2021-05-20 02:57:24 -0700 | [diff] [blame] | 287 | RepositoryFunction.addExternalFilesDependencies(rootedPath, directories, env); |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 288 | break; |
Kristina Chodorow | f9fdc8d | 2015-12-08 12:49:31 +0000 | [diff] [blame] | 289 | } |
Googler | ba4862d | 2019-05-03 06:13:23 -0700 | [diff] [blame] | 290 | return fileType; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 291 | } |
| 292 | } |