Skip to content

Commit 11642e9

Browse files
haxorzCopybara-Service
authored and
Copybara-Service
committed
Delete supporting code that is now dead after the
--incompatible_load_argument_is_label flag was removed in 940dbc5. Also simplify existing code. RELNOTES: None PiperOrigin-RevId: 216464695
1 parent b5dfe58 commit 11642e9

File tree

8 files changed

+31
-192
lines changed

8 files changed

+31
-192
lines changed

src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,6 @@ static SkylarkImportResult fetchImportsFromBuildFile(
561561
ImmutableList<SkylarkImport> imports = buildFileAST.getImports();
562562
Map<String, Extension> importMap = Maps.newHashMapWithExpectedSize(imports.size());
563563
ImmutableList.Builder<SkylarkFileDependency> fileDependencies = ImmutableList.builder();
564-
ImmutableMap<String, Label> importPathMap;
565564

566565
// Find the labels corresponding to the load statements.
567566
Label labelForCurrBuildFile;
@@ -571,15 +570,8 @@ static SkylarkImportResult fetchImportsFromBuildFile(
571570
// Shouldn't happen; the Label is well-formed by construction.
572571
throw new IllegalStateException(e);
573572
}
574-
try {
575-
importPathMap = SkylarkImportLookupFunction.findLabelsForLoadStatements(
576-
imports, labelForCurrBuildFile, env);
577-
if (importPathMap == null) {
578-
return null;
579-
}
580-
} catch (SkylarkImportFailedException e) {
581-
throw propagateSkylarkImportFailedException(packageId, e);
582-
}
573+
ImmutableMap<String, Label> importPathMap =
574+
SkylarkImportLookupFunction.getLabelsForLoadStatements(imports, labelForCurrBuildFile);
583575

584576
// Look up and load the imports.
585577
ImmutableCollection<Label> importLabels = importPathMap.values();

src/main/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunction.java

+14-51
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.google.common.collect.ImmutableCollection;
2121
import com.google.common.collect.ImmutableList;
2222
import com.google.common.collect.ImmutableMap;
23-
import com.google.common.collect.ImmutableMultimap;
2423
import com.google.common.collect.ImmutableSet;
2524
import com.google.common.collect.LinkedHashMultimap;
2625
import com.google.common.collect.Lists;
@@ -62,6 +61,7 @@
6261
import java.util.List;
6362
import java.util.Map;
6463
import java.util.logging.Logger;
64+
import java.util.stream.Collectors;
6565
import javax.annotation.Nullable;
6666

6767
/**
@@ -230,18 +230,12 @@ private SkylarkImportLookupValue computeInternal(
230230

231231
// Process the load statements in the file.
232232
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();
240235

241236
// Look up and load the imports.
242-
ImmutableCollection<Label> importLabels = labelsForImports.values();
243237
List<SkyKey> importLookupKeys =
244-
Lists.newArrayListWithExpectedSize(importLabels.size());
238+
Lists.newArrayListWithExpectedSize(labelsForImports.size());
245239
for (Label importLabel : importLabels) {
246240
importLookupKeys.add(SkylarkImportLookupValue.key(importLabel, inWorkspace));
247241
}
@@ -297,7 +291,7 @@ private SkylarkImportLookupValue computeInternal(
297291
// Process the loaded imports.
298292
Map<String, Extension> extensionsForImports = Maps.newHashMapWithExpectedSize(imports.size());
299293
ImmutableList.Builder<SkylarkFileDependency> fileDependencies =
300-
ImmutableList.builderWithExpectedSize(labelsForImports.size());
294+
ImmutableList.builderWithExpectedSize(importLabels.size());
301295
for (Map.Entry<String, Label> importEntry : labelsForImports.entrySet()) {
302296
String importString = importEntry.getKey();
303297
Label importLabel = importEntry.getValue();
@@ -399,53 +393,22 @@ private static ImmutableMap<PathFragment, Label> labelsForAbsoluteImports(
399393
}
400394

401395
/**
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.
403398
*
404399
* @param imports a collection of Skylark {@link LoadStatement}s
405400
* @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
409401
*/
410402
@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) {
414405
Preconditions.checkArgument(
415406
!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)));
449412
}
450413

451414
/**

src/main/java/com/google/devtools/build/lib/skyframe/ToplevelSkylarkAspectFunction.java

+4-18
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
import com.google.common.collect.Iterables;
2020
import com.google.devtools.build.lib.causes.LabelCause;
2121
import com.google.devtools.build.lib.cmdline.Label;
22-
import com.google.devtools.build.lib.events.Event;
2322
import com.google.devtools.build.lib.packages.SkylarkAspect;
2423
import com.google.devtools.build.lib.skyframe.AspectFunction.AspectCreationException;
2524
import com.google.devtools.build.lib.skyframe.AspectValue.SkylarkAspectLoadingKey;
26-
import com.google.devtools.build.lib.skyframe.SkylarkImportLookupFunction.SkylarkImportFailedException;
2725
import com.google.devtools.build.lib.syntax.SkylarkImport;
2826
import com.google.devtools.build.skyframe.SkyFunction;
2927
import com.google.devtools.build.skyframe.SkyFunctionException;
@@ -56,22 +54,10 @@ public SkyValue compute(SkyKey skyKey, Environment env)
5654
SkylarkImport extensionFile = aspectLoadingKey.getSkylarkImport();
5755

5856
// Find label corresponding to skylark file, if one exists.
59-
ImmutableMap<String, Label> labelLookupMap;
60-
try {
61-
labelLookupMap =
62-
SkylarkImportLookupFunction.findLabelsForLoadStatements(
63-
ImmutableList.of(extensionFile),
64-
Label.parseAbsoluteUnchecked("//:empty"),
65-
env
66-
);
67-
} catch (SkylarkImportFailedException e) {
68-
env.getListener().handle(Event.error(e.getMessage()));
69-
throw new LoadSkylarkAspectFunctionException(
70-
new AspectCreationException(e.getMessage(), Label.parseAbsoluteUnchecked("//:empty")));
71-
}
72-
if (labelLookupMap == null) {
73-
return null;
74-
}
57+
ImmutableMap<String, Label> labelLookupMap =
58+
SkylarkImportLookupFunction.getLabelsForLoadStatements(
59+
ImmutableList.of(extensionFile),
60+
Label.parseAbsoluteUnchecked("//:empty"));
7561

7662
SkylarkAspect skylarkAspect;
7763
Label extensionFileLabel = Iterables.getOnlyElement(labelLookupMap.values());

src/main/java/com/google/devtools/build/lib/syntax/SkylarkImport.java

-12
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,4 @@ public interface SkylarkImport extends Serializable {
4545
* @throws IllegalStateException if this import takes the form of an absolute path.
4646
*/
4747
Label getLabel(@Nullable Label containingFileLabel);
48-
49-
/**
50-
* True if this import takes the form of an absolute path.
51-
*/
52-
boolean hasAbsolutePath();
53-
54-
/**
55-
* Returns a {@link PathFragment} representing the import path.
56-
*
57-
* @throws IllegalStateException if this import does not take the form of an absolute path.
58-
*/
59-
PathFragment getAbsolutePath();
6048
}

src/main/java/com/google/devtools/build/lib/syntax/SkylarkImports.java

-80
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,6 @@ public String getImportString() {
5454
@Override
5555
public abstract Label getLabel(Label containingFileLabel);
5656

57-
@Override
58-
public boolean hasAbsolutePath() {
59-
return false;
60-
}
61-
62-
@Override
63-
public PathFragment getAbsolutePath() {
64-
throw new IllegalStateException("can't request absolute path from a non-absolute import");
65-
}
66-
6757
@Override
6858
public int hashCode() {
6959
return Objects.hash(getClass(), importString);
@@ -84,76 +74,6 @@ public boolean equals(Object that) {
8474
}
8575
}
8676

87-
@VisibleForSerialization
88-
@AutoCodec
89-
static final class AbsolutePathImport extends SkylarkImportImpl {
90-
private final PathFragment importPath;
91-
92-
@VisibleForSerialization
93-
AbsolutePathImport(String importString, PathFragment importPath) {
94-
super(importString);
95-
this.importPath = importPath;
96-
}
97-
98-
@Override
99-
public PathFragment asPathFragment() {
100-
return importPath;
101-
}
102-
103-
@Override
104-
public Label getLabel(Label containingFileLabel) {
105-
throw new IllegalStateException("can't request a label from an absolute path import");
106-
}
107-
108-
@Override
109-
public boolean hasAbsolutePath() {
110-
return true;
111-
}
112-
113-
@Override
114-
public PathFragment getAbsolutePath() {
115-
return this.importPath;
116-
}
117-
118-
}
119-
120-
@VisibleForSerialization
121-
@AutoCodec
122-
static final class RelativePathImport extends SkylarkImportImpl {
123-
private final String importFile;
124-
125-
@VisibleForSerialization
126-
RelativePathImport(String importString, String importFile) {
127-
super(importString);
128-
this.importFile = importFile;
129-
}
130-
131-
@Override
132-
public PathFragment asPathFragment() {
133-
return PathFragment.create(importFile);
134-
}
135-
136-
@Override
137-
public Label getLabel(Label containingFileLabel) {
138-
// The twistiness of the code below is due to the fact that the containing file may be in
139-
// a subdirectory of the package that contains it. We need to construct a Label with
140-
// the imported file in the same subdirectory of the package.
141-
PathFragment containingDirInPkg =
142-
PathFragment.create(containingFileLabel.getName()).getParentDirectory();
143-
String targetNameForImport = containingDirInPkg.getRelative(importFile).toString();
144-
try {
145-
// This is for imports relative to the current repository, so repositoryMapping can be
146-
// empty
147-
return containingFileLabel.getRelativeWithRemapping(targetNameForImport, ImmutableMap.of());
148-
} catch (LabelSyntaxException e) {
149-
// Shouldn't happen because the parent label is assumed to be valid and the target string is
150-
// validated on construction.
151-
throw new IllegalStateException(e);
152-
}
153-
}
154-
155-
}
156-
15777
@VisibleForSerialization
15878
@AutoCodec
15979
static final class AbsoluteLabelImport extends SkylarkImportImpl {

src/test/java/com/google/devtools/build/lib/skyframe/SkylarkImportLookupFunctionTest.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -119,45 +119,45 @@ public void testLoadFromSkylarkFileInRemoteRepo() throws Exception {
119119
}
120120

121121
@Test
122-
public void testLoadRelativePath() throws Exception {
122+
public void testLoadRelativeLabel() throws Exception {
123123
scratch.file("pkg/BUILD");
124124
scratch.file("pkg/ext1.bzl", "a = 1");
125125
scratch.file("pkg/ext2.bzl", "load(':ext1.bzl', 'a')");
126-
get(key("//pkg:ext2.bzl"));
126+
checkSuccessfulLookup("//pkg:ext2.bzl");
127127
}
128128

129129
@Test
130-
public void testLoadAbsolutePath() throws Exception {
130+
public void testLoadAbsoluteLabel() throws Exception {
131131
scratch.file("pkg2/BUILD");
132132
scratch.file("pkg3/BUILD");
133133
scratch.file("pkg2/ext.bzl", "b = 1");
134134
scratch.file("pkg3/ext.bzl", "load('//pkg2:ext.bzl', 'b')");
135-
get(key("//pkg3:ext.bzl"));
135+
checkSuccessfulLookup("//pkg3:ext.bzl");
136136
}
137137

138138
@Test
139-
public void testLoadFromSameAbsolutePathTwice() throws Exception {
139+
public void testLoadFromSameAbsoluteLabelTwice() throws Exception {
140140
scratch.file("pkg1/BUILD");
141141
scratch.file("pkg2/BUILD");
142142
scratch.file("pkg1/ext.bzl", "a = 1", "b = 2");
143143
scratch.file("pkg2/ext.bzl", "load('//pkg1:ext.bzl', 'a')", "load('//pkg1:ext.bzl', 'b')");
144-
get(key("//pkg2:ext.bzl"));
144+
checkSuccessfulLookup("//pkg2:ext.bzl");
145145
}
146146

147147
@Test
148-
public void testLoadFromSameRelativePathTwice() throws Exception {
148+
public void testLoadFromSameRelativeLabelTwice() throws Exception {
149149
scratch.file("pkg/BUILD");
150150
scratch.file("pkg/ext1.bzl", "a = 1", "b = 2");
151151
scratch.file("pkg/ext2.bzl", "load(':ext1.bzl', 'a')", "load(':ext1.bzl', 'b')");
152-
get(key("//pkg:ext2.bzl"));
152+
checkSuccessfulLookup("//pkg:ext2.bzl");
153153
}
154154

155155
@Test
156-
public void testLoadFromRelativePathInSubdir() throws Exception {
156+
public void testLoadFromRelativeLabelInSubdir() throws Exception {
157157
scratch.file("pkg/BUILD");
158158
scratch.file("pkg/subdir/ext1.bzl", "a = 1");
159159
scratch.file("pkg/subdir/ext2.bzl", "load(':subdir/ext1.bzl', 'a')");
160-
get(key("//pkg:subdir/ext2.bzl"));
160+
checkSuccessfulLookup("//pkg:subdir/ext2.bzl");
161161
}
162162

163163
private EvaluationResult<SkylarkImportLookupValue> get(SkyKey skylarkImportLookupKey)
@@ -171,7 +171,7 @@ private EvaluationResult<SkylarkImportLookupValue> get(SkyKey skylarkImportLooku
171171
return result;
172172
}
173173

174-
private SkyKey key(String label) throws Exception {
174+
private SkyKey key(String label) {
175175
return SkylarkImportLookupValue.key(Label.parseAbsoluteUnchecked(label), false);
176176
}
177177

src/test/java/com/google/devtools/build/lib/syntax/ParserTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,6 @@ private void validNonAbsoluteImportTest(String importString, String containingFi
10991099
SkylarkImport imp = SkylarkImports.create(stmt.getImport().getValue());
11001100

11011101
assertThat(imp.getImportString()).named("getImportString()").isEqualTo(importString);
1102-
assertThat(imp.hasAbsolutePath()).named("hasAbsolutePath()").isFalse();
11031102

11041103
Label containingFileLabel = Label.parseAbsoluteUnchecked(containingFileLabelString);
11051104
assertThat(imp.getLabel(containingFileLabel)).named("containingFileLabel()")

0 commit comments

Comments
 (0)