Skip to content

Commit

Permalink
base: add Sys.detectwsl() (#57069)
Browse files Browse the repository at this point in the history
Close #36425, fix #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]>
  • Loading branch information
5 people authored Jan 21, 2025
1 parent 4e13e0e commit 323ca86
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
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

0 comments on commit 323ca86

Please sign in to comment.