Skip to content

Commit 0f5679e

Browse files
ulfjackCopybara-Service
authored andcommitted
Use PATH and LD_LIBRARY_PATH from the client's environment if possible
This is technically an incompatible change, but I think it's unlikely to affect a lot of users. Note that this change leaves out Windows, where we set the PATH to the server env PATH plus the MSYS root determined from the shell path. This is a cleanup, and it also makes unknown commit slightly safer. PiperOrigin-RevId: 189981959
1 parent 9c4cecd commit 0f5679e

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/BazelConfiguration.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,25 @@ public PathFragment getShellExecutable() {
132132

133133
@Override
134134
public void setupActionEnvironment(Map<String, String> builder) {
135+
// All entries in the builder that have a value of null inherit the value from the client
136+
// environment, which is only known at execution time - we don't want to bake the client env
137+
// into the configuration since any change to the configuration requires rerunning the full
138+
// analysis phase.
135139
if (useStrictActionEnv) {
136-
String path = pathOrDefault(os, null, getShellExecutable());
137-
builder.put("PATH", path);
138-
} else {
139-
// TODO(ulfjack): Avoid using System.getenv; it's the wrong environment!
140+
builder.put("PATH", pathOrDefault(os, null, getShellExecutable()));
141+
} else if (os == OS.WINDOWS) {
142+
// TODO(ulfjack): We want to add the MSYS root to the PATH, but that prevents us from
143+
// inheriting PATH from the client environment. For now we use System.getenv even though
144+
// that is incorrect. We should enable strict_action_env by default and then remove this
145+
// code, but that change may break Windows users who are relying on the MSYS root being in
146+
// the PATH.
140147
builder.put("PATH", pathOrDefault(os, System.getenv("PATH"), getShellExecutable()));
141-
142-
String ldLibraryPath = System.getenv("LD_LIBRARY_PATH");
143-
if (ldLibraryPath != null) {
144-
builder.put("LD_LIBRARY_PATH", ldLibraryPath);
145-
}
148+
builder.put("LD_LIBRARY_PATH", null);
149+
} else {
150+
// The previous implementation used System.getenv (which uses the server's environment), and
151+
// fell back to a hard-coded "/bin:/usr/bin" if PATH was not set.
152+
builder.put("PATH", null);
153+
builder.put("LD_LIBRARY_PATH", null);
146154
}
147155
}
148156

@@ -168,7 +176,7 @@ static String pathOrDefault(OS os, @Nullable String path, @Nullable PathFragment
168176
// from the local machine. For now, this can be overridden with --action_env=PATH=<value>, so
169177
// at least there's a workaround.
170178
if (os != OS.WINDOWS) {
171-
return path == null ? "/bin:/usr/bin" : path;
179+
return "/bin:/usr/bin";
172180
}
173181

174182
// Attempt to compute the MSYS root (the real Windows path of "/") from `sh`.
@@ -187,10 +195,8 @@ static String pathOrDefault(OS os, @Nullable String path, @Nullable PathFragment
187195
newPath += ";" + path;
188196
}
189197
return newPath;
190-
} else if (path != null) {
191-
return path;
192198
} else {
193-
return "";
199+
return null;
194200
}
195201
}
196202
}

src/test/java/com/google/devtools/build/lib/bazel/rules/BazelConfigurationTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,13 @@ public void strictActionEnv() {
6767
@Test
6868
public void pathOrDefaultOnLinux() {
6969
assertThat(pathOrDefault(OS.LINUX, null, null)).isEqualTo("/bin:/usr/bin");
70-
assertThat(pathOrDefault(OS.LINUX, "/not/bin", null)).isEqualTo("/not/bin");
70+
assertThat(pathOrDefault(OS.LINUX, "/not/bin", null)).isEqualTo("/bin:/usr/bin");
7171
}
7272

7373
@Test
7474
public void pathOrDefaultOnWindows() {
75-
assertThat(pathOrDefault(OS.WINDOWS, null, null)).isEqualTo("");
76-
assertThat(pathOrDefault(OS.WINDOWS, "C:/mypath", null))
77-
.isEqualTo("C:/mypath");
75+
assertThat(pathOrDefault(OS.WINDOWS, null, null)).isNull();
76+
assertThat(pathOrDefault(OS.WINDOWS, "C:/mypath", null)).isNull();
7877
assertThat(pathOrDefault(OS.WINDOWS, "C:/mypath", PathFragment.create("D:/foo/shell")))
7978
.isEqualTo("D:\\foo;C:/mypath");
8079
}

src/test/shell/bazel/bazel_test_test.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,12 @@ EOF
198198

199199
# We don't just use the local PATH, but use the test's PATH, which is more restrictive.
200200
PATH=$PATH:$PWD/scripts bazel --nomaster_bazelrc test //testing:t1 -s --run_under=hello \
201-
--test_output=all >& $TEST_log && fail "Expected failure"
201+
--test_output=all --experimental_strict_action_env >& $TEST_log && fail "Expected failure"
202+
203+
# With --noexperimental_strict_action_env, the local PATH is forwarded to the test.
204+
PATH=$PATH:$PWD/scripts bazel test //testing:t1 -s --run_under=hello \
205+
--test_output=all --noexperimental_strict_action_env >& $TEST_log || fail "Expected success"
206+
expect_log 'hello script!!! testing/t1'
202207

203208
# We need to forward the PATH to make it work.
204209
PATH=$PATH:$PWD/scripts bazel test //testing:t1 -s --run_under=hello \

0 commit comments

Comments
 (0)