Skip to content

Commit 9bcafb6

Browse files
committed
Extract exe path logic
1 parent 541f36c commit 9bcafb6

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

R/model.R

+37-14
Original file line numberDiff line numberDiff line change
@@ -528,19 +528,7 @@ compile <- function(quiet = TRUE,
528528
warning("'compile_hessian_method' is deprecated. The hessian method is compiled with all models.")
529529
}
530530

531-
if (length(self$exe_file()) == 0) {
532-
if (is.null(dir)) {
533-
exe_base <- self$stan_file()
534-
} else {
535-
exe_base <- file.path(dir, basename(self$stan_file()))
536-
}
537-
exe <- cmdstan_ext(strip_ext(exe_base))
538-
if (dir.exists(exe)) {
539-
stop("There is a subfolder matching the model name in the same folder as the model! Please remove or rename the subfolder and try again.", call. = FALSE)
540-
}
541-
} else {
542-
exe <- self$exe_file()
543-
}
531+
exe <- resolve_exe_path(dir, private$dir_, self$exe_file(), self$stan_file())
544532

545533
# Resolve stanc and cpp options
546534
if (pedantic) {
@@ -588,6 +576,7 @@ compile <- function(quiet = TRUE,
588576
# - the executable does not exist
589577
# - the stan model was changed since last compilation
590578
# - a user header is used and the user header changed since last compilation (#813)
579+
self$exe_file(exe)
591580
if (!file.exists(exe)) {
592581
force_recompile <- TRUE
593582
} else if (file.exists(self$stan_file())
@@ -608,7 +597,6 @@ compile <- function(quiet = TRUE,
608597
private$precompile_stanc_options_ <- NULL
609598
private$precompile_include_paths_ <- NULL
610599
self$functions$existing_exe <- TRUE
611-
self$exe_file(exe)
612600
return(invisible(self))
613601
} else {
614602
if (rlang::is_interactive()) {
@@ -2436,3 +2424,38 @@ model_compile_info <- function(exe_file, version) {
24362424
is_variables_method_supported <- function(mod) {
24372425
cmdstan_version() >= "2.27.0" && mod$has_stan_file() && file.exists(mod$stan_file())
24382426
}
2427+
resolve_exe_path <- function(
2428+
dir = NULL, private_dir = NULL, self_exe_file = NULL, self_stan_file = NULL
2429+
) {
2430+
if (is.null(dir) && !is.null(private_dir)) {
2431+
dir <- absolute_path(private_dir)
2432+
} else if (!is.null(dir)) {
2433+
dir <- absolute_path(dir)
2434+
}
2435+
if (!is.null(dir)) {
2436+
dir <- repair_path(dir)
2437+
assert_dir_exists(dir, access = "rw")
2438+
if (length(self_exe_file) != 0) {
2439+
self_exe_file <- file.path(dir, basename(self_exe_file))
2440+
}
2441+
}
2442+
if (length(self_exe_file) == 0) {
2443+
if (is.null(dir)) {
2444+
exe_base <- self_stan_file
2445+
} else {
2446+
exe_base <- file.path(dir, basename(self_stan_file))
2447+
}
2448+
exe <- cmdstan_ext(strip_ext(exe_base))
2449+
if (dir.exists(exe)) {
2450+
stop(
2451+
"There is a subfolder matching the model name ",
2452+
"in the same folder as the model! ",
2453+
"Please remove or rename the subfolder and try again.",
2454+
call. = FALSE
2455+
)
2456+
}
2457+
} else {
2458+
exe <- self_exe_file
2459+
}
2460+
exe
2461+
}

0 commit comments

Comments
 (0)