Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggest format method after error due to old syntax #852

Merged
merged 6 commits into from
Sep 15, 2023
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
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
Loading