diff --git a/R/model.R b/R/model.R index 25f8dc111..64fa52087 100644 --- a/R/model.R +++ b/R/model.R @@ -405,6 +405,9 @@ compile <- function(quiet = TRUE, exe_base <- file.path(dir, basename(self$stan_file())) } exe <- cmdstan_ext(paste0(strip_ext(exe_base), exe_suffix)) + if (dir.exists(exe)) { + 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) + } model_name <- sub(" ", "_", paste0(strip_ext(basename(self$stan_file())), "_model")) # compile if: diff --git a/tests/testthat/test-model-compile.R b/tests/testthat/test-model-compile.R index 6d60e9a17..68fc17ecb 100644 --- a/tests/testthat/test-model-compile.R +++ b/tests/testthat/test-model-compile.R @@ -420,3 +420,26 @@ test_that("check_syntax() works with pedantic=TRUE", { ) }) +test_that("compiliation errors if folder with the model name exists", { + skip_on_cran() + skip_if(os_is_windows()) + model_code <- " + parameters { + real y; + } + model { + y ~ std_normal(); + } + " + stan_file <- write_stan_file(model_code) + exe <- strip_ext(stan_file) + if (!dir.exists(exe)) { + if (file.exists(exe)) { + file.remove(exe) + } + dir.create(exe) + } + expect_error(cmdstan_model(stan_file), + "There is a subfolder matching the model name in the same folder as the model! Please remove or rename the subfolder and try again.") +}) +