Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

base: add Sys.detectwsl() #57069

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ New library functions
* `uuid7()` creates an RFC 9652 compliant UUID with version 7 ([#54834]).
* `insertdims(array; dims)` allows to insert singleton dimensions into an array which is the inverse operation to `dropdims`. ([#45793])
* The new `Fix` type is a generalization of `Fix1/Fix2` for fixing a single argument ([#54653]).
* `Sys.detectwsl()` allows to testing if Julia is running inside WSL at runtime. ([#57069])

New library features
--------------------
Expand Down
24 changes: 23 additions & 1 deletion base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export BINDIR,
isreadable,
iswritable,
username,
which
which,
detectwsl

import ..Base: show

Expand Down Expand Up @@ -532,6 +533,27 @@ including e.g. a WebAssembly JavaScript embedding in a web browser.
"""
isjsvm(os::Symbol) = (os === :Emscripten)

"""
Sys.detectwsl()

Runtime predicate for testing if Julia is running inside
Windows Subsystem for Linux (WSL).

!!! note
Unlike `Sys.iswindows`, `Sys.islinux` etc., this is a runtime test, and thus
cannot meaningfully be used in `@static if` constructs.

!!! compat "Julia 1.12"
This function requires at least Julia 1.12.
"""
function detectwsl()
# We use the same approach as canonical/snapd do to detect WSL
islinux() && (
isfile("/proc/sys/fs/binfmt_misc/WSLInterop")
|| isdir("/run/WSL")
)
end

for f in (:isunix, :islinux, :isbsd, :isapple, :iswindows, :isfreebsd, :isopenbsd, :isnetbsd, :isdragonfly, :isjsvm)
@eval $f() = $(getfield(@__MODULE__, f)(KERNEL))
end
Expand Down
5 changes: 5 additions & 0 deletions test/osutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ using Libdl
else
@test Sys.windows_version() >= v"1.0.0-"
end

# TODO: When we have a WSL CI, add a new test here `@test detectwsl()`
if !Sys.islinux()
@test !Sys.detectwsl()
end
end

@testset "@static" begin
Expand Down
2 changes: 1 addition & 1 deletion test/path.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@
@testset "uripath" begin
host = if Sys.iswindows()
""
elseif ispath("/proc/sys/fs/binfmt_misc/WSLInterop")
elseif Sys.detectwsl()
distro = get(ENV, "WSL_DISTRO_NAME", "") # See <https://patrickwu.space/wslconf/>
"wsl%24/$distro" # See <https://github.com/microsoft/terminal/pull/14993> and <https://learn.microsoft.com/en-us/windows/wsl/filesystems>
else
Expand Down
Loading