Skip to content

Commit ab50a29

Browse files
authored
Merge pull request #1183 from stan-dev/wsl-setup-v7-minimal
Minimal WSL setup-v7 CI fix
2 parents a9fcfa7 + 7172d75 commit ab50a29

5 files changed

Lines changed: 72 additions & 12 deletions

File tree

.github/workflows/R-CMD-check-wsl.yaml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ jobs:
3030
CMDSTANR_OPENCL_TESTS: true
3131

3232
steps:
33-
- name: cmdstan env vars
34-
run: |
35-
echo "CMDSTAN_PATH=${HOME}/.cmdstan" >> $GITHUB_ENV
36-
shell: bash
37-
3833
- uses: actions/checkout@v6
3934

4035
- uses: r-lib/actions/setup-r@v2
@@ -44,7 +39,7 @@ jobs:
4439
with:
4540
extra-packages: any::rcmdcheck, local::.
4641

47-
- uses: Vampire/setup-wsl@v6
42+
- uses: Vampire/setup-wsl@v7
4843
with:
4944
distribution: Ubuntu-22.04
5045
wsl-version: 2
@@ -55,12 +50,18 @@ jobs:
5550
run: |
5651
sudo apt-get update
5752
sudo apt-get install -y build-essential libopenmpi-dev ocl-icd-opencl-dev pocl-opencl-icd
53+
sudo chmod 755 /root
5854
shell: wsl-bash {0}
5955

6056
- name: Install cmdstan
6157
run: |
6258
cmdstanr::check_cmdstan_toolchain()
6359
cmdstanr::install_cmdstan(cores = 2, wsl = TRUE, overwrite = TRUE)
60+
cat(
61+
paste0("CMDSTAN=", cmdstanr::cmdstan_path(), "\n"),
62+
file = Sys.getenv("GITHUB_ENV"),
63+
append = TRUE
64+
)
6465
shell: Rscript {0}
6566

6667
- name: Session info

R/path.R

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#' @export
1010
#'
1111
#' @param path (string) The full file path to the CmdStan installation. If
12-
#' `NULL` (the default) then the path is set to the default path used by
12+
#' `NULL` (the default) then the path is set using the `"CMDSTAN"`
13+
#' environment variable when available, otherwise the default path used by
1314
#' [install_cmdstan()] if it exists.
1415
#' @return A string. Either the file path to the CmdStan installation or the
1516
#' CmdStan version number.
@@ -39,7 +40,8 @@
3940
#'
4041
set_cmdstan_path <- function(path = NULL) {
4142
if (is.null(path)) {
42-
path <- cmdstan_default_path()
43+
env_path <- Sys.getenv("CMDSTAN")
44+
path <- if (nzchar(env_path)) env_path else cmdstan_default_path()
4345
}
4446
if (dir.exists(path)) {
4547
path <- absolute_path(path)
@@ -219,22 +221,56 @@ cmdstan_default_path <- function(dir = NULL) {
219221
latest_cmdstan
220222
}
221223

224+
is_wsl_unc_path <- function(path) {
225+
is.character(path) &&
226+
length(path) == 1 &&
227+
!is.na(path) &&
228+
startsWith(repair_path(path), "//wsl$/")
229+
}
230+
231+
cmdstan_version_from_path <- function(path) {
232+
path <- repair_path(path)
233+
match <- regmatches(
234+
path,
235+
regexpr("cmdstan-[0-9]+\\.[0-9]+\\.[0-9]+(?:-rc[0-9]+)?$", path)
236+
)
237+
if (!length(match) || is.na(match) || !nzchar(match)) {
238+
return(NULL)
239+
}
240+
sub("^cmdstan-", "", match)
241+
}
242+
222243

223244
#' Find the version of CmdStan from makefile
224245
#' @noRd
225246
#' @param path Path to installation.
226247
#' @return Version number as a string.
227248
read_cmdstan_version <- function(path) {
228249
makefile_path <- file.path(path, "makefile")
229-
if (!file.exists(makefile_path)) {
250+
if (is_wsl_unc_path(path)) {
251+
makefile <- tryCatch(
252+
suppressWarnings(readLines(makefile_path, warn = FALSE)),
253+
error = function(e) NULL
254+
)
255+
} else if (file.exists(makefile_path)) {
256+
makefile <- readLines(makefile_path)
257+
} else {
258+
makefile <- NULL
259+
}
260+
if (is.null(makefile)) {
261+
if (is_wsl_unc_path(path)) {
262+
version_from_path <- cmdstan_version_from_path(path)
263+
if (!is.null(version_from_path)) {
264+
return(version_from_path)
265+
}
266+
}
230267
warning(
231268
"Can't find CmdStan makefile to detect version number. ",
232269
"Path may not point to valid installation.",
233270
call. = FALSE
234271
)
235272
return(NULL)
236273
}
237-
makefile <- readLines(makefile_path)
238274
version_line <- grep("^CMDSTAN_VERSION :=", makefile, value = TRUE)
239275
if (length(version_line) == 0) {
240276
stop("CmdStan makefile is missing a version number.", call. = FALSE)

man/set_cmdstan_path.Rd

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-install.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ test_that("install_cmdstan() works with version and release_url", {
117117
fixed = TRUE
118118
)
119119
expect_true(dir.exists(file.path(dir, "cmdstan-2.36.0")))
120-
set_cmdstan_path(cmdstan_default_path())
120+
set_cmdstan_path()
121121
})
122122

123123
test_that("toolchain checks on Unix work", {
@@ -143,6 +143,7 @@ test_that("toolchain checks on Unix work", {
143143
})
144144

145145
test_that("clean and rebuild works", {
146+
set_cmdstan_path()
146147
expect_output(
147148
rebuild_cmdstan(cores = CORES),
148149
paste0("CmdStan v", cmdstan_version(), " built"),

tests/testthat/test-path.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ test_that("Setting path from env var is detected", {
4545
expect_false(is.null(.cmdstanr$VERSION))
4646
})
4747

48+
test_that("set_cmdstan_path() uses CMDSTAN env var when path is omitted", {
49+
unset_cmdstan_path()
50+
withr::local_envvar(c(CMDSTAN = PATH))
51+
expect_message(
52+
set_cmdstan_path(),
53+
paste("CmdStan path set to:", PATH),
54+
fixed = TRUE
55+
)
56+
expect_equal(cmdstan_path(), PATH)
57+
})
58+
4859
test_that("Unsupported CmdStan path from env var is rejected", {
4960
unset_cmdstan_path()
5061
.cmdstanr$WSL <- TRUE
@@ -184,6 +195,16 @@ test_that("CmdStan version helpers handle invalid inputs", {
184195
expect_false(is_supported_cmdstan_version("not-a-version"))
185196
})
186197

198+
test_that("CmdStan version can be recovered from WSL UNC install path", {
199+
wsl_path <- "//wsl$/Ubuntu-22.04/root/.cmdstan/cmdstan-2.38.0"
200+
201+
expect_true(is_wsl_unc_path(wsl_path))
202+
expect_equal(cmdstan_version_from_path(wsl_path), "2.38.0")
203+
expect_equal(cmdstan_version_from_path(paste0(wsl_path, "/")), "2.38.0")
204+
expect_equal(suppressWarnings(read_cmdstan_version(wsl_path)), "2.38.0")
205+
expect_null(cmdstan_version_from_path("//wsl$/Ubuntu-22.04/root/.cmdstan/not-cmdstan"))
206+
})
207+
187208
test_that("cmdstan_ext() works", {
188209
if (os_is_windows() && !os_is_wsl()) {
189210
expect_identical(cmdstan_ext(), ".exe")

0 commit comments

Comments
 (0)