Skip to content

Commit 323ca86

Browse files
inkydragonmgautam98fingolfinararslan
authored
base: add Sys.detectwsl() (JuliaLang#57069)
Close JuliaLang#36425, fix JuliaLang#36354 ## How to detect WSL? There are a number of ways that can be used to detect WSL environments, but each can have false positives. We finally chose to use the same method as Snapd to detect WSL. Because Windows installs Ubuntu LTS as WSL by default. So we assume that Snapd's detection method will work for most users. - Ubuntu/Snapd: https://github.com/canonical/snapd/blob/03a578a5dff26467dcc80580fcd4720a486185a5/release/release.go#L151-L172 - microsoft/WSL#423 - microsoft/WSL#4071 - https://superuser.com/q/1749781/1460597 ## Known limitations - this is a runtime test, and thus cannot meaningfully be used in `@static if` constructs. - Linux users can create their own - `/proc/sys/fs/binfmt_misc/WSLInterop` file - or `/run/WSL/` folder to pretend to be a WSL environment. --- - I've tested this under: Ubuntu 22.04.5 LTS (default/Offical) and alpine-release-3.17.0 (win store) - [x] Add compat, NEWS, tests - [x] Take a look at different detect methods, figure out which one is more robust --------- Co-authored-by: Gautam Mishra <[email protected]> Co-authored-by: Max Horn <[email protected]> Co-authored-by: Chengyu Han <[email protected]> Co-authored-by: Alex Arslan <[email protected]>
1 parent 4e13e0e commit 323ca86

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ New library functions
9797
* `uuid7()` creates an RFC 9652 compliant UUID with version 7 ([#54834]).
9898
* `insertdims(array; dims)` allows to insert singleton dimensions into an array which is the inverse operation to `dropdims`. ([#45793])
9999
* The new `Fix` type is a generalization of `Fix1/Fix2` for fixing a single argument ([#54653]).
100+
* `Sys.detectwsl()` allows to testing if Julia is running inside WSL at runtime. ([#57069])
100101

101102
New library features
102103
--------------------

base/sysinfo.jl

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export BINDIR,
3636
isreadable,
3737
iswritable,
3838
username,
39-
which
39+
which,
40+
detectwsl
4041

4142
import ..Base: show
4243

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

536+
"""
537+
Sys.detectwsl()
538+
539+
Runtime predicate for testing if Julia is running inside
540+
Windows Subsystem for Linux (WSL).
541+
542+
!!! note
543+
Unlike `Sys.iswindows`, `Sys.islinux` etc., this is a runtime test, and thus
544+
cannot meaningfully be used in `@static if` constructs.
545+
546+
!!! compat "Julia 1.12"
547+
This function requires at least Julia 1.12.
548+
"""
549+
function detectwsl()
550+
# We use the same approach as canonical/snapd do to detect WSL
551+
islinux() && (
552+
isfile("/proc/sys/fs/binfmt_misc/WSLInterop")
553+
|| isdir("/run/WSL")
554+
)
555+
end
556+
535557
for f in (:isunix, :islinux, :isbsd, :isapple, :iswindows, :isfreebsd, :isopenbsd, :isnetbsd, :isdragonfly, :isjsvm)
536558
@eval $f() = $(getfield(@__MODULE__, f)(KERNEL))
537559
end

test/osutils.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ using Libdl
2929
else
3030
@test Sys.windows_version() >= v"1.0.0-"
3131
end
32+
33+
# TODO: When we have a WSL CI, add a new test here `@test detectwsl()`
34+
if !Sys.islinux()
35+
@test !Sys.detectwsl()
36+
end
3237
end
3338

3439
@testset "@static" begin

test/path.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@
314314
@testset "uripath" begin
315315
host = if Sys.iswindows()
316316
""
317-
elseif ispath("/proc/sys/fs/binfmt_misc/WSLInterop")
317+
elseif Sys.detectwsl()
318318
distro = get(ENV, "WSL_DISTRO_NAME", "") # See <https://patrickwu.space/wslconf/>
319319
"wsl%24/$distro" # See <https://github.com/microsoft/terminal/pull/14993> and <https://learn.microsoft.com/en-us/windows/wsl/filesystems>
320320
else

0 commit comments

Comments
 (0)