Skip to content

Commit acc1ff6

Browse files
authored
[7.3.0] Be resilient to outdated exec paths in action cache entries (#23230)
60924fd changed the canonical repo name separator from `~` to `+`. Older repo names containing `~` now trigger a syntax error. If an action cache entry refers to an exec path from a previous version of Bazel that used `~`, we need to be resilient and treat the cache entry as corrupted, rather than just crash. Fixes #23180. Closes #23227. PiperOrigin-RevId: 660105601 Change-Id: Iea5d86c635056d12ba20598383da463bdde03ab0
1 parent 5f5355b commit acc1ff6

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/main/java/com/google/devtools/build/lib/cmdline/PackageIdentifier.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.devtools.build.skyframe.SkyFunctionName;
2424
import com.google.devtools.build.skyframe.SkyKey;
2525
import com.google.devtools.build.skyframe.SkyKey.SkyKeyInterner;
26+
import java.util.Optional;
2627
import javax.annotation.Nullable;
2728
import javax.annotation.concurrent.Immutable;
2829

@@ -77,8 +78,11 @@ public static PackageIdentifier createInMainRepo(PathFragment name) {
7778
*
7879
* In this case, this method returns a package identifier for foo/bar, even though that is not a
7980
* package. Callers need to look up the actual package if needed.
81+
*
82+
* <p>Returns {@link Optional#empty()} if the path corresponds to an invalid label (e.g. with a
83+
* malformed repo name).
8084
*/
81-
public static PackageIdentifier discoverFromExecPath(
85+
public static Optional<PackageIdentifier> discoverFromExecPath(
8286
PathFragment execPath, boolean forFiles, boolean siblingRepositoryLayout) {
8387
Preconditions.checkArgument(!execPath.isAbsolute(), execPath);
8488
PathFragment tofind =
@@ -93,10 +97,15 @@ public static PackageIdentifier discoverFromExecPath(
9397
if (tofind.startsWith(prefix)) {
9498
// Using the path prefix can be either "external" or "..", depending on whether the sibling
9599
// repository layout is used.
96-
RepositoryName repository = RepositoryName.createUnvalidated(tofind.getSegment(1));
97-
return PackageIdentifier.create(repository, tofind.subFragment(2));
100+
try {
101+
RepositoryName repository = RepositoryName.create(tofind.getSegment(1));
102+
return Optional.of(PackageIdentifier.create(repository, tofind.subFragment(2)));
103+
} catch (LabelSyntaxException e) {
104+
// The path corresponds to an invalid label.
105+
return Optional.empty();
106+
}
98107
} else {
99-
return PackageIdentifier.createInMainRepo(tofind);
108+
return Optional.of(PackageIdentifier.createInMainRepo(tofind));
100109
}
101110
}
102111

src/main/java/com/google/devtools/build/lib/rules/cpp/HeaderDiscovery.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.devtools.build.lib.actions.PathMapper;
2323
import com.google.devtools.build.lib.cmdline.LabelConstants;
2424
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
25-
import com.google.devtools.build.lib.cmdline.RepositoryName;
2625
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
2726
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
2827
import com.google.devtools.build.lib.vfs.FileSystemUtils;
@@ -32,6 +31,7 @@
3231
import java.util.HashMap;
3332
import java.util.List;
3433
import java.util.Map;
34+
import java.util.Optional;
3535
import javax.annotation.Nullable;
3636

3737
/**
@@ -178,10 +178,13 @@ private static NestedSet<Artifact> runDiscovery(
178178
}
179179
Artifact artifact = regularDerivedArtifacts.get(execPathFragment);
180180
if (artifact == null) {
181-
RepositoryName repository =
182-
PackageIdentifier.discoverFromExecPath(execPathFragment, false, siblingRepositoryLayout)
183-
.getRepository();
184-
artifact = artifactResolver.resolveSourceArtifact(execPathFragment, repository);
181+
Optional<PackageIdentifier> pkgId =
182+
PackageIdentifier.discoverFromExecPath(
183+
execPathFragment, false, siblingRepositoryLayout);
184+
if (pkgId.isPresent()) {
185+
artifact =
186+
artifactResolver.resolveSourceArtifact(execPathFragment, pkgId.get().getRepository());
187+
}
185188
}
186189
if (artifact != null) {
187190
// We don't need to add the sourceFile itself as it is a mandatory input.

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
import java.util.HashSet;
117117
import java.util.List;
118118
import java.util.Map;
119+
import java.util.Optional;
119120
import java.util.Set;
120121
import java.util.concurrent.atomic.AtomicBoolean;
121122
import java.util.function.Consumer;
@@ -686,11 +687,13 @@ public Map<PathFragment, Root> findPackageRootsForFiles(Iterable<PathFragment> e
686687
PathFragment parent =
687688
checkNotNull(path.getParentDirectory(), "Must pass in files, not root directory");
688689
checkArgument(!parent.isAbsolute(), path);
689-
ContainingPackageLookupValue.Key depKey =
690-
ContainingPackageLookupValue.key(
691-
PackageIdentifier.discoverFromExecPath(path, true, siblingRepositoryLayout));
692-
depKeys.put(path, depKey);
693-
packageLookupsRequested.add(depKey);
690+
Optional<PackageIdentifier> pkgId =
691+
PackageIdentifier.discoverFromExecPath(path, true, siblingRepositoryLayout);
692+
if (pkgId.isPresent()) {
693+
ContainingPackageLookupValue.Key depKey = ContainingPackageLookupValue.key(pkgId.get());
694+
depKeys.put(path, depKey);
695+
packageLookupsRequested.add(depKey);
696+
}
694697
}
695698

696699
SkyframeLookupResult values = env.getValuesAndExceptions(depKeys.values());

0 commit comments

Comments
 (0)