Skip to content

Commit e50e477

Browse files
committed
Merge branch 'pr/836' into auto-format-suggestion
2 parents 7a94882 + 121abd0 commit e50e477

4 files changed

Lines changed: 134 additions & 136 deletions

File tree

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ Imports:
3939
posterior (>= 1.4.1),
4040
processx (>= 3.5.0),
4141
R6 (>= 2.4.0),
42-
withr (>= 2.5.0)
42+
withr (>= 2.5.0),
43+
rlang (>= 0.4.7)
4344
Suggests:
4445
bayesplot,
4546
knitr (>= 1.37),
4647
loo (>= 2.0.0),
47-
rlang (>= 0.4.7),
4848
rmarkdown,
4949
testthat (>= 2.1.0),
5050
Rcpp,

R/model.R

Lines changed: 75 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ compile <- function(quiet = TRUE,
568568
}
569569

570570
if (!force_recompile) {
571-
if (interactive()) {
571+
if (rlang::is_interactive()) {
572572
message("Model executable is up to date!")
573573
}
574574
private$cpp_options_ <- cpp_options
@@ -579,7 +579,7 @@ compile <- function(quiet = TRUE,
579579
self$exe_file(exe)
580580
return(invisible(self))
581581
} else {
582-
if (interactive()) {
582+
if (rlang::is_interactive()) {
583583
message("Compiling Stan program...")
584584
}
585585
}
@@ -630,93 +630,92 @@ compile <- function(quiet = TRUE,
630630

631631
stancflags_val <- paste0("STANCFLAGS += ", stancflags_val, paste0(" ", stancflags_combined, collapse = " "))
632632

633-
if (dry_run) {
634-
return(invisible(self))
635-
}
633+
if (!dry_run) {
636634

637-
if (compile_standalone) {
638-
expose_stan_functions(self$functions, !quiet)
639-
}
635+
if (compile_standalone) {
636+
expose_stan_functions(self$functions, !quiet)
637+
}
640638

641-
withr::with_path(
642-
c(
643-
toolchain_PATH_env_var(),
644-
tbb_path()
645-
),
646-
run_log <- wsl_compatible_run(
647-
command = make_cmd(),
648-
args = c(wsl_safe_path(tmp_exe),
649-
cpp_options_to_compile_flags(cpp_options),
650-
stancflags_val),
651-
wd = cmdstan_path(),
652-
echo = !quiet || is_verbose_mode(),
653-
echo_cmd = is_verbose_mode(),
654-
spinner = quiet && interactive() && !identical(Sys.getenv("IN_PKGDOWN"), "true"),
655-
stderr_callback = function(x, p) {
656-
if (!startsWith(x, paste0(make_cmd(), ": *** No rule to make target"))) {
657-
message(x)
658-
}
659-
if (grepl("PCH file", x) || grepl("precompiled header", x) || grepl(".hpp.gch", x) ) {
660-
warning(
661-
"CmdStan's precompiled header (PCH) files may need to be rebuilt.\n",
662-
"If your model failed to compile please run rebuild_cmdstan().\n",
663-
"If the issue persists please open a bug report.",
664-
call. = FALSE
665-
)
666-
}
667-
if (grepl("No space left on device", x) || grepl("error in backend: IO failure on output stream", x)) {
668-
warning(
669-
"The C++ compiler ran out of disk space and was unable to build the executables for your model!\n",
670-
"See the above error for more details.",
671-
call. = FALSE
672-
)
673-
}
674-
if (os_is_macos()) {
675-
if (R.version$arch == "aarch64"
676-
&& grepl("but the current translation unit is being compiled for target", x)) {
639+
withr::with_path(
640+
c(
641+
toolchain_PATH_env_var(),
642+
tbb_path()
643+
),
644+
run_log <- wsl_compatible_run(
645+
command = make_cmd(),
646+
args = c(wsl_safe_path(tmp_exe),
647+
cpp_options_to_compile_flags(cpp_options),
648+
stancflags_val),
649+
wd = cmdstan_path(),
650+
echo = !quiet || is_verbose_mode(),
651+
echo_cmd = is_verbose_mode(),
652+
spinner = quiet && rlang::is_interactive() && !identical(Sys.getenv("IN_PKGDOWN"), "true"),
653+
stderr_callback = function(x, p) {
654+
if (!startsWith(x, paste0(make_cmd(), ": *** No rule to make target"))) {
655+
message(x)
656+
}
657+
if (grepl("PCH file", x) || grepl("precompiled header", x) || grepl(".hpp.gch", x) ) {
677658
warning(
678-
"The C++ compiler has errored due to incompatibility between the x86 and ",
679-
"Apple Silicon architectures.\n",
680-
"If you are running R inside an IDE (RStudio, VSCode, ...), ",
681-
"make sure the IDE is a native Apple Silicon app.\n",
659+
"CmdStan's precompiled header (PCH) files may need to be rebuilt.\n",
660+
"If your model failed to compile please run rebuild_cmdstan().\n",
661+
"If the issue persists please open a bug report.",
682662
call. = FALSE
683663
)
684664
}
685-
}
686-
},
687-
error_on_status = FALSE
665+
if (grepl("No space left on device", x) || grepl("error in backend: IO failure on output stream", x)) {
666+
warning(
667+
"The C++ compiler ran out of disk space and was unable to build the executables for your model!\n",
668+
"See the above error for more details.",
669+
call. = FALSE
670+
)
671+
}
672+
if (os_is_macos()) {
673+
if (R.version$arch == "aarch64"
674+
&& grepl("but the current translation unit is being compiled for target", x)) {
675+
warning(
676+
"The C++ compiler has errored due to incompatibility between the x86 and ",
677+
"Apple Silicon architectures.\n",
678+
"If you are running R inside an IDE (RStudio, VSCode, ...), ",
679+
"make sure the IDE is a native Apple Silicon app.\n",
680+
call. = FALSE
681+
)
682+
}
683+
}
684+
},
685+
error_on_status = FALSE
686+
)
688687
)
689-
)
690-
if (is.na(run_log$status) || run_log$status != 0) {
691-
err_msg <- "An error occured during compilation! See the message above for more information."
692-
if (grepl("auto-format flag to stanc", run_log$stderr)) {
693-
format_msg <- "\nTo fix deprecated or removed syntax please see ?cmdstanr::format for an example."
694-
err_msg <- paste(err_msg, format_msg)
688+
if (is.na(run_log$status) || run_log$status != 0) {
689+
stop("An error occured during compilation! See the message above for more information.",
690+
call. = FALSE)
695691
}
696-
stop(err_msg, call. = FALSE)
697-
}
698-
if (file.exists(exe)) {
699-
file.remove(exe)
700-
}
701-
file.copy(tmp_exe, exe, overwrite = TRUE)
702-
if (os_is_wsl()) {
703-
res <- processx::run(
704-
command = "wsl",
705-
args = c("chmod", "+x", wsl_safe_path(exe)),
706-
error_on_status = FALSE
707-
)
708-
}
692+
if (file.exists(exe)) {
693+
file.remove(exe)
694+
}
695+
file.copy(tmp_exe, exe, overwrite = TRUE)
696+
if (os_is_wsl()) {
697+
res <- processx::run(
698+
command = "wsl",
699+
args = c("chmod", "+x", wsl_safe_path(exe)),
700+
error_on_status = FALSE
701+
)
702+
}
703+
} # End - if(!dry_run)
704+
709705
private$exe_file_ <- exe
710706
private$cpp_options_ <- cpp_options
711707
private$precompile_cpp_options_ <- NULL
712708
private$precompile_stanc_options_ <- NULL
713709
private$precompile_include_paths_ <- NULL
714710
private$model_methods_env_ <- new.env()
715-
suppressWarnings(private$model_methods_env_$hpp_code_ <- readLines(private$hpp_file_, warn = FALSE))
716-
if (compile_model_methods) {
717-
expose_model_methods(env = private$model_methods_env_,
718-
verbose = !quiet,
719-
hessian = compile_hessian_method)
711+
712+
if(!dry_run) {
713+
suppressWarnings(private$model_methods_env_$hpp_code_ <- readLines(private$hpp_file_, warn = FALSE))
714+
if (compile_model_methods) {
715+
expose_model_methods(env = private$model_methods_env_,
716+
verbose = !quiet,
717+
hessian = compile_hessian_method)
718+
}
720719
}
721720
invisible(self)
722721
}
@@ -878,7 +877,7 @@ check_syntax <- function(pedantic = FALSE,
878877
wd = cmdstan_path(),
879878
echo = is_verbose_mode(),
880879
echo_cmd = is_verbose_mode(),
881-
spinner = quiet && interactive(),
880+
spinner = quiet && rlang::is_interactive(),
882881
stderr_callback = function(x, p) {
883882
message(x)
884883
},

tests/testthat/helper-custom-expectations.R

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,21 @@ expect_compilation <- function(mod, ...) {
1616
invisible(mod)
1717
}
1818

19+
#' Check compilation from a call (expecting a constructor call, but not necessarily).
20+
#' @param constructor_call a call returning a CmdStanModel object that should have been compiled
21+
#' @return the newly created model
22+
expect_call_compilation <- function(constructor_call) {
23+
before_time <- Sys.time()
24+
mod <- expect_interactive_message(constructor_call, "Compiling Stan program...")
25+
if(length(mod$exe_file()) == 0 || !file.exists(mod$exe_file())) {
26+
fail(sprint("Model executable '%s' does not exist after compilation.", mod$exe_file()))
27+
}
28+
after_mtime <- file.mtime(mod$exe_file())
29+
expect(before_time <= after_mtime, sprintf("Exe file '%s' has old timestamp, despite expecting (re)compilation", mod$exe_file()))
30+
invisible(mod)
31+
}
32+
33+
1934
#' @param ... arguments passed to mod$compile()
2035
expect_no_recompilation <- function(mod, ...) {
2136
if(length(mod$exe_file()) == 0 || !file.exists(mod$exe_file())) {
@@ -70,14 +85,11 @@ expect_gq_output <- function(object, num_chains = NULL) {
7085
}
7186

7287
expect_interactive_message <- function(object, regexp = NULL) {
73-
# Non-interactive message suppression failing under Windows CI,
74-
# temporarily skip message check only on Windows
75-
if (os_is_windows() && !os_is_wsl()) {
76-
return(object)
77-
}
78-
if (interactive()) {
79-
expect_message(object = object, regexp = regexp)
80-
} else {
81-
expect_silent(object = object)
82-
}
88+
rlang::with_interactive(value = TRUE,
89+
expect_message(object = object, regexp = regexp))
90+
}
91+
92+
expect_noninteractive_silent <- function(object) {
93+
rlang::with_interactive(value = FALSE,
94+
expect_silent(object))
8395
}

0 commit comments

Comments
 (0)