|
20 | 20 | import com.google.common.collect.ImmutableCollection;
|
21 | 21 | import com.google.common.collect.ImmutableList;
|
22 | 22 | import com.google.common.collect.ImmutableMap;
|
23 |
| -import com.google.common.collect.ImmutableMultimap; |
24 | 23 | import com.google.common.collect.ImmutableSet;
|
25 | 24 | import com.google.common.collect.LinkedHashMultimap;
|
26 | 25 | import com.google.common.collect.Lists;
|
|
62 | 61 | import java.util.List;
|
63 | 62 | import java.util.Map;
|
64 | 63 | import java.util.logging.Logger;
|
| 64 | +import java.util.stream.Collectors; |
65 | 65 | import javax.annotation.Nullable;
|
66 | 66 |
|
67 | 67 | /**
|
@@ -230,18 +230,12 @@ private SkylarkImportLookupValue computeInternal(
|
230 | 230 |
|
231 | 231 | // Process the load statements in the file.
|
232 | 232 | ImmutableList<SkylarkImport> imports = ast.getImports();
|
233 |
| - ImmutableMap<String, Label> labelsForImports; |
234 |
| - |
235 |
| - // Find the labels corresponding to the load statements. |
236 |
| - labelsForImports = findLabelsForLoadStatements(imports, fileLabel, env); |
237 |
| - if (labelsForImports == null) { |
238 |
| - return null; |
239 |
| - } |
| 233 | + ImmutableMap<String, Label> labelsForImports = getLabelsForLoadStatements(imports, fileLabel); |
| 234 | + ImmutableCollection<Label> importLabels = labelsForImports.values(); |
240 | 235 |
|
241 | 236 | // Look up and load the imports.
|
242 |
| - ImmutableCollection<Label> importLabels = labelsForImports.values(); |
243 | 237 | List<SkyKey> importLookupKeys =
|
244 |
| - Lists.newArrayListWithExpectedSize(importLabels.size()); |
| 238 | + Lists.newArrayListWithExpectedSize(labelsForImports.size()); |
245 | 239 | for (Label importLabel : importLabels) {
|
246 | 240 | importLookupKeys.add(SkylarkImportLookupValue.key(importLabel, inWorkspace));
|
247 | 241 | }
|
@@ -297,7 +291,7 @@ private SkylarkImportLookupValue computeInternal(
|
297 | 291 | // Process the loaded imports.
|
298 | 292 | Map<String, Extension> extensionsForImports = Maps.newHashMapWithExpectedSize(imports.size());
|
299 | 293 | ImmutableList.Builder<SkylarkFileDependency> fileDependencies =
|
300 |
| - ImmutableList.builderWithExpectedSize(labelsForImports.size()); |
| 294 | + ImmutableList.builderWithExpectedSize(importLabels.size()); |
301 | 295 | for (Map.Entry<String, Label> importEntry : labelsForImports.entrySet()) {
|
302 | 296 | String importString = importEntry.getKey();
|
303 | 297 | Label importLabel = importEntry.getValue();
|
@@ -399,53 +393,22 @@ private static ImmutableMap<PathFragment, Label> labelsForAbsoluteImports(
|
399 | 393 | }
|
400 | 394 |
|
401 | 395 | /**
|
402 |
| - * Computes the set of {@link Label}s corresponding to a set of Skylark {@link LoadStatement}s. |
| 396 | + * Given a collection of {@link SkylarkImport}, returns a map from import string to label of |
| 397 | + * imported file. |
403 | 398 | *
|
404 | 399 | * @param imports a collection of Skylark {@link LoadStatement}s
|
405 | 400 | * @param containingFileLabel the {@link Label} of the file containing the load statements
|
406 |
| - * @return an {@link ImmutableMap} which maps a {@link String} used in the load statement to its |
407 |
| - * corresponding {@Label}. Returns {@code null} if any Skyframe dependencies are unavailable. |
408 |
| - * @throws SkylarkImportFailedException if no package can be found that contains the loaded file |
409 | 401 | */
|
410 | 402 | @Nullable
|
411 |
| - static ImmutableMap<String, Label> findLabelsForLoadStatements( |
412 |
| - ImmutableCollection<SkylarkImport> imports, Label containingFileLabel, Environment env) |
413 |
| - throws SkylarkImportFailedException, InterruptedException { |
| 403 | + static ImmutableMap<String, Label> getLabelsForLoadStatements( |
| 404 | + ImmutableCollection<SkylarkImport> imports, Label containingFileLabel) { |
414 | 405 | Preconditions.checkArgument(
|
415 | 406 | !containingFileLabel.getPackageIdentifier().getRepository().isDefault());
|
416 |
| - Map<String, Label> outputMap = Maps.newHashMapWithExpectedSize(imports.size()); |
417 |
| - |
418 |
| - // Filter relative vs. absolute paths. |
419 |
| - ImmutableSet.Builder<PathFragment> absoluteImportsToLookup = new ImmutableSet.Builder<>(); |
420 |
| - // We maintain a multimap from path fragments to their correspond import strings, to cover the |
421 |
| - // (unlikely) case where two distinct import strings generate the same path fragment. |
422 |
| - ImmutableMultimap.Builder<PathFragment, String> pathToImports = |
423 |
| - new ImmutableMultimap.Builder<>(); |
424 |
| - for (SkylarkImport imp : imports) { |
425 |
| - if (imp.hasAbsolutePath()) { |
426 |
| - absoluteImportsToLookup.add(imp.getAbsolutePath()); |
427 |
| - pathToImports.put(imp.getAbsolutePath(), imp.getImportString()); |
428 |
| - } else { |
429 |
| - outputMap.put(imp.getImportString(), imp.getLabel(containingFileLabel)); |
430 |
| - } |
431 |
| - } |
432 |
| - |
433 |
| - // Look up labels for absolute paths. |
434 |
| - ImmutableMap<PathFragment, Label> absoluteLabels = |
435 |
| - labelsForAbsoluteImports(absoluteImportsToLookup.build(), env); |
436 |
| - if (absoluteLabels == null) { |
437 |
| - return null; |
438 |
| - } |
439 |
| - for (Map.Entry<PathFragment, Label> entry : absoluteLabels.entrySet()) { |
440 |
| - PathFragment currPath = entry.getKey(); |
441 |
| - Label currLabel = entry.getValue(); |
442 |
| - for (String importString : pathToImports.build().get(currPath)) { |
443 |
| - outputMap.put(importString, currLabel); |
444 |
| - } |
445 |
| - } |
446 |
| - |
447 |
| - ImmutableMap<String, Label> immutableOutputMap = ImmutableMap.copyOf(outputMap); |
448 |
| - return immutableOutputMap; |
| 407 | + return ImmutableMap.copyOf(imports.stream().collect( |
| 408 | + Collectors.toMap( |
| 409 | + SkylarkImport::getImportString, |
| 410 | + imp -> imp.getLabel(containingFileLabel), |
| 411 | + (oldLabel, newLabel) -> oldLabel))); |
449 | 412 | }
|
450 | 413 |
|
451 | 414 | /**
|
|
0 commit comments