stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

#include support in stancjs #1430

Closed WardBrian closed 1 week ago

WardBrian commented 2 weeks ago

Is your feature request related to a problem? Please describe.

stanc.js often runs in environments like v8 where the file system is not available, and so it currently cannot handle #include statements. This limits its ability to be used for tasks like formatting, and compiling requires preprocessing outside of the compiler which leads to bugs and worse error messages.

Describe the solution you'd like

The stanc() function could accept a final argument which is a mapping from file paths to a string of their contents, e.g if i had a stan model foo.stan,

functions {
#include <helpers/bar.stan>
}
// ...

I could invoke stanc() like

stanc("foo", fooCode, arguments, {"helpers/bar.stan": barcode});

Describe alternatives you've considered In contexts where the file system is actually available, like Node, we could just use it directly.

Additional context

Properly preparing a call to the above would require some work based on the include-paths argument the user supplied. Of course, you could over specify the final argument and include the text of all .stan files in the directory - perhaps a bit slower, but would work.

@andrjohns @hsbadr - does this sound like something RStan would use if it was supported

andrjohns commented 1 week ago

It sounds good to me!