1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.skyframe;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelValidator;
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
import com.google.devtools.build.lib.packages.BuildFileNotFoundException;
import com.google.devtools.build.lib.packages.ErrorDeterminingRepositoryException;
import com.google.devtools.build.lib.packages.NoSuchPackageException;
import com.google.devtools.build.lib.pkgcache.PathPackageLocator;
import com.google.devtools.build.lib.skyframe.PackageLookupValue.BuildFileName;
import com.google.devtools.build.lib.syntax.EvalException;
import com.google.devtools.build.lib.util.Preconditions;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.lib.vfs.RootedPath;
import com.google.devtools.build.skyframe.SkyFunction;
import com.google.devtools.build.skyframe.SkyFunctionException;
import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
/**
* SkyFunction for {@link PackageLookupValue}s.
*/
public class PackageLookupFunction implements SkyFunction {
/** Lists possible ways to handle a package label which crosses into a new repository. */
public enum CrossRepositoryLabelViolationStrategy {
/** Ignore the violation. */
IGNORE,
/** Generate an error. */
ERROR;
}
private final AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages;
private final CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy;
private final List<BuildFileName> buildFilesByPriority;
public PackageLookupFunction(
AtomicReference<ImmutableSet<PackageIdentifier>> deletedPackages,
CrossRepositoryLabelViolationStrategy crossRepositoryLabelViolationStrategy,
List<BuildFileName> buildFilesByPriority) {
this.deletedPackages = deletedPackages;
this.crossRepositoryLabelViolationStrategy = crossRepositoryLabelViolationStrategy;
this.buildFilesByPriority = buildFilesByPriority;
}
@Override
public SkyValue compute(SkyKey skyKey, Environment env)
throws PackageLookupFunctionException, InterruptedException {
PathPackageLocator pkgLocator = PrecomputedValue.PATH_PACKAGE_LOCATOR.get(env);
PackageIdentifier packageKey = (PackageIdentifier) skyKey.argument();
if (PackageFunction.isDefaultsPackage(packageKey)) {
return PackageLookupValue.success(pkgLocator.getPathEntries().get(0), BuildFileName.BUILD);
}
if (!packageKey.getRepository().isMain()) {
return computeExternalPackageLookupValue(skyKey, env, packageKey);
} else if (packageKey.equals(Label.EXTERNAL_PACKAGE_IDENTIFIER)) {
return computeWorkspacePackageLookupValue(env, pkgLocator.getPathEntries());
}
String packageNameErrorMsg = LabelValidator.validatePackageName(
packageKey.getPackageFragment().getPathString());
if (packageNameErrorMsg != null) {
return PackageLookupValue.invalidPackageName("Invalid package name '" + packageKey + "': "
+ packageNameErrorMsg);
}
if (deletedPackages.get().contains(packageKey)) {
return PackageLookupValue.DELETED_PACKAGE_VALUE;
}
BlacklistedPackagePrefixesValue blacklistedPatternsValue =
(BlacklistedPackagePrefixesValue) env.getValue(BlacklistedPackagePrefixesValue.key());
if (blacklistedPatternsValue == null) {
return null;
}
PathFragment buildFileFragment = packageKey.getPackageFragment();
for (PathFragment pattern : blacklistedPatternsValue.getPatterns()) {
if (buildFileFragment.startsWith(pattern)) {
return PackageLookupValue.DELETED_PACKAGE_VALUE;
}
}
return findPackageByBuildFile(env, pkgLocator, packageKey);
}
@Nullable
@Override
public String extractTag(SkyKey skyKey) {
return null;
}
@Nullable
private PackageLookupValue findPackageByBuildFile(
Environment env, PathPackageLocator pkgLocator, PackageIdentifier packageKey)
throws PackageLookupFunctionException, InterruptedException {
// TODO(bazel-team): The following is O(n^2) on the number of elements on the package path due
// to having restart the SkyFunction after every new dependency. However, if we try to batch
// the missing value keys, more dependencies than necessary will be declared. This wart can be
// fixed once we have nicer continuation support [skyframe-loading]
for (Path packagePathEntry : pkgLocator.getPathEntries()) {
// This checks for the build file names in the correct precedence order.
for (BuildFileName buildFileName : buildFilesByPriority) {
PackageLookupValue result =
getPackageLookupValue(env, packagePathEntry, packageKey, buildFileName);
if (result == null) {
return null;
}
if (result != PackageLookupValue.NO_BUILD_FILE_VALUE) {
return result;
}
}
}
return PackageLookupValue.NO_BUILD_FILE_VALUE;
}
@Nullable
private static FileValue getFileValue(
RootedPath fileRootedPath, Environment env, PackageIdentifier packageIdentifier)
throws PackageLookupFunctionException, InterruptedException {
String basename = fileRootedPath.asPath().getBaseName();
SkyKey fileSkyKey = FileValue.key(fileRootedPath);
FileValue fileValue = null;
try {
fileValue = (FileValue) env.getValueOrThrow(fileSkyKey, IOException.class,
FileSymlinkException.class, InconsistentFilesystemException.class);
} catch (IOException e) {
// TODO(bazel-team): throw an IOException here and let PackageFunction wrap that into a
// BuildFileNotFoundException.
throw new PackageLookupFunctionException(new BuildFileNotFoundException(packageIdentifier,
"IO errors while looking for " + basename + " file reading "
+ fileRootedPath.asPath() + ": " + e.getMessage(), e),
Transience.PERSISTENT);
} catch (FileSymlinkException e) {
throw new PackageLookupFunctionException(new BuildFileNotFoundException(packageIdentifier,
"Symlink cycle detected while trying to find " + basename + " file "
+ fileRootedPath.asPath()),
Transience.PERSISTENT);
} catch (InconsistentFilesystemException e) {
// This error is not transient from the perspective of the PackageLookupFunction.
throw new PackageLookupFunctionException(e, Transience.PERSISTENT);
}
return fileValue;
}
private PackageLookupValue getPackageLookupValue(
Environment env,
ImmutableList<Path> packagePathEntries,
PackageIdentifier packageIdentifier,
BuildFileName buildFileName)
throws PackageLookupFunctionException, InterruptedException {
// TODO(bazel-team): The following is O(n^2) on the number of elements on the package path due
// to having restart the SkyFunction after every new dependency. However, if we try to batch
// the missing value keys, more dependencies than necessary will be declared. This wart can be
// fixed once we have nicer continuation support [skyframe-loading]
for (Path packagePathEntry : packagePathEntries) {
PackageLookupValue result =
getPackageLookupValue(env, packagePathEntry, packageIdentifier, buildFileName);
if (result == null) {
return null;
}
if (result != PackageLookupValue.NO_BUILD_FILE_VALUE) {
return result;
}
}
return PackageLookupValue.NO_BUILD_FILE_VALUE;
}
private PackageLookupValue getPackageLookupValue(
Environment env,
Path packagePathEntry,
PackageIdentifier packageIdentifier,
BuildFileName buildFileName)
throws InterruptedException, PackageLookupFunctionException {
PathFragment buildFileFragment = buildFileName.getBuildFileFragment(packageIdentifier);
RootedPath buildFileRootedPath = RootedPath.toRootedPath(packagePathEntry, buildFileFragment);
if (crossRepositoryLabelViolationStrategy == CrossRepositoryLabelViolationStrategy.ERROR) {
// Is this path part of a local repository?
RootedPath currentPath =
RootedPath.toRootedPath(packagePathEntry, buildFileFragment.getParentDirectory());
SkyKey repositoryLookupKey = LocalRepositoryLookupValue.key(currentPath);
// TODO(jcater): Consider parallelizing these lookups.
LocalRepositoryLookupValue localRepository;
try {
localRepository =
(LocalRepositoryLookupValue)
env.getValueOrThrow(repositoryLookupKey, ErrorDeterminingRepositoryException.class);
if (localRepository == null) {
return null;
}
} catch (ErrorDeterminingRepositoryException e) {
// If the directory selected isn't part of a repository, that's an error.
// TODO(katre): Improve the error message given here.
throw new PackageLookupFunctionException(
new BuildFileNotFoundException(
packageIdentifier,
"Unable to determine the local repository for directory "
+ currentPath.asPath().getPathString()),
Transience.PERSISTENT);
}
if (localRepository.exists()
&& !localRepository.getRepository().equals(packageIdentifier.getRepository())) {
// There is a repository mismatch, this is an error.
// TODO(jcater): Work out the correct package name for this error message.
return PackageLookupValue.invalidPackageName(
"Package crosses into repository " + localRepository.getRepository().getName());
}
// There's no local repository, keep going.
} else {
// Future-proof against adding future values to CrossRepositoryLabelViolationStrategy.
Preconditions.checkState(
crossRepositoryLabelViolationStrategy == CrossRepositoryLabelViolationStrategy.IGNORE,
crossRepositoryLabelViolationStrategy);
}
// Check for the existence of the build file.
FileValue fileValue = getFileValue(buildFileRootedPath, env, packageIdentifier);
if (fileValue == null) {
return null;
}
if (fileValue.isFile()) {
return PackageLookupValue.success(buildFileRootedPath.getRoot(), buildFileName);
}
return PackageLookupValue.NO_BUILD_FILE_VALUE;
}
private PackageLookupValue computeWorkspacePackageLookupValue(
Environment env, ImmutableList<Path> packagePathEntries)
throws PackageLookupFunctionException, InterruptedException {
PackageLookupValue result =
getPackageLookupValue(
env, packagePathEntries, Label.EXTERNAL_PACKAGE_IDENTIFIER, BuildFileName.WORKSPACE);
if (result == null) {
return null;
}
if (result.packageExists()) {
return result;
}
// Fall back on the last package path entry if there were any and nothing else worked.
// TODO(kchodorow): get rid of this, the semantics are wrong (successful package lookup should
// mean the package exists). a bunch of tests need to be rewritten first though.
if (packagePathEntries.isEmpty()) {
return PackageLookupValue.NO_BUILD_FILE_VALUE;
}
Path lastPackagePath = packagePathEntries.get(packagePathEntries.size() - 1);
FileValue lastPackagePackagePathFileValue = getFileValue(
RootedPath.toRootedPath(lastPackagePath, PathFragment.EMPTY_FRAGMENT),
env,
Label.EXTERNAL_PACKAGE_IDENTIFIER);
if (lastPackagePackagePathFileValue == null) {
return null;
}
return lastPackagePackagePathFileValue.exists()
? PackageLookupValue.success(lastPackagePath, BuildFileName.WORKSPACE)
: PackageLookupValue.NO_BUILD_FILE_VALUE;
}
/**
* Gets a PackageLookupValue from a different Bazel repository.
*
* <p>To do this, it looks up the "external" package and finds a path mapping for the repository
* name.
*/
private PackageLookupValue computeExternalPackageLookupValue(
SkyKey skyKey, Environment env, PackageIdentifier packageIdentifier)
throws PackageLookupFunctionException, InterruptedException {
PackageIdentifier id = (PackageIdentifier) skyKey.argument();
SkyKey repositoryKey = RepositoryValue.key(id.getRepository());
RepositoryValue repositoryValue;
try {
repositoryValue = (RepositoryValue) env.getValueOrThrow(
repositoryKey, NoSuchPackageException.class, IOException.class, EvalException.class);
if (repositoryValue == null) {
return null;
}
} catch (NoSuchPackageException | IOException | EvalException e) {
throw new PackageLookupFunctionException(new BuildFileNotFoundException(id, e.getMessage()),
Transience.PERSISTENT);
}
// This checks for the build file names in the correct precedence order.
for (BuildFileName buildFileName : buildFilesByPriority) {
PathFragment buildFileFragment =
id.getPackageFragment().getChild(buildFileName.getFilename());
RootedPath buildFileRootedPath =
RootedPath.toRootedPath(repositoryValue.getPath(), buildFileFragment);
FileValue fileValue = getFileValue(buildFileRootedPath, env, packageIdentifier);
if (fileValue == null) {
return null;
}
if (fileValue.isFile()) {
return PackageLookupValue.success(repositoryValue.getPath(), buildFileName);
}
}
return PackageLookupValue.NO_BUILD_FILE_VALUE;
}
/**
* Used to declare all the exception types that can be wrapped in the exception thrown by
* {@link PackageLookupFunction#compute}.
*/
private static final class PackageLookupFunctionException extends SkyFunctionException {
public PackageLookupFunctionException(BuildFileNotFoundException e, Transience transience) {
super(e, transience);
}
public PackageLookupFunctionException(InconsistentFilesystemException e,
Transience transience) {
super(e, transience);
}
}
}
|