@@ -267,17 +267,65 @@ latest_cmdstan_installed <- function(installs_path) {
267267 latest_cmdstan
268268}
269269
270+ is_wsl_unc_path <- function (path ) {
271+ is.character(path ) &&
272+ length(path ) == 1 &&
273+ ! is.na(path ) &&
274+ startsWith(repair_path(path ), " //wsl$/" )
275+ }
276+
277+ # Extract the distro name from a WSL UNC path like //wsl$/Ubuntu-22.04/...
278+ wsl_unc_distro_name <- function (path ) {
279+ sub(" ^//wsl\\ $/([^/]+).*$" , " \\ 1" , repair_path(path ))
280+ }
281+
282+ # Convert a WSL UNC path to the corresponding Linux path within the distro.
283+ wsl_unc_path_to_linux <- function (path ) {
284+ sub(" ^//wsl\\ $/[^/]+" , " " , repair_path(path ))
285+ }
286+
287+ read_lines_direct <- function (path ) {
288+ tryCatch(
289+ suppressWarnings(readLines(path , warn = FALSE )),
290+ error = function (e ) NULL
291+ )
292+ }
293+
294+ # Fall back to reading through `wsl` when Windows R can't read a WSL UNC path.
295+ read_lines_via_wsl <- function (path ) {
296+ file_contents <- processx :: run(
297+ command = " wsl" ,
298+ args = c(" -d" , wsl_unc_distro_name(path ), " cat" , wsl_unc_path_to_linux(path )),
299+ error_on_status = FALSE
300+ )
301+ if (file_contents $ status != 0 ) {
302+ return (NULL )
303+ }
304+ if (! nzchar(file_contents $ stdout )) {
305+ return (character (0 ))
306+ }
307+ con <- textConnection(file_contents $ stdout )
308+ on.exit(close(con ), add = TRUE )
309+ readLines(con , warn = FALSE )
310+ }
311+
312+ # Preserve existing direct reads and only use the WSL fallback when needed.
313+ read_lines_with_wsl_fallback <- function (path ) {
314+ file_contents <- read_lines_direct(path )
315+ if (! is.null(file_contents ) || ! is_wsl_unc_path(path )) {
316+ return (file_contents )
317+ }
318+ read_lines_via_wsl(path )
319+ }
320+
270321
271322# ' Find the version of CmdStan from makefile
272323# ' @noRd
273324# ' @param path Path to installation.
274325# ' @return Version number as a string.
275326read_cmdstan_version <- function (path ) {
276327 makefile_path <- file.path(path , " makefile" )
277- makefile <- tryCatch(
278- suppressWarnings(readLines(makefile_path , warn = FALSE )),
279- error = function (e ) NULL
280- )
328+ makefile <- read_lines_with_wsl_fallback(makefile_path )
281329 if (is.null(makefile )) {
282330 warning(
283331 " Can't find CmdStan makefile to detect version number. " ,
0 commit comments