stan-dev / cmdstanr

CmdStanR: the R interface to CmdStan
https://mc-stan.org/cmdstanr/
Other
143 stars 63 forks source link

Models with #include fail at run-time when project has spaces in path #820

Open mike-lawrence opened 1 year ago

mike-lawrence commented 1 year ago

Describe the bug Models with #include fail at run-time when project has spaces in path. I previously didn't encounter this behaviour with the same code, so I believe it is new but I'm unsure which versions of cmdstan/cmdstanr I last had it working fine with.

To Reproduce

library(tidyverse)
library(cmdstanr)

if(getwd() %>% str_detect(' ')){
    stop("Please run this code from a working directory with no spaces in it's path")
}

# write an include file
include_file_path = fs::path('stan','includes','data',ext='stan')
include_file_path %>% fs::path_dir() %>% fs::dir_create()
write_lines(
    file = include_file_path
    , x = 'data{ int n; vector[n] x ; }'
)
cat(read_file(include_file_path))

# write model using said include
mod_file_path = fs::path('stan','mod',ext='stan')
"
#include includes/data.stan
parameters{
    real mu ;
    real<lower=0> sigma ;
}
model{
    mu ~ std_normal() ;
    sigma ~ weibull(2,1) ;
    x ~ normal(mu,sigma) ;
}
" %>% write_lines(
    file = mod_file_path
    , x = .
)
cat(read_file(mod_file_path),sep='\n')

mod = cmdstan_model(mod_file_path)
mod$sample(
    data = lst(n=10,x=rnorm(n))
)
#samples fine

#now copy everything to a subdir with a space in the path:
fs::dir_copy('stan','path with spaces/stan')
setwd('path with spaces') #important!
mod = cmdstan_model(mod_file_path,force_recompile=T)
mod$sample(
    data = lst(n=10,x=rnorm(n))
)
#fails with error.

Error encountered:

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):
    -------------------------------------------------
    1:  
    2:  #include includes/data.stan
    ^
    3:  parameters{
        4:      real mu ;
        -------------------------------------------------

            Could not find include file 'includes/data.stan' in specified include paths.

Operating system Ubuntu 22.04.3 LTS

CmdStan version number 2.32.2

CmdStanR version number 0.6.0

mike-lawrence commented 1 year ago

@adrian-lison , is it possible that your edits here could be driving this new behaviour?