Skip to content

Commit c87879a

Browse files
committed
Initial fix
1 parent 22b391e commit c87879a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

R/model.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,17 @@ variables <- function() {
653653
}
654654
assert_stan_file_exists(self$stan_file())
655655
if (is.null(private$variables_) && file.exists(self$stan_file())) {
656-
private$variables_ <- model_variables(self$stan_file(), self$include_paths(), allow_undefined = private$using_user_header_)
656+
private$variables_ <- model_variables(
657+
stan_file = self$stan_file(),
658+
include_paths = {
659+
if (length(self$exe_file()) > 0 && file.exists(self$exe_file())) {
660+
self$include_paths()
661+
} else {
662+
private$precompile_include_paths_
663+
}
664+
},
665+
allow_undefined = private$using_user_header_
666+
)
657667
}
658668
private$variables_
659669
}

tests/testthat/test-model-variables.R

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,36 @@ test_that("$variables() errors on no stan_file", {
108108
fixed = TRUE
109109
)
110110
})
111+
112+
test_that("$variables() works with #includes, both pre and post compilation.", {
113+
114+
data_code <- "
115+
data {
116+
int N;
117+
}
118+
"
119+
model_code <- "
120+
#include data.stan
121+
parameters {
122+
vector[N] y;
123+
}
124+
model {
125+
y ~ std_normal();
126+
}
127+
"
128+
129+
model_file <- write_stan_file(code = model_code)
130+
data_file <- write_stan_file(code = data_code, basename = "data.stan")
131+
132+
mod <- cmdstan_model(
133+
stan_file = model_file,
134+
include_paths = dirname(data_file),
135+
compile = FALSE
136+
)
137+
138+
vars_pre <- mod$variables()
139+
mod$compile()
140+
vars_post <- mod$variables()
141+
142+
expect_equal(vars_pre, vars_post)
143+
})

0 commit comments

Comments
 (0)