diff --git a/R/install.R b/R/install.R index 7379e4dfd..dbe95d3e6 100644 --- a/R/install.R +++ b/R/install.R @@ -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 @@ -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 diff --git a/R/run.R b/R/run.R index 5c9f2400b..77b88e73b 100644 --- a/R/run.R +++ b/R/run.R @@ -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 @@ -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) { @@ -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." diff --git a/R/stanc.R b/R/stanc.R index f88de6c14..82911ccef 100644 --- a/R/stanc.R +++ b/R/stanc.R @@ -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 diff --git a/man/install_cmdstan.Rd b/man/install_cmdstan.Rd index aa658cc5f..e9a50eeff 100644 --- a/man/install_cmdstan.Rd +++ b/man/install_cmdstan.Rd @@ -54,8 +54,8 @@ If \code{TRUE} only errors will be printed.} \item{overwrite}{(logical) Should CmdStan still be downloaded and installed even if an installation of the same version is found in \code{dir}? The default -is \code{FALSE}, in which case an informative error is thrown instead of -overwriting the user's installation.} +is \code{FALSE}, in which case the existing installation is left as it is, +with a warning.} \item{timeout}{(positive real) Time limit (in seconds) for each stage of the installation. For \code{install_cmdstan()} the stages are the download of @@ -91,7 +91,9 @@ that the required toolchain is installed and properly configured? The default is \code{TRUE}.} \item{wsl}{(logical) Should CmdStan be installed and run through the Windows -Subsystem for Linux (WSL). The default is \code{FALSE}.} +Subsystem for Linux (WSL). The default is \code{FALSE}. With \code{wsl = TRUE}, +models run inside the Linux distribution, where Smart App Control does +not block them.} \item{copy_make_local}{(logical) Should the \code{make/local} file of the CmdStan installation currently in use be copied to the new installation? The copy diff --git a/tests/testthat/test-model-rebuild-rules.R b/tests/testthat/test-model-rebuild-rules.R index 80f45a34d..315c21f71 100644 --- a/tests/testthat/test-model-rebuild-rules.R +++ b/tests/testthat/test-model-rebuild-rules.R @@ -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() @@ -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 = "")