Skip to content

Commit 5b9717f

Browse files
committed
Avoid fragile file checks on WSL CmdStan paths
1 parent f5c8c24 commit 5b9717f

4 files changed

Lines changed: 34 additions & 13 deletions

File tree

R/install.R

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,17 @@ cmdstan_make_local <- function(dir = cmdstan_path(),
327327
}
328328
write(built_flags, file = make_local_path, append = append)
329329
}
330-
if (file.exists(make_local_path)) {
331-
return(trimws(strsplit(trimws(readChar(make_local_path, file.info(make_local_path)$size)), "\n")[[1]]))
332-
} else {
330+
make_local_contents <- tryCatch(
331+
suppressWarnings(readLines(make_local_path, warn = FALSE)),
332+
error = function(e) NULL
333+
)
334+
if (is.null(make_local_contents)) {
333335
return(NULL)
334336
}
337+
if (length(make_local_contents) == 0) {
338+
return("")
339+
}
340+
trimws(strsplit(trimws(paste(make_local_contents, collapse = "\n")), "\n", fixed = TRUE)[[1]])
335341
}
336342

337343
#' @rdname install_cmdstan

R/path.R

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,6 @@ cmdstan_default_path <- function(dir = NULL) {
253253

254254
latest_cmdstan_installed <- function(installs_path) {
255255
cmdstan_installs <- list.dirs(path = installs_path, recursive = FALSE, full.names = FALSE)
256-
# if installed in cmdstan folder with no version move to cmdstan-version folder
257-
if ("cmdstan" %in% cmdstan_installs) {
258-
ver <- read_cmdstan_version(file.path(installs_path, "cmdstan"))
259-
old_path <- file.path(installs_path, "cmdstan")
260-
new_path <- file.path(installs_path, paste0("cmdstan-", ver))
261-
file.rename(old_path, new_path)
262-
cmdstan_installs <- list.dirs(path = installs_path, recursive = FALSE, full.names = FALSE)
263-
}
264256
latest_cmdstan <- ""
265257
if (length(cmdstan_installs) > 0) {
266258
cmdstan_installs <- grep("^cmdstan-", cmdstan_installs, value = TRUE)
@@ -282,15 +274,18 @@ latest_cmdstan_installed <- function(installs_path) {
282274
#' @return Version number as a string.
283275
read_cmdstan_version <- function(path) {
284276
makefile_path <- file.path(path, "makefile")
285-
if (!file.exists(makefile_path)) {
277+
makefile <- tryCatch(
278+
suppressWarnings(readLines(makefile_path, warn = FALSE)),
279+
error = function(e) NULL
280+
)
281+
if (is.null(makefile)) {
286282
warning(
287283
"Can't find CmdStan makefile to detect version number. ",
288284
"Path may not point to valid installation.",
289285
call. = FALSE
290286
)
291287
return(NULL)
292288
}
293-
makefile <- readLines(makefile_path)
294289
version_line <- grep("^CMDSTAN_VERSION :=", makefile, value = TRUE)
295290
if (length(version_line) == 0) {
296291
stop("CmdStan makefile is missing a version number.", call. = FALSE)

tests/testthat/test-path.R

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ test_that("cmdstan_default_path() returns NULL for empty custom install director
203203
expect_null(cmdstan_default_path(dir = installs))
204204
})
205205

206+
test_that("cmdstan_default_path() ignores unversioned cmdstan directory", {
207+
installs <- withr::local_tempdir(pattern = "cmdstan-legacy-installs")
208+
dir.create(file.path(installs, "cmdstan"), recursive = TRUE, showWarnings = FALSE)
209+
dir.create(file.path(installs, "cmdstan-2.36.0"), recursive = TRUE, showWarnings = FALSE)
210+
211+
expect_equal(
212+
cmdstan_default_path(dir = installs),
213+
file.path(installs, "cmdstan-2.36.0")
214+
)
215+
expect_true(dir.exists(file.path(installs, "cmdstan")))
216+
})
217+
206218
test_that("CmdStan version helpers handle invalid inputs", {
207219
expect_identical(cmdstan_min_version(), "2.35.0")
208220
expect_false(is_supported_cmdstan_version(NULL))

tests/testthat/test-utils.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,14 @@ test_that("cmdstan_make_local() works", {
196196
cmdstan_make_local(cpp_options = as.list(exisiting_make_local), append = FALSE)
197197
})
198198

199+
test_that("cmdstan_make_local() preserves empty make/local behavior", {
200+
dir <- withr::local_tempdir()
201+
dir.create(file.path(dir, "make"), recursive = TRUE, showWarnings = FALSE)
202+
file.create(file.path(dir, "make", "local"))
203+
204+
expect_identical(cmdstan_make_local(dir = dir), "")
205+
})
206+
199207
test_that("matching_variables() works", {
200208
ret <- matching_variables(c("beta"), c("alpha", "beta[1]", "beta[2]", "beta[3]"))
201209
expect_equal(

0 commit comments

Comments
 (0)