Skip to content

Commit

Permalink
Merge pull request #852 from stan-dev/auto-format-suggestion
Browse files Browse the repository at this point in the history
suggest format method after error due to old syntax
  • Loading branch information
jgabry authored Sep 15, 2023
2 parents 22ee5fc + 480a775 commit 17678d5
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
42 changes: 33 additions & 9 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -529,26 +529,24 @@ compile <- function(quiet = TRUE,
cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(user_header))
stanc_options[["allow-undefined"]] <- TRUE
private$using_user_header_ <- TRUE
}
else if (!is.null(cpp_options[["USER_HEADER"]])) {
if(!is.null(cpp_options[["user_header"]])) {
} else if (!is.null(cpp_options[["USER_HEADER"]])) {
if (!is.null(cpp_options[["user_header"]])) {
warning('User header specified both via cpp_options[["USER_HEADER"]] and cpp_options[["user_header"]].', call. = FALSE)
}

user_header <- cpp_options[["USER_HEADER"]]
cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(cpp_options[["USER_HEADER"]]))
private$using_user_header_ <- TRUE
}
else if (!is.null(cpp_options[["user_header"]])) {
} else if (!is.null(cpp_options[["user_header"]])) {
user_header <- cpp_options[["user_header"]]
cpp_options[["user_header"]] <- wsl_safe_path(absolute_path(cpp_options[["user_header"]]))
private$using_user_header_ <- TRUE
}


if(!is.null(user_header)) {
if (!is.null(user_header)) {
user_header <- absolute_path(user_header) # As mentioned above, just absolute, not wsl_safe_path()
if(!file.exists(user_header)) {
if (!file.exists(user_header)) {
stop(paste0("User header file '", user_header, "' does not exist."), call. = FALSE)
}
}
Expand Down Expand Up @@ -688,8 +686,12 @@ compile <- function(quiet = TRUE,
)
)
if (is.na(run_log$status) || run_log$status != 0) {
stop("An error occured during compilation! See the message above for more information.",
call. = FALSE)
err_msg <- "An error occured during compilation! See the message above for more information."
if (grepl("auto-format flag to stanc", run_log$stderr)) {
format_msg <- "\nTo fix deprecated or removed syntax please see ?cmdstanr::format for an example."
err_msg <- paste(err_msg, format_msg)
}
stop(err_msg, call. = FALSE)
}
if (file.exists(exe)) {
file.remove(exe)
Expand Down Expand Up @@ -932,6 +934,28 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax)
#'
#' @examples
#' \dontrun{
#'
#' # Example of fixing old syntax
#' # real x[2] --> array[2] real x;
#' file <- write_stan_file("
#' parameters {
#' real x[2];
#' }
#' model {
#' x ~ std_normal();
#' }
#' ")
#'
#' # set compile=FALSE then call format to fix old syntax
#' mod <- cmdstan_model(file, compile = FALSE)
#' mod$format(canonicalize = list("deprecations"))
#'
#' # overwrite the original file instead of just printing it
#' mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE)
#' mod$compile()
#'
#'
#' # Example of removing unnecessary whitespace
#' file <- write_stan_file("
#' data {
#' int N;
Expand Down
22 changes: 22 additions & 0 deletions man/model-method-format.Rd

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

6 changes: 6 additions & 0 deletions tests/testthat/resources/stan/old_array_syntax.stan
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters {
real x[3];
}
model {
x ~ normal(0, 1);
}
9 changes: 9 additions & 0 deletions tests/testthat/test-model-compile.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ test_that("compile errors are shown", {
)
})

test_that("compile suggests using format to fix old syntax", {
stan_file <- testing_stan_file("old_array_syntax")
expect_error(
cmdstan_model(stan_file),
"To fix deprecated or removed syntax please see ?cmdstanr::format for an example.",
fixed = TRUE
)
})

test_that("dir arg works for cmdstan_model and $compile()", {
tmp_dir <- tempdir()
tmp_dir_2 <- tempdir()
Expand Down

0 comments on commit 17678d5

Please sign in to comment.