diff options
author | 2017-08-10 10:48:34 +0200 | |
---|---|---|
committer | 2017-08-10 13:48:13 +0200 | |
commit | 1bf94673f1c3308bda0a1272d1ab16736b53a4dd (patch) | |
tree | cf52e1eea2f14f5c0d86df3a7f576187edc6c832 | |
parent | 0ec7180c361fd524f0beef1814c7930e7ed9898d (diff) |
Make repository_ctx.which() portable
See https://github.com/bazelbuild/bazel/issues/3477
Change-Id: I39e0138ec7489f9d9c1e31ee683dbbf7a72cee95
PiperOrigin-RevId: 164828274
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java index 7f3f01477b..72a873c0cf 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/repository/skylark/SkylarkRepositoryContext.java @@ -46,6 +46,7 @@ import com.google.devtools.build.lib.syntax.Runtime; import com.google.devtools.build.lib.syntax.SkylarkDict; import com.google.devtools.build.lib.syntax.SkylarkList; import com.google.devtools.build.lib.syntax.SkylarkType; +import com.google.devtools.build.lib.util.OsUtils; import com.google.devtools.build.lib.util.StringUtilities; import com.google.devtools.build.lib.vfs.FileSystemUtils; import com.google.devtools.build.lib.vfs.Path; @@ -423,19 +424,32 @@ public class SkylarkRepositoryContext { Location.BUILTIN, "Program argument of which() may not contains a / or a \\ ('" + program + "' given)"); } + try { + SkylarkPath commandPath = findCommandOnPath(program); + if (commandPath != null) { + return commandPath; + } + + if (!program.endsWith(OsUtils.executableExtension())) { + program += OsUtils.executableExtension(); + return findCommandOnPath(program); + } + } catch (IOException e) { + // IOException when checking executable file means we cannot read the file data so + // we cannot execute it, swallow the exception. + } + return null; + } + + private SkylarkPath findCommandOnPath(String program) throws IOException { for (String p : getPathEnvironment()) { PathFragment fragment = PathFragment.create(p); if (fragment.isAbsolute()) { // We ignore relative path as they don't mean much here (relative to where? the workspace // root?). Path path = outputDirectory.getFileSystem().getPath(fragment).getChild(program); - try { - if (path.exists() && path.isExecutable()) { - return new SkylarkPath(path); - } - } catch (IOException e) { - // IOException when checking executable file means we cannot read the file data so - // we cannot executes it, swallow the exception. + if (path.exists() && path.isExecutable()) { + return new SkylarkPath(path); } } } |