Error when trying to create R package that involves stan, using rstantools

As I want to create an R package that uses rstan, I first tried the toy package shown in this tutorial, which explains how to use the rstantools package. However the following mistake appears when I run the function roxygen2::roxygenize() the second time:

C:\Users\ahauser\AppData\Local\Temp\cc19bxIs.s: Assembler messages:
   C:\Users\ahauser\AppData\Local\Temp\cc19bxIs.s: Fatal error: can't write 87 bytes to section .text of stanExports_lm.o: 'file too big'
   as: stanExports_lm.o: too many sections (38862)
   C:\Users\ahauser\AppData\Local\Temp\cc19bxIs.s: Fatal error: stanExports_lm.o: file too big
   make: *** [C:/PROGRA~1/R/R-42~1.0/etc/x64/Makeconf:257: stanExports_lm.o] Error 1
   ERROR: compilation failed for package 'rstanlm'
─  removing 'C:/Users/ahauser/AppData/Local/Temp/RtmpasF7nw/devtools_install_5d06aa03fb2/rstanlm'
Error in c("(function (command = NULL, args = character(), error_on_status = TRUE, ",  : 
  ! System command 'Rcmd.exe' failed

Here is what the .Last.error.trace says:

Stack trace:

 1. roxygen2::roxygenize()
 2. roxygen2:::load_code(base_path)
 3. pkgload::load_all(path, helpers = FALSE, attach_testthat = FALSE)
 4. pkgbuild::compile_dll(path, quiet = quiet)
 5. withr::with_makevars(compiler_flags(debug), assignment = "+=",  ...
 6. withr:::with_envvar(c(R_MAKEVARS_USER = makevars_file), { ...
 7. base:::force(code)
 8. base:::force(code)
 9. pkgbuild:::install_min(path, dest = install_dir, components = "libs",  ...
 10. pkgbuild:::rcmd_build_tools("INSTALL", c(path, paste("--library=",  ...
 11. pkgbuild:::with_build_tools({ ...
 12. withr::with_path(rtools_path(), code)
 13. base:::force(code)
 14. base:::withCallingHandlers(callr::rcmd_safe(..., env = env, spinner = FALSE,  ...
 15. callr::rcmd_safe(..., env = env, spinner = FALSE, show = FALSE,  ...
 16. callr:::run_r(options)
 17. base:::with(options, with_envvar(env, do.call(processx::run,  ...
 18. base:::with.default(options, with_envvar(env, do.call(processx::run,  ...
 19. base:::eval(substitute(expr), data, enclos = parent.frame())
 20. base:::eval(substitute(expr), data, enclos = parent.frame())
 21. callr:::with_envvar(env, do.call(processx::run, c(list(bin, args = real_cmdargs,  ...
 22. base:::force(code)
 23. base:::do.call(processx::run, c(list(bin, args = real_cmdargs,  ...
 24. (function (command = NULL, args = character(), error_on_status = TRUE,  ...
 25. base:::throw(new_process_error(res, call = sys.call(), echo = echo,  ...
 26. base:::signalCondition(cond)
 27. (function (e)  ...
 28. asNamespace("callr")$err$throw(e)

 x ! System command 'Rcmd.exe' failed 

I use R 4.2.0, rstantools 2.2.0 and Stan 2.26.1.

Thanks!

I encounter exactly the same error.
Guess it is a version issue?

@andrjohns @hsbadr @bgoodri

Just came across this thread. I had a similar issue a while back, with the same “file too big” and “too many sections” error messages, which could seemingly occur at multiple stages in the rstantools build workflow whenever the Stan files are compiled.

Long and painful story short, in my case the problem turned out to be that I was failing to invoke my Makevars.win, to which I’d added a number of -Wno-blahblah flags to suppress the extremely verbose and unhelpful warning messages that (recent versions of?) the compiler toolchain spits out. I don’t expect @anthony_462 or @yyang97 have my particular issue of an overlooked environment variable pointing to a location that is not the customized Makevars.win, but if you’re new to Stan-dependent packages then you likely haven’t added any flags beyond what’s prescribed in the rstantools tutorial. At least in the case of my packages, those minimal flags are not sufficient, and compilation will fail with the same sort of error you’re getting.

Most of that epic thread is me chasing my own tail and is probably not directly applicable, but you might be interested in the GitHub Issues linked in this post, which discuss a closely related problem (in the context of TMB rather than Stan) and some important caveats when adding no-warning flags to Makevars.

If this does turn out to be the issue and my experience could help other users, I’d be thrilled. One way to test it would be to compile lm.stan directly with rstan::stan_model() or rstan::stan(). If the compiler spits out lots of warnings, those are the ones you’re trying to suppress.