Skip to content

Commit

Permalink
Make Sys.which return nothing instead of throwing
Browse files Browse the repository at this point in the history
Currently `Sys.which("prog")` will throw an error if `prog` is not
found or is found but is not executable. This changes the function to
instead return `nothing` in those cases, thereby simplifying logic for
situations such as hierarchically choosing an executable based on which
in a list is present.

Fixes #27284.
  • Loading branch information
ararslan committed May 28, 2018
1 parent c545d77 commit 45d6125
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ function which(program_name::String)
end
end

# If we couldn't find anything, complain
error("$program_name not found")
# If we couldn't find anything, don't return anything
nothing
end
which(program_name::AbstractString) = which(String(program_name))

Expand Down
8 changes: 4 additions & 4 deletions test/spawn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -561,12 +561,12 @@ mktempdir() do dir
@test Sys.which(foo_path) == realpath(foo_path)

chmod(foo_path, 0o666)
@test_throws ErrorException Sys.which("foo")
@test_throws ErrorException Sys.which(foo_path)
@test Sys.which("foo") === nothing
@test Sys.which(foo_path) === nothing
end

# Test that completely missing files also fail
@test_throws ErrorException Sys.which("this_is_not_a_command")
# Test that completely missing files also return nothing
@test Sys.which("this_is_not_a_command") === nothing
end
end

Expand Down

0 comments on commit 45d6125

Please sign in to comment.