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
22 changes: 21 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,25 @@ including e.g. a WebAssembly JavaScript embedding in a web browser.
"""
isjsvm(os::Symbol) = (os === :Emscripten)

"""
Sys.detectwsl([os])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps Sys.iswsl() for consistency with the other platform detection functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually on purpose, as detectwsl detects at runtime, so names of the form is_platform are not used.
See discuss in #36354

inkydragon marked this conversation as resolved.
Show resolved Hide resolved

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 tests, and thus
cannot meaningfully be used in `@static if ` constructs.
inkydragon marked this conversation as resolved.
Show resolved Hide resolved

!!! compat "Julia 1.12"
This function requires at least Julia 1.12.
"""
function detectwsl()
islinux() &&
isfile("/proc/sys/kernel/osrelease") &&
contains(read("/proc/sys/kernel/osrelease", String), r"Microsoft|WSL"i)
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

@test !Sys.detectwsl(:Windows)
inkydragon marked this conversation as resolved.
Show resolved Hide resolved
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