Skip to content

include_paths dropped for pre-compiled models in subdirectories #1094

Open
@wlandau

Description

@wlandau

Apologies if this is a duplicate of #550 or #687, but I have a case where include_paths is not recognized for a pre-compiled model in a subdirectory. Users of instantiate are having trouble building packages with pre-compiled models split over multiple Stan files (c.f. wlandau/instantiate#28).

Here's the full context. With these files:

fs::dir_tree("bin")
bin
└── stan
    ├── bernoulli.stan
    └── utils
        └── silly.stan

bin/stan/bernoulli.stan:

#include utils/silly.stan
data {
  int<lower=0> N;
  array[N] int<lower=0,upper=1> y;
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  theta ~ beta(1,1);  // uniform prior on interval 0,1
  y ~ bernoulli(theta);
}
generated quantities {
  real theta_lin;
  theta_lin = silly_logit(theta);
}

and bin/stan/utils/silly.stan:

functions {
  real silly_logit(real x) {
    real out;
    out = logit(x);
    return out;
  }
}

and R code to compile and reuse the model:

library(cmdstanr)
#> This is cmdstanr version 0.9.0
#> - CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
#> - CmdStan path: /Users/C240390/.cmdstan/cmdstan-2.36.0
#> - CmdStan version: 2.36.0
model <- cmdstan_model(
  stan_file = "bin/stan/bernoulli.stan",
  exe_file = "bin/stan/bernoulli",
  compile = TRUE,
  include_paths = "bin/stan"
)

rstudioapi::restartSession()

library(cmdstanr)
model <- cmdstan_model(
  stan_file = "bin/stan/bernoulli.stan",
  exe_file = "bin/stan/bernoulli",
  compile = FALSE,
  include_paths = "bin/stan"
)
y <- c(0, 1, 1, 0, 1, 0)
samples <- model$sample(data = list(N = length(y), y = y))

I get this error:

Error in `(function (command = NULL, args = character(), error_on_status = TRUE, …`:
! System command 'stanc' failed
---
Exit status: 1
Stderr (last 10 lines, see `$stderr` for more):
    34:  */
    35:  
    36:  #include utils/silly.stan
         ^
    37:  data {
    38:    int<lower=0> N;
   -------------------------------------------------

Could not find include file 'utils/silly.stan' in specified include paths.
Current include paths: None

Same if I set include_paths to "bin/stan/utils", file.path(getwd(), "bin/stan"), or file.path(getwd(), "bin/stan/utils").

R version 4.5.0 (2025-04-11)
Platform: aarch64-apple-darwin20
Running under: macOS Sequoia 15.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.12.1

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Indiana/Indianapolis
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] cmdstanr_0.9.0

loaded via a namespace (and not attached):
 [1] tensorA_0.36.2.1     backports_1.5.0      R6_2.6.1             xfun_0.52           
 [5] magrittr_2.0.3       posterior_1.6.1      glue_1.8.0           abind_1.4-8         
 [9] tibble_3.2.1         knitr_1.50           pkgconfig_2.0.3      generics_0.1.3      
[13] lifecycle_1.0.4      ps_1.9.1             cli_3.6.4            processx_3.8.6      
[17] vctrs_0.6.5          withr_3.0.2          compiler_4.5.0       distributional_0.5.0
[21] rstudioapi_0.17.1    tools_4.5.0          checkmate_2.3.2      evaluate_1.0.3      
[25] pillar_1.10.2        jsonlite_2.0.0       rlang_1.1.6  

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions