Browse files

Unify executable lookup (which_*) into one

In the case of Windows, shelling out to perform the lookup was even more
expensive than POSIX.

What was done here is remove the dependency on `which.bat` support file and
perform a direct lookup over ENV['PATH'] looking for the required

The approach taken will append '.exe' to supplied cmd name under Windows.

Only .exe are truly executables and anything else (.bat, .vbs, .msc) can't
be executed directly.

To execute batch files (.bat, .cmd), Ruby wraps them with cmd.exe.

Based on existing executables on the platform (node.exe, cscript.exe) and
that .com hasn't been seen in the past 10 years (only for compatibility),
the lookup over PATHEXT seems unnecessary.
  • Loading branch information...
1 parent c1f0953 commit 7b830a084327954feb33524b8f4489873e9b1c18 @luislavena luislavena committed Dec 6, 2011
Showing with 5 additions and 6 deletions.
  1. +5 −6 lib/execjs/external_runtime.rb
@@ -121,12 +121,11 @@ def binary
@binary ||= locate_binary
- def which_windows(name)
- result = `#{shell_escape("#{ExecJS.root}/support/which.bat", name)}`
- result.strip.split("\n").first
- end
+ def locate_executable(cmd)
+ if && File.extname(cmd) == ""
+ cmd << ".exe"
+ end
- def which_unix(cmd)
if File.executable? cmd
@@ -165,7 +164,7 @@ def locate_binary
def which(command)
Array(command).find do |name|
name, args = name.split(/\s+/, 2)
- path = ? which_windows(name) : which_unix(name)
+ path = locate_executable(name)
next unless path

0 comments on commit 7b830a0

Please sign in to comment.