Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 5 additions & 3 deletions R/install.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
#' If `TRUE` only errors will be printed.
#' @param overwrite (logical) Should CmdStan still be downloaded and installed
#' even if an installation of the same version is found in `dir`? The default
#' is `FALSE`, in which case an informative error is thrown instead of
#' overwriting the user's installation.
#' is `FALSE`, in which case the existing installation is left as it is,
#' with a warning.
#' @param timeout (positive real) Time limit (in seconds) for each stage of
#' the installation. For `install_cmdstan()` the stages are the download of
#' the CmdStan archive, the build of CmdStan and the build of the example
Expand Down Expand Up @@ -79,7 +79,9 @@
#' that the required toolchain is installed and properly configured? The
#' default is `TRUE`.
#' @param wsl (logical) Should CmdStan be installed and run through the Windows
#' Subsystem for Linux (WSL). The default is `FALSE`.
#' Subsystem for Linux (WSL). The default is `FALSE`. With `wsl = TRUE`,
#' models run inside the Linux distribution, where Smart App Control does
#' not block them.
#' @param copy_make_local (logical) Should the `make/local` file of the CmdStan
#' installation currently in use be copied to the new installation? The copy
#' happens before CmdStan is built, so the flags are already in effect for
Expand Down
23 changes: 18 additions & 5 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,11 @@ check_target_exe <- function(exe) {
#' reason (for example "Permission denied") or the executable's own output, and
#' says how to rebuild it, or that there is no Stan file to rebuild it from.
#' When the TBB the build linked against is no longer there it says so, since
#' that's one likely cause and reinstalling it is the other way out.
#' that's one likely cause and reinstalling it is the other way out. When
#' Windows Application Control refused the launch (system error 4551, which
#' Smart App Control and an organization's policy both raise, since a freshly
#' built model is unsigned and unknown to them), it says that instead, because
#' a rebuild would be blocked the same way.
#'
#' @param exe_file Path to the executable.
#' @param stan_file The model's Stan file, empty for a model created from an
Expand All @@ -568,10 +572,11 @@ check_target_exe <- function(exe) {
stop_cannot_run <- function(exe_file, stan_file, reason, tbb_dir = NULL) {
system_error <- regmatches(
reason,
regexec("\\(system error [0-9]+, ([^)]*)\\)", reason)
regexec("\\(system error ([0-9]+), ([^)]*)\\)", reason)
)[[1]]
if (length(system_error) == 2) {
reason <- system_error[[2]]
blocked <- length(system_error) == 3 && system_error[[2]] == "4551"
if (length(system_error) == 3) {
reason <- system_error[[3]]
}
tbb_gone <- !is.null(tbb_dir) && !dir.exists(tbb_dir)
if (tbb_gone) {
Expand All @@ -582,7 +587,15 @@ stop_cannot_run <- function(exe_file, stan_file, reason, tbb_dir = NULL) {
"' no longer exists."
)
}
remedy <- if (length(stan_file) > 0 && tbb_gone) {
remedy <- if (blocked) {
paste0(
"Windows Application Control may be blocking programs it does not ",
"recognize, including models built by CmdStanR. Models cannot run ",
"while Smart App Control is on, unless CmdStan runs through WSL (see ",
"the `wsl` argument of `install_cmdstan()`). If the policy is set by ",
"your organization, ask your administrator."
)
} else if (length(stan_file) > 0 && tbb_gone) {
paste(
"Reinstall it there or run cmdstan_model() with force_recompile = TRUE",
"to rebuild it."
Expand Down
25 changes: 20 additions & 5 deletions R/stanc.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,28 @@
# it, and reading what it printed. The build and the model methods both come
# through here.

# Returns the stanc exe path depending on the OS
#' The stanc command, relative to the CmdStan directory
#'
#' Stops with a clear error if the installation has no stanc. That happens
#' when `rebuild_cmdstan()` deletes it and the build that follows fails.
#' Without the check, processx would only say the command was not found.
#'
#' @return `"bin/stanc"`, with `.exe` on Windows outside WSL.
#' @noRd
stanc_cmd <- function() {
if (os_is_windows() && !os_is_wsl()) {
"bin/stanc.exe"
} else {
"bin/stanc"
cmd <- cmdstan_ext("bin/stanc")
path <- checked_cmdstan_path()
if (!file.exists(file.path(path, cmd))) {
stop(
"The CmdStan installation at '",
path,
"' has no `",
cmd,
"`. Run `rebuild_cmdstan()` to restore it.",
call. = FALSE
)
}
cmd
}

#' Check the user's `stanc_options` for what stanc or CmdStanR would reject
Expand Down
8 changes: 5 additions & 3 deletions man/install_cmdstan.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 55 additions & 0 deletions tests/testthat/test-model-rebuild-rules.R
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,33 @@ test_that("a CmdStan rebuilt in place at a newer version is a rebuild reason", {
expect_equal(cmdstan_version(), "2.39.0")
})

test_that("an installation without stanc is an error that names it", {
skip_if(
os_is_wsl(),
"a Windows directory cannot stand in for a WSL installation"
)
old_path <- cmdstan_path()
withr::defer(set_cmdstan_path(old_path))

install_dir <- withr::local_tempdir()
dir.create(file.path(install_dir, "bin"))
writeLines("CMDSTAN_VERSION := 2.40.0", file.path(install_dir, "makefile"))
stan_file <- local_bernoulli()
set_cmdstan_path(install_dir)

expect_error(
cmdstan_model(stan_file),
paste0(
"The CmdStan installation at '",
cmdstan_path(),
"' has no `",
cmdstan_ext("bin/stanc"),
"`. Run `rebuild_cmdstan()` to restore it."
),
fixed = TRUE
)
})

test_that("dir puts the executable there and a second call reuses it", {
stan_file <- local_bernoulli()
exe_dir <- withr::local_tempdir()
Expand Down Expand Up @@ -498,6 +525,34 @@ test_that("an executable that starts but cannot answer help-all is an error", {
)
})

test_that("a launch blocked by Windows Application Control says so", {
local_mocked_bindings(wsl_compatible_run = function(...) {
stop(
"create process 'C:/models/bern.exe' (system error 4551, An ",
"Application Control policy has blocked this file.) ",
"@win/processx.c:1270 (processx_exec)"
)
})
expect_error(
parse_cmdstan_args(
"C:/models/bern.exe",
"sample",
"C:/models/bern.stan",
tbb_dir = NULL
),
paste0(
"The executable at 'C:/models/bern.exe' could not be run: ",
"An Application Control policy has blocked this file.\n",
"Windows Application Control may be blocking programs it does not ",
"recognize, including models built by CmdStanR. Models cannot run ",
"while Smart App Control is on, unless CmdStan runs through WSL (see ",
"the `wsl` argument of `install_cmdstan()`). If the policy is set by ",
"your organization, ask your administrator."
),
fixed = TRUE
)
})

test_that("a failed launch says when the recorded TBB is gone", {
local_mocked_bindings(wsl_compatible_run = function(...) {
list(status = 127L, stdout = "", stderr = "")
Expand Down
Loading