@@ -88,29 +88,40 @@ func DefaultNumWorkers() int {
8888 return numCPU
8989}
9090
91+ var underWSL struct {
92+ once sync.Once
93+ wsl bool
94+ }
95+
9196// runningUnderWSL returns if we're a Widows executable running in WSL.
9297// See [DefaultToSlash] for an explanation of the heuristics used here.
93- var runningUnderWSL = sync . OnceValue ( func () bool {
98+ func runningUnderWSL () bool {
9499 if runtime .GOOS != "windows" {
95100 return false
96101 }
97- // Best check (but not super fast)
98- if _ , err := os .Lstat ("/proc/sys/fs/binfmt_misc/WSLInterop" ); err == nil {
99- return true
100- }
101- // Fast check, but could provide a false positive if the user sets
102- // this on the Windows side.
103- if os .Getenv ("WSL_DISTRO_NAME" ) != "" {
104- return true
105- }
106- // If the binary is compiled for Windows and we're running under Linux
107- // then honestly just the presence of "/proc/version" should be enough
108- // to determine that we're running under WSL, but check the version
109- // string just to be pedantic.
110- data , _ := os .ReadFile ("/proc/version" )
111- return bytes .Contains (data , []byte ("microsoft" )) ||
112- bytes .Contains (data , []byte ("Microsoft" ))
113- })
102+ w := & underWSL
103+ w .once .Do (func () {
104+ w .wsl = func () bool {
105+ // Best check (but not super fast)
106+ if _ , err := os .Lstat ("/proc/sys/fs/binfmt_misc/WSLInterop" ); err == nil {
107+ return true
108+ }
109+ // Fast check, but could provide a false positive if the user sets
110+ // this on the Windows side.
111+ if os .Getenv ("WSL_DISTRO_NAME" ) != "" {
112+ return true
113+ }
114+ // If the binary is compiled for Windows and we're running under Linux
115+ // then honestly just the presence of "/proc/version" should be enough
116+ // to determine that we're running under WSL, but check the version
117+ // string just to be pedantic.
118+ data , _ := os .ReadFile ("/proc/version" )
119+ return bytes .Contains (data , []byte ("microsoft" )) ||
120+ bytes .Contains (data , []byte ("Microsoft" ))
121+ }()
122+ })
123+ return w .wsl
124+ }
114125
115126// DefaultToSlash returns the default ToSlash value used by the global config
116127// and only applies to Windows. For any other OS this function is a no-op.
@@ -136,10 +147,7 @@ var runningUnderWSL = sync.OnceValue(func() bool {
136147// Additionally, the result of this function is cached the cached value will be
137148// returned on all subsequent calls.
138149func DefaultToSlash () bool {
139- if runtime .GOOS != "windows" {
140- return false
141- }
142- return runningUnderWSL ()
150+ return runtime .GOOS == "windows" && runningUnderWSL ()
143151}
144152
145153// DefaultConfig is the default Config used when none is supplied.
0 commit comments