From bf82e7b4c1886c8286fa2a5bc99bedb5e8032ab4 Mon Sep 17 00:00:00 2001 From: Tiago Quelhas Date: Wed, 17 May 2023 18:40:35 +0200 Subject: [PATCH] [6.3.0] Prevent CredentialHelperEnvironment crash when invoking Bazel outside of a workspace. (#18430) PiperOrigin-RevId: 532048391 Change-Id: Id8138140f469d76469d417f622bc4ad45a10b914 --- .../credentialhelper/CredentialHelper.java | 5 ++++- .../CredentialHelperEnvironment.java | 12 +++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelper.java b/src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelper.java index 62201082210beb..cf15e0f917c35a 100644 --- a/src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelper.java +++ b/src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelper.java @@ -158,7 +158,10 @@ private Subprocess spawnSubprocess(CredentialHelperEnvironment environment, Stri // WindowsSubprocessFactory cannot redirect stdin to subprocess. return new SubprocessBuilder(JavaSubprocessFactory.INSTANCE) .setArgv(ImmutableList.builder().add(path.getPathString()).add(args).build()) - .setWorkingDirectory(environment.getWorkspacePath().getPathFile()) + .setWorkingDirectory( + environment.getWorkspacePath() != null + ? environment.getWorkspacePath().getPathFile() + : null) .setEnv(environment.getClientEnvironment()) .setTimeoutMillis(environment.getHelperExecutionTimeout().toMillis()) .start(); diff --git a/src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelperEnvironment.java b/src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelperEnvironment.java index e2ae01c190b3da..1632b21aeefebd 100644 --- a/src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelperEnvironment.java +++ b/src/main/java/com/google/devtools/build/lib/authandtls/credentialhelper/CredentialHelperEnvironment.java @@ -19,6 +19,7 @@ import com.google.devtools.build.lib.events.Reporter; import com.google.devtools.build.lib.vfs.Path; import java.time.Duration; +import javax.annotation.Nullable; /** Environment for running {@link CredentialHelper}s in. */ @AutoValue @@ -27,10 +28,12 @@ public abstract class CredentialHelperEnvironment { public abstract Reporter getEventReporter(); /** - * Returns the (absolute) path to the workspace. + * Returns the absolute path to the workspace, or null if Bazel was invoked outside a workspace. * - *

Used as working directory when invoking the subprocess. + *

If available, it will be used as the working directory when invoking the helper subprocess. + * Otherwise, the working directory is inherited from the Bazel server process. */ + @Nullable public abstract Path getWorkspacePath(); /** @@ -55,10 +58,9 @@ public abstract static class Builder { public abstract Builder setEventReporter(Reporter reporter); /** - * Sets the (absolute) path to the workspace to use as working directory when invoking the - * subprocess. + * Sets the absolute path to the workspace, or null if Bazel was invoked outside a workspace. */ - public abstract Builder setWorkspacePath(Path path); + public abstract Builder setWorkspacePath(@Nullable Path path); /** * Sets the environment from the Bazel client to pass as environment variables to the