Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
ba7dbbf
Trying to parallelize tests
VisruthSK Oct 13, 2026
6c8d5fc
Serial WSL, fixed timestamps and recompile
VisruthSK Oct 14, 2026
7fa9b60
Speed up parallel tests
VisruthSK Oct 14, 2026
0b708d4
Speed up parallel tests
VisruthSK Oct 14, 2026
52585c0
Resolve precompiled header names inside the build environment
VisruthSK Oct 14, 2026
8f71f31
Give the knitr test a Stan program no other file writes
VisruthSK Oct 14, 2026
8b6d415
Add -fPIC to SUNDIALS once before workers start
VisruthSK Oct 14, 2026
144e916
Merge v1.0 into parallel-tests-worker-copies
VisruthSK Oct 14, 2026
411bd4a
Another attempt at speeding up; building using up to four cores; atte…
VisruthSK Oct 15, 2026
11cee57
Apply batched suggestions from code review
VisruthSK Oct 15, 2026
bcf4303
Added dep on filelock
VisruthSK Oct 15, 2026
55c05e9
Moved test tesup to setup.R
VisruthSK Oct 15, 2026
d7a9df0
Use a directory lock again, fix WSL toolchain test, wrap long lines
VisruthSK Oct 15, 2026
38b02f9
Take the WSL flag from the CmdStan path when making a private copy
VisruthSK Oct 16, 2026
8d2154c
Run a test early
VisruthSK Oct 16, 2026
68a638b
Small formatting changes
VisruthSK Oct 16, 2026
61b653d
Merge branch 'v1.0' into parallel-tests-worker-copies
VisruthSK Oct 16, 2026
1692ffc
Simplify the CmdStan prebuild in the test setup
VisruthSK Oct 16, 2026
0c0c881
Build the precompiled headers from their full make target
VisruthSK Oct 16, 2026
589d855
Trying filelock again
VisruthSK Oct 16, 2026
5d12deb
Warm up cmdstan once instead of sharing compile cache
VisruthSK Oct 17, 2026
0d93d7a
Prebuild only the precompiled headers in the test warm-up
VisruthSK Oct 17, 2026
e5a202e
Only build the precompiled headers when they are missing
VisruthSK Oct 17, 2026
cc193a5
Run the WSL tests with three workers
VisruthSK Oct 17, 2026
77878f4
Merge branch 'v1.0' into parallel-tests-worker-copies
VisruthSK Oct 20, 2026
0830128
Merge branch 'v1.0' into parallel-tests-worker-copies
VisruthSK Oct 22, 2026
4fbbd83
Addressed some review comments
VisruthSK Oct 23, 2026
711cc10
Only build missing CmdStan targets in the test warm-up
VisruthSK Oct 24, 2026
928f2a8
Build with two cores on WSL and count autodiff-stack check once
VisruthSK Oct 24, 2026
b966bd2
Install CmdStan in the Windows temp directory again in the WSL instal…
VisruthSK Oct 24, 2026
48a1ed2
Merge branch 'v1.0' into parallel-tests-worker-copies
VisruthSK Oct 24, 2026
6b665c4
Run the WSL tests serially
VisruthSK Oct 24, 2026
a7a6a32
Clean up stale CmdStan copies in the test setup and tidy the test docs
VisruthSK Oct 25, 2026
55c527a
Batch test warm-up queries and cap CI job times
VisruthSK Oct 27, 2026
c193e4c
Keep WSL test installations on the Linux filesystem
VisruthSK Oct 27, 2026
91d77d1
Batch WSL file permission checks
VisruthSK Oct 27, 2026
aee1750
Some small docs changes
VisruthSK Oct 27, 2026
6c88f94
Merge branch 'v1.0' into parallel-tests-worker-copies
jgabry Nov 10, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/R-CMD-check-wsl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ jobs:
WSL-R-CMD-check:
if: "! contains(github.event.head_commit.message, '[ci skip]')"
runs-on: windows-latest
timeout-minutes: 240
timeout-minutes: 200

name: windows-latest-WSL2

env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
NOT_CRAN: true
TESTTHAT_PARALLEL: false
CMDSTANR_OPENCL_TESTS: true

steps:
Expand Down
11 changes: 9 additions & 2 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
R-CMD-check:
if: "! contains(github.event.head_commit.message, '[ci skip]')"
runs-on: ${{ matrix.config.os }}
timeout-minutes: 150

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

Expand All @@ -50,6 +51,7 @@ jobs:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
NOT_CRAN: true
TESTTHAT_CPUS: ${{ (matrix.config.os == 'macOS-latest' || matrix.config.os == 'macos-26') && 3 || 4 }}
CMDSTANR_OPENCL_TESTS: ${{ matrix.config.opencl }}
CMDSTAN_TEST_TARBALL_URL: ${{ github.event.inputs.tarball_url }}
PKG_SYSREQS_DB_UPDATE_TIMEOUT: 30s
Expand Down Expand Up @@ -165,15 +167,20 @@ jobs:
- name: Install cmdstan
run: |
cmdstanr::check_cmdstan_toolchain()
cpp_options <- list()
if (Sys.info()[["sysname"]] == "Linux") {
cpp_options <- list("CPPFLAGS_SUNDIALS += -fPIC")
}
tarball_url <- Sys.getenv("CMDSTAN_TEST_TARBALL_URL")
if (nzchar(tarball_url)) {
cmdstanr::install_cmdstan(
cores = 2,
overwrite = TRUE,
release_url = tarball_url
release_url = tarball_url,
cpp_options = cpp_options
)
} else {
cmdstanr::install_cmdstan(cores = 2)
cmdstanr::install_cmdstan(cores = 2, cpp_options = cpp_options)
}
shell: Rscript {0}

Expand Down
8 changes: 7 additions & 1 deletion .github/workflows/Test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
test-coverage:
if: "! contains(github.event.head_commit.message, '[ci skip]')"
runs-on: ${{ matrix.config.os }}
timeout-minutes: 150

name: ${{ matrix.config.os }}

Expand All @@ -34,6 +35,7 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
NOT_CRAN: true
TESTTHAT_CPUS: 4

steps:
- name: cmdstan env vars
Expand All @@ -54,7 +56,11 @@ jobs:
- name: Install cmdstan
run: |
cmdstanr::check_cmdstan_toolchain()
cmdstanr::install_cmdstan(cores = 2)
cpp_options <- list()
if (Sys.info()[["sysname"]] == "Linux") {
cpp_options <- list("CPPFLAGS_SUNDIALS += -fPIC")
}
cmdstanr::install_cmdstan(cores = 2, cpp_options = cpp_options)
shell: Rscript {0}

- name: Test coverage (Linux)
Expand Down
4 changes: 4 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ Imports:
withr (>= 2.5.0)
Suggests:
bayesplot,
filelock,
ggplot2,
knitr (>= 1.37),
loo (>= 2.0.0),
Expand All @@ -73,4 +74,7 @@ Suggests:
vctrs
VignetteBuilder: knitr
Config/testthat/edition: 3
Config/testthat/parallel: true
Config/testthat/start-first: install, model-compile, fit-mcmc,
model-methods, model-rebuild, fit-shared
Config/roxygen2/version: 8.1.1
19 changes: 10 additions & 9 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,14 @@ check_file_exists <- function(files, access = "", ...) {
.wsl_check_exists <- function(path, is_dir = TRUE, access = "") {
path_check <- processx::run(
command = "wsl",
args = c("ls", "-la", wsl_safe_path(path)),
args = c(
"--exec",
"sh",
"-c",
'ls -la "$1" && id -un',
"cmdstanr",
wsl_safe_path(path)
),
error_on_status = FALSE
)

Expand All @@ -782,14 +789,8 @@ check_file_exists <- function(files, access = "", ...) {

path_metadata <- strsplit(path_check$stdout, split = "\n", fixed = TRUE)[[1]]

wsl_user <- processx::run(
command = "wsl",
args = c("echo", "$USER"),
error_on_status = FALSE
)$stdout
wsl_user <- gsub("\n", "", wsl_user, fixed = TRUE)

path_metadata <- grep(wsl_user, path_metadata, value = TRUE)
wsl_user <- utils::tail(path_metadata, 1)
path_metadata <- grep(wsl_user, utils::head(path_metadata, -1), value = TRUE)

if (!is.null(access)) {
path_permissions <- strsplit(path_metadata, " ", fixed = TRUE)[[1]][1]
Expand Down
13 changes: 11 additions & 2 deletions tests/testthat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,17 @@ in `helper-mock-cli.R` stands in for `make` and for the executable's `info`
call, so a test can go through the build code without a compiler.
`mock_cmdstan_model()` wraps it and removes the stand-in executable and its
record when the calling frame ends, so a later real compile of the same
program does not find them. `setup.R` removes everything but the `.stan`
files under `resources/stan/` before and after a run.
program does not find them.

The full suite runs in parallel unless `TESTTHAT_PARALLEL=false`, with
`TESTTHAT_CPUS` workers, so files must not depend on each other or on the order
they run in. Each worker has its own copy of `resources/stan/` in
`tempdir()/stan`, which `setup.R` cleans of everything but the `.stan` files
before and after a run. Before the workers start, the first one to take a lock
in `setup.R` builds the precompiled headers and main objects that all of them
share in the CmdStan installation. A test that changes the installation
(`make/local`, `bin/`, a rebuild) calls `local_private_cmdstan()`, or
`local_cmdstan_make_local()`, which calls it, to work on a per-worker copy.

`test-install.R` installs CmdStan from source, so it can be convenient to run
single tests from it with the `desc` argument of `test_file()` rather than the
Expand Down
73 changes: 70 additions & 3 deletions tests/testthat/helper-envvars-and-paths.R
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,81 @@ local_make_local_backup <- function(envir = parent.frame()) {
invisible(NULL)
}

# Write cpp_options to the make/local of the current CmdStan installation and
# restore its original contents (or absence) when `envir` exits. Called at the
# top level of a test file, the restore runs after all tests in that file.
# Switch to this worker's private CmdStan copy and write cpp_options to its
# make/local. When `envir` exits, restore the original make/local (or its
# absence) and switch back to the shared installation. Called at the top level
# of a test file, the whole file runs on the private copy.
local_cmdstan_make_local <- function(
cpp_options,
envir = parent.frame(),
append = TRUE
) {
local_private_cmdstan(envir = envir)
local_make_local_backup(envir = envir)
cmdstan_make_local(cpp_options = cpp_options, append = append)
}

private_cmdstan <- new.env(parent = emptyenv())

copy_cmdstan <- function(from, to) {
if (os_is_wsl()) {
wsl_compatible_run(
command = "cp",
args = c("-a", wsl_safe_path(from), wsl_safe_path(to))
)
return(invisible(TRUE))
}
if (os_is_windows()) {
status <- system2(
"robocopy",
c(
shQuote(from),
shQuote(to),
"/E",
"/MT:16",
"/NFL",
"/NDL",
"/NJH",
"/NJS",
"/NP"
),
stdout = FALSE
)
failed <- status >= 8
} else {
status <- system2("cp", c("-a", shQuote(from), shQuote(to)))
failed <- status != 0
}
if (failed) {
stop("Could not copy '", from, "' to '", to, "'.", call. = FALSE)
}
invisible(TRUE)
}

local_build_tempdir <- function(envir = parent.frame()) {
if (!os_is_wsl()) {
return(withr::local_tempdir("cmdstan-private-", .local_envir = envir))
}
wsl_dir <- wsl_tempdir()
withr::defer(
wsl_compatible_run(command = "rm", args = c("-rf", wsl_dir)),
envir = envir
)
wsl_safe_path(wsl_dir, revert = TRUE)
}

# Point cmdstan_path() at this worker's private copy of the installation, made
# on first use, until `envir` exits. The copy is deleted at worker teardown.
local_private_cmdstan <- function(envir = parent.frame()) {
shared <- cmdstan_path()
if (is.null(private_cmdstan$path)) {
to <- file.path(
local_build_tempdir(testthat::teardown_env()),
basename(shared)
)
copy_cmdstan(shared, to)
private_cmdstan$path <- to
}
suppressMessages(set_cmdstan_path(private_cmdstan$path))
withr::defer(suppressMessages(set_cmdstan_path(shared)), envir = envir)
}
5 changes: 4 additions & 1 deletion tests/testthat/helper-models.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ testing_data <- function(name) {
}
}
testing_stan_file <- function(name) {
test_path("resources", "stan", paste0(name, ".stan"))
stan_fixture(paste0(name, ".stan"))
}
stan_fixture <- function(...) {
file.path(tempdir(), "stan", ...)
}

local_include_model_with_spaces <- function(.local_envir = parent.frame()) {
Expand Down
59 changes: 57 additions & 2 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
# CI runners have four cores, so run two chains at a time
# Each worker runs two chains at a time; CI runs up to four workers.
options(mc.cores = 2)

stan_dir <- stan_fixture()
dir.create(stan_dir, recursive = TRUE, showWarnings = FALSE)
file.copy(
list.files(test_path("resources", "stan"), full.names = TRUE),
stan_dir,
recursive = TRUE,
overwrite = TRUE
)

cleanup_stan_artifacts <- function() {
all_files_in_stan <- list.files(
test_path("resources", "stan"),
stan_dir,
full.names = TRUE,
recursive = TRUE
)
Expand All @@ -18,3 +27,49 @@ cleanup_stan_artifacts <- function() {

cleanup_stan_artifacts()
withr::defer(cleanup_stan_artifacts(), testthat::teardown_env())

# Build what every worker would otherwise build at once in the shared
# installation: the -fPIC SUNDIALS flag, and the precompiled headers and main
# objects for plain and threaded models. Concurrent builds corrupt these files,
# so the first worker to take the lock builds them and the rest wait.
if (identical(Sys.getenv("NOT_CRAN"), "true")) {
local({
message("Preparing CmdStan in ", cmdstan_path(), " for the tests")
lock_dir <- tools::R_user_dir("cmdstanr", "cache")
dir.create(lock_dir, recursive = TRUE, showWarnings = FALSE)
lock <- filelock::lock(file.path(lock_dir, "test-warm-up.lock"))
withr::defer(filelock::unlock(lock))

# Workers killed during teardown leave their CmdStan copies behind
stale <- Sys.glob(
file.path(dirname(tempdir()), "Rtmp*", "cmdstan-private-*")
)
unlink(stale[file.mtime(stale) < Sys.time() - 86400], recursive = TRUE)

withr::with_options(
list(mc.cores = parallel::detectCores()),
check_sundials_fpic(get_cmdstan_flags("CPPFLAGS_SUNDIALS"), FALSE)
)
for (make_args in list(character(), "STAN_THREADS=true")) {
flags <- c("PRECOMPILED_MODEL_HEADER", "CMDSTAN_MAIN_O")
output <- run_make(
c("-s", make_args, paste0("print-", flags)),
quiet = TRUE
)$stdout
header <- parse_make_print_flag(flags[1], output)
main_o <- parse_make_print_flag(flags[2], output)
targets <- c(
header,
sub("\\.hpp\\.gch$", ".d", header),
main_o,
sub("\\.o$", ".d", main_o)
)
targets <- targets[nzchar(targets)]
# make fails on Windows when a target is already built
missing <- targets[!file.exists(file.path(cmdstan_path(), targets))]
if (length(missing) > 0) {
run_make(c("-j2", missing, make_args), quiet = TRUE)
}
}
})
}
26 changes: 12 additions & 14 deletions tests/testthat/test-install.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ if (!nzchar(cmdstan_test_tarball_url)) {
cmdstan_test_tarball_url <- NULL
}

test_that("install_cmdstan() successfully installs cmdstan", {
dir <- withr::local_tempdir()
test_that("install_cmdstan() successfully installs cmdstan and rebuild_cmdstan() rebuilds it", {
build_cores <- min(parallel::detectCores(), 4)
dir <- local_build_tempdir()
withr::defer(set_cmdstan_path())
expect_message(
expect_output(
install_cmdstan(
dir = dir,
cores = 2,
cores = build_cores,
quiet = FALSE,
overwrite = TRUE,
release_url = cmdstan_test_tarball_url,
Expand All @@ -24,6 +25,11 @@ test_that("install_cmdstan() successfully installs cmdstan", {
"CmdStan path set",
fixed = TRUE
)
expect_output(
rebuild_cmdstan(dir = cmdstan_path(), cores = build_cores),
paste0("CmdStan v", cmdstan_version(), " built"),
fixed = TRUE
)
})

test_that("install_cmdstan() errors if installation already exists", {
Expand Down Expand Up @@ -133,7 +139,7 @@ test_that("install_cmdstan() errors if invalid version or URL", {

test_that("install_cmdstan() installs from release_url", {
skip_if(!is.null(cmdstan_test_tarball_url))
dir <- withr::local_tempdir()
dir <- local_build_tempdir()
url <- paste0(
"https://github.com/stan-dev/cmdstan/releases/download/",
"v2.37.0/cmdstan-2.37.0.tar.gz"
Expand Down Expand Up @@ -211,15 +217,6 @@ test_that("check_cmdstan_toolchain() reports what is missing on Unix", {
expect_error(check_cmdstan_toolchain(), err_msg_cpp, fixed = TRUE)
})

test_that("clean and rebuild works", {
set_cmdstan_path()
expect_output(
rebuild_cmdstan(cores = 2),
paste0("CmdStan v", cmdstan_version(), " built"),
fixed = TRUE
)
})

test_that("github_download_url constructs correct url", {
expect_equal(
github_download_url("FOO"),
Expand Down Expand Up @@ -391,7 +388,7 @@ test_that("download_with_retries() drops GITHUB_PAT after auth failure", {
})

test_that("install_cmdstan() installs from release_file", {
dir <- withr::local_tempdir()
dir <- local_build_tempdir()
destfile <- file.path(withr::local_tempdir(), "cmdstan-2.37.0.tar.gz")
download_with_retries(
paste0(
Expand Down Expand Up @@ -777,6 +774,7 @@ test_that("install_cmdstan() rejects a non-logical copy_make_local", {
})

test_that("cmdstan_make_local() works", {
local_private_cmdstan()
# Backup only, cmdstan_make_local() is the thing being tested.
local_make_local_backup()

Expand Down
Loading
Loading