Skip to content

Commit 58d31c5

Browse files
committed
Handle non-existence
1 parent eca2631 commit 58d31c5

File tree

2 files changed

+47
-24
lines changed

2 files changed

+47
-24
lines changed

src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
import com.google.devtools.build.lib.vfs.RootedPath;
123123
import com.google.devtools.common.options.OptionsBase;
124124
import com.google.devtools.common.options.OptionsParsingResult;
125+
import java.io.FileNotFoundException;
125126
import java.io.IOException;
126127
import java.lang.reflect.InvocationTargetException;
127128
import java.time.Instant;
@@ -377,28 +378,40 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
377378
}
378379
Path repoContentsCachePath = repositoryCache.getRepoContentsCache().getPath();
379380
if (repoContentsCachePath != null) {
381+
Path resolvedOutputBase;
380382
try {
381-
Path resolvedRepoContentsCache = repoContentsCachePath.resolveSymbolicLinks();
382-
Path resolvedOutputBase = env.getOutputBase().resolveSymbolicLinks();
383-
if (resolvedOutputBase.startsWith(resolvedOutputBase)) {
384-
// This is dangerous as the repo contents cache GC may delete files in the output base.
385-
throw new AbruptExitException(
386-
detailedExitCode(
387-
"""
388-
The output base [%s] is inside the repo contents cache [%s]. This can cause \
389-
spurious failures. Disable the repo contents cache with `--repo_contents_cache=`, \
390-
or specify `--repo_contents_cache=<path that doesn't contain the output base>`.
391-
"""
392-
.formatted(resolvedOutputBase, resolvedRepoContentsCache),
393-
Code.BAD_REPO_CONTENTS_CACHE));
394-
}
383+
resolvedOutputBase = env.getOutputBase().resolveSymbolicLinks();
384+
} catch (IOException e) {
385+
throw new AbruptExitException(
386+
detailedExitCode(
387+
"could not resolve output base: %s".formatted(e.getMessage()),
388+
Code.BAD_REPO_CONTENTS_CACHE),
389+
e);
390+
}
391+
Path resolvedRepoContentsCache = repoContentsCachePath;
392+
try {
393+
resolvedRepoContentsCache = resolvedRepoContentsCache.resolveSymbolicLinks();
394+
} catch (FileNotFoundException e) {
395+
// Will be created later.
395396
} catch (IOException e) {
396397
throw new AbruptExitException(
397398
detailedExitCode(
398399
"could not resolve repo contents cache path: %s".formatted(e.getMessage()),
399400
Code.BAD_REPO_CONTENTS_CACHE),
400401
e);
401402
}
403+
if (resolvedOutputBase.startsWith(resolvedOutputBase)) {
404+
// This is dangerous as the repo contents cache GC may delete files in the output base.
405+
throw new AbruptExitException(
406+
detailedExitCode(
407+
"""
408+
The output base [%s] is inside the repo contents cache [%s]. This can cause \
409+
spurious failures. Disable the repo contents cache with `--repo_contents_cache=`, \
410+
or specify `--repo_contents_cache=<path that doesn't contain the output base>`.
411+
"""
412+
.formatted(resolvedOutputBase, resolvedRepoContentsCache),
413+
Code.BAD_REPO_CONTENTS_CACHE));
414+
}
402415
}
403416
if (repoContentsCachePath != null
404417
&& env.getWorkspace() != null

src/main/java/com/google/devtools/build/lib/remote/RemoteModule.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
import io.netty.handler.codec.DecoderException;
112112
import io.netty.handler.codec.http.HttpResponseStatus;
113113
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
114+
import java.io.FileNotFoundException;
114115
import java.io.IOException;
115116
import java.net.URI;
116117
import java.net.URISyntaxException;
@@ -344,22 +345,31 @@ public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
344345
boolean enableRemoteDownloader = shouldEnableRemoteDownloader(remoteOptions);
345346

346347
if (enableDiskCache) {
348+
Path resolvedOutputBase;
347349
try {
348-
Path resolvedOutputBase = env.getOutputBase().resolveSymbolicLinks();
349-
Path resolvedDiskCache =
350-
env.getWorkingDirectory().getRelative(remoteOptions.diskCache).resolveSymbolicLinks();
351-
if (resolvedOutputBase.startsWith(resolvedDiskCache)) {
352-
// This is dangerous as the disk cache GC may delete files in the output base.
353-
throw createOptionsExitException(
354-
"The output base [%s] cannot be a subdirectory of the --disk_cache directory [%s]"
355-
.formatted(resolvedOutputBase, resolvedDiskCache),
356-
FailureDetails.RemoteOptions.Code.EXECUTION_WITH_INVALID_CACHE);
357-
}
350+
resolvedOutputBase = env.getOutputBase().resolveSymbolicLinks();
351+
} catch (IOException e) {
352+
throw createOptionsExitException(
353+
"Failed to resolve output base: %s".formatted(e.getMessage()),
354+
FailureDetails.RemoteOptions.Code.EXECUTION_WITH_INVALID_CACHE);
355+
}
356+
Path resolvedDiskCache = env.getWorkingDirectory().getRelative(remoteOptions.diskCache);
357+
try {
358+
resolvedDiskCache = resolvedDiskCache.resolveSymbolicLinks();
359+
} catch (FileNotFoundException ignored) {
360+
// Will be created later.
358361
} catch (IOException e) {
359362
throw createOptionsExitException(
360363
"Failed to resolve disk cache directory: %s".formatted(e.getMessage()),
361364
FailureDetails.RemoteOptions.Code.EXECUTION_WITH_INVALID_CACHE);
362365
}
366+
if (resolvedOutputBase.startsWith(resolvedDiskCache)) {
367+
// This is dangerous as the disk cache GC may delete files in the output base.
368+
throw createOptionsExitException(
369+
"The output base [%s] cannot be a subdirectory of the --disk_cache directory [%s]"
370+
.formatted(resolvedOutputBase, resolvedDiskCache),
371+
FailureDetails.RemoteOptions.Code.EXECUTION_WITH_INVALID_CACHE);
372+
}
363373
var gcIdleTask =
364374
DiskCacheGarbageCollectorIdleTask.create(remoteOptions, env.getWorkingDirectory());
365375
if (gcIdleTask != null) {

0 commit comments

Comments
 (0)