Skip to content

Commit

Permalink
[7.4.0] CommandLinePathFactory: skip empty or relative path elements (#…
Browse files Browse the repository at this point in the history
…23376)

Fixes #23305

Closes #23310.

PiperOrigin-RevId: 665745084
Change-Id: Ib22290e08f62c52c5a596eeabd31b1b910fcf421

Commit
8b2057e

Co-authored-by: Jay Conrod <[email protected]>
  • Loading branch information
bazel-io and jayconrod authored Aug 26, 2024
1 parent 84725e5 commit ecde9e2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,14 @@ public Path create(Map<String, String> env, String value) throws IOException {
String pathVariable = env.getOrDefault("PATH", "");
if (!Strings.isNullOrEmpty(pathVariable)) {
for (String lookupPath : PATH_SPLITTER.split(pathVariable)) {
Path maybePath = fileSystem.getPath(lookupPath).getRelative(path);
PathFragment lookupPathFragment = PathFragment.create(lookupPath);
if (lookupPathFragment.isEmpty() || !lookupPathFragment.isAbsolute()) {
// Ignore empty or relative path components. These are uncommon and may be confusing if
// bazel is running in a different directory than the user's current directory.
continue;
}

Path maybePath = fileSystem.getPath(lookupPathFragment).getRelative(path);
if (maybePath.exists(Symlinks.FOLLOW)
&& maybePath.isFile(Symlinks.FOLLOW)
&& maybePath.isExecutable()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,13 @@ public void pathLookupWithExistingAndNonExistingDirectoryOnPath() throws Excepti
ImmutableMap.of("PATH", PATH_JOINER.join("/bin", "/does/not/exist", "/usr/bin")),
"a"));
}

@Test
public void pathLookupWithInvalidPath() throws Exception {
CommandLinePathFactory factory = new CommandLinePathFactory(filesystem, ImmutableMap.of());

createExecutable("/bin/true");
var path = ImmutableMap.of("PATH", PATH_JOINER.join("", ".", "/bin"));
assertThat(factory.create(path, "true")).isEqualTo(filesystem.getPath("/bin/true"));
}
}

0 comments on commit ecde9e2

Please sign in to comment.