RStan build failure on MS-Win 10

I tried removing the package and going again. But the end result was

*** arch - i386
"J:/rtools40/mingw32/bin/"g++  -std=gnu++14 -I"J:/Programs/R-4.1.2/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -DSTAN_THREADS -DRCPP_PARALLEL_USE_TBB=1 -I'J:/Programs/R-4.1.2/library/Rcpp/include' -I'J:/Programs/R-4.1.2/library/RcppEigen/include' -I'J:/Programs/R-4.1.2/library/BH/include' -I'J:/Programs/R-4.1.2/library/StanHeaders/include' -I'J:/Programs/R-4.1.2/library/RcppParallel/include'     -DRCPP_PARALLEL_USE_TBB=1   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2" -c sparse_extractors.cpp -o sparse_extractors.o
sh: -c: line 1: unexpected EOF while looking for matching `"'
sh: -c: line 2: syntax error: unexpected end of file
make: *** [J:/Programs/R-4.1.2/etc/i386/Makeconf:245: sparse_extractors.o] Error 2
ERROR: compilation failed for package 'rstan'
* removing 'J:/Programs/R-4.1.2/library/rstan'
* restoring previous 'J:/Programs/R-4.1.2/library/rstan'
  1. The problem looking for matching " does not seem to be with the first printed line; all the " look balanced to me. The use of "J:/rtools40/mingw32/bin/"g++ looks funny, but plenty of earlier compilations had that without error.
  2. I was doing parallel compiles, but the line shown seems to be the only one active in this section.

Fuller History

Using 64 bit vanilla R throughout and following RStan Getting Started · stan-dev/rstan Wiki · GitHub.

Removed rstan and verified no .RData file. Restarted.

Binary build

> install.packages("rstan", repos = "https://cloud.r-project.org/", dependencies = TRUE)
trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.1/rstan_2.21.3.zip'
Content type 'application/zip' length 5241778 bytes (5.0 MB)
downloaded 5.0 MB

package ‘rstan’ successfully unpacked and MD5 sums checked

> example(stan_model, package = "rstan", run.dontrun = TRUE)
Loading required package: StanHeaders
Loading required package: ggplot2
rstan (Version 2.21.3, GitRev: 2e1f913d3ca3)
# output, including program listings, deleted

Compilation ERROR, function(s)/method(s) not created!
Error in compileCode(f, code, language = language, verbose = verbose) : 
  sh: -c: line 1: unexpected EOF while looking for matching `"'sh: -c: line 2: syntax error: unexpected end of filemake: *** [J:/Programs/R-4.1.2/etc/x64/Makeconf:245: file5de038ef1c06.o] Error 2

That is not exactly the error given in the installation instructions, but I followed their next step to do a source build. Restarted R first.

Source Build

> parallel::detectCores()
[1] 20
> # Compile packages using all cores
> Sys.setenv(MAKEFLAGS = paste0("-j",parallel::detectCores()))
> date(); install.packages(c("StanHeaders","rstan"),type="source"); date()
[1] "Mon Jan 10 10:47:58 2022"
--- Please select a CRAN mirror for use in this session ---
trying URL 'https://ftp.osuosl.org/pub/cran/src/contrib/StanHeaders_2.21.0-7.tar.gz'
Content type 'application/x-gzip' length 1402572 bytes (1.3 MB)
downloaded 1.3 MB

trying URL 'https://ftp.osuosl.org/pub/cran/src/contrib/rstan_2.21.3.tar.gz'
Content type 'application/x-gzip' length 1163545 bytes (1.1 MB)
downloaded 1.1 MB

* installing *source* package 'StanHeaders' ...
** package 'StanHeaders' successfully unpacked and MD5 sums checked
** using staged installation
** libs
# builds for i386 and x64
# various warnings about unused or uninitialized variables
J:/rtools40/mingw64/bin/gcc -shared -s -static-libgcc -o StanHeaders.dll tmp.def cvodes/cvodes_nls_stg1.o cvodes/cvodes_nls_sim.o cvodes/cvodes_ls.o cvodes/cvodea_io.o cvodes/cvodes_spils.o cvodes/cvodes.o cvodes/cvodes_bandpre.o cvodes/cvodes_bbdpre.o cvodes/cvodes_nls_stg.o cvodes/cvodea.o cvodes/cvodes_io.o cvodes/cvodes_direct.o cvodes/cvodes_diag.o cvodes/cvodes_nls.o sundials/sundials_sparse.o sundials/sundials_dense.o sundials/sundials_nvector_senswrapper.o sundials/sundials_nvector.o sundials/sundials_pcg.o sundials/sundials_math.o sundials/sundials_sptfqmr.o sundials/sundials_mpi.o sundials/sundials_linearsolver.o sundials/sundials_iterative.o sundials/sundials_spbcgs.o sundials/sundials_band.o sundials/sundials_version.o sundials/sundials_nonlinearsolver.o sundials/sundials_direct.o sundials/sundials_matrix.o sunmatrix/band/sunmatrix_band.o sunmatrix/dense/sunmatrix_dense.o sunlinsol/band/sunlinsol_band.o sunlinsol/dense/sunlinsol_dense.o sunnonlinsol/newton/sunnonlinsol_newton.o sunnonlinsol/fixedpoint/sunnonlinsol_fixedpoint.o idas/idas_nls.o idas/idas_nls_sim.o idas/idas_ls.o idas/idas.o idas/idas_bbdpre.o idas/idas_nls_stg.o idas/idaa_io.o idas/idas_direct.o idas/idas_io.o idas/idaa.o idas/idas_ic.o idas/idas_spils.o nvector/serial/nvector_serial.o init.o -LJ:/Programs/R-4.1.2/bin/x64 -lR
installing via 'install.libs.R' to J:/Programs/R-4.1.2/library/00LOCK-StanHeaders/00new/StanHeaders
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
  converting help for package 'StanHeaders'
    finding HTML links ... done
    CxxFlags                                html  
    stanFunction                            html  
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
*** arch - i386
*** arch - x64
** testing if installed package can be loaded from final location
*** arch - i386
*** arch - x64
** testing if installed package keeps a record of temporary installation path
* DONE (StanHeaders)
Making 'packages.html' ... done
* installing *source* package 'rstan' ...
** package 'rstan' successfully unpacked and MD5 sums checked
** using staged installation
** libs

*** arch - i386
"J:/rtools40/mingw32/bin/"g++  -std=gnu++14 -I"J:/Programs/R-4.1.2/include" -DNDEBUG -I"../inst/include" -I"../inst/include/boost_not_in_BH" -I"." -DBOOST_DISABLE_ASSERTS -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION -DBOOST_NO_AUTO_PTR -DSTAN_THREADS -DRCPP_PARALLEL_USE_TBB=1 -I'J:/Programs/R-4.1.2/library/Rcpp/include' -I'J:/Programs/R-4.1.2/library/RcppEigen/include' -I'J:/Programs/R-4.1.2/library/BH/include' -I'J:/Programs/R-4.1.2/library/StanHeaders/include' -I'J:/Programs/R-4.1.2/library/RcppParallel/include'     -DRCPP_PARALLEL_USE_TBB=1   -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -mtune=native -O3 -mmmx -msse -msse2 -msse3 -mssse3 -msse4.1 -msse4.2" -c sparse_extractors.cpp -o sparse_extractors.o
sh: -c: line 1: unexpected EOF while looking for matching `"'
sh: -c: line 2: syntax error: unexpected end of file
make: *** [J:/Programs/R-4.1.2/etc/i386/Makeconf:245: sparse_extractors.o] Error 2
ERROR: compilation failed for package 'rstan'
* removing 'J:/Programs/R-4.1.2/library/rstan'
* restoring previous 'J:/Programs/R-4.1.2/library/rstan'

It looks as if it built StanHeaders OK but failed one the first attempted compilation for RStan.