Error in sink(type = "output") : invalid connection

The model is

fit <-
brm(data = d,family = gaussian,
y ~0+ Intercept + treatment,
prior = c(prior(normal(0,2), class = b),
prior(student_t(3,1,1), class = sigma)),
seed =1)

I have an error although I installed rstan and stanheaders

Compiling Stan program...
make cmd is
  make -f "C:/PROGRA~1/R/R-41~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-41~1.2/share/make/winshlib.mk" CXX='$(CXX14) $(CXX14STD)' CXXFLAGS='$(CXX14FLAGS)' CXXPICFLAGS='$(CXX14PICFLAGS)' SHLIB_LDFLAGS='$(SHLIB_CXX14LDFLAGS)' SHLIB_LD='$(SHLIB_CXX14LD)' SHLIB="file1bf099c2961.dll" WIN=64 TCLBIN=64 OBJECTS="file1bf099c2961.o"

make would use
C:/rtools40/ucrt64/mingw_64/bin/g++  -std=gnu++14 -I"C:/PROGRA~1/R/R-41~1.2/include" -DNDEBUG   -I"C:/Program Files/R/R-4.1.2/library/Rcpp/include/"  -I"C:/Program Files/R/R-4.1.2/library/RcppEigen/include/"  -I"C:/Program Files/R/R-4.1.2/library/RcppEigen/include/unsupported"  -I"C:/Program Files/R/R-4.1.2/library/BH/include" -I"C:/Program Files/R/R-4.1.2/library/StanHeaders/include/src/"  -I"C:/Program Files/R/R-4.1.2/library/StanHeaders/include/"  -I"C:/Program Files/R/R-4.1.2/library/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Program Files/R/R-4.1.2/library/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -DBOOST_NO_AUTO_PTR  -include "C:/Program Files/R/R-4.1.2/library/StanHeaders/include/stan/math/prim/fun/Eigen.hpp"  -std=c++1y         -O2 -Wall  -mfpmath=sse -msse2 -mstackrealign  -c file1bf099c2961.cpp -o file1bf099c2961.o
if test "zfile1bf099c2961.o" != "z"; then \
  if test -e "file1bf099c2961-win.def"; then \
    echo C:/rtools40/ucrt64/mingw_64/bin/g++  -shared -s -static-libgcc -o file1bf099c2961.dll file1bf099c2961-win.def file1bf099c2961.o  "C:/Program Files/R/R-4.1.2/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.1.2/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.1.2/library/RcppParallel/lib/x64" -ltbb -LC:/PROGRA~1/R/R-41~1.2/library/RCPPPA~1/lib/x64 -ltbb -ltbbmalloc   -L"C:/PROGRA~1/R/R-41~1.2/bin/x64" -lR ; \
    C:/rtools40/ucrt64/mingw_64/bin/g++  -shared -s -static-libgcc -o file1bf099c2961.dll file1bf099c2961-win.def file1bf099c2961.o  "C:/Program Files/R/R-4.1.2/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.1.2/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.1.2/library/RcppParallel/lib/x64" -ltbb -LC:/PROGRA~1/R/R-41~1.2/library/RCPPPA~1/lib/x64 -ltbb -ltbbmalloc   -L"C:/PROGRA~1/R/R-41~1.2/bin/x64" -lR ; \
  else \
    echo EXPORTS > tmp.def; \
    C:/rtools40/ucrt64/mingw_64/bin/nm file1bf099c2961.o | sed -n 's/^.* [BCDRT] / /p' | sed -e '/[.]refptr[.]/d' -e '/[.]weak[.]/d' | sed 's/[^ ][^ ]*/"&"/g'  >> tmp.def; \
    echo C:/rtools40/ucrt64/mingw_64/bin/g++  -shared -s -static-libgcc -o file1bf099c2961.dll tmp.def file1bf099c2961.o  "C:/Program Files/R/R-4.1.2/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.1.2/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.1.2/library/RcppParallel/lib/x64" -ltbb -LC:/PROGRA~1/R/R-41~1.2/library/RCPPPA~1/lib/x64 -ltbb -ltbbmalloc   -L"C:/PROGRA~1/R/R-41~1.2/bin/x64" -lR ; \
    C:/rtools40/ucrt64/mingw_64/bin/g++  -shared -s -static-libgcc -o file1bf099c2961.dll tmp.def file1bf099c2961.o  "C:/Program Files/R/R-4.1.2/library/rstan/lib/x64/libStanServices.a" -L"C:/Program Files/R/R-4.1.2/library/StanHeaders/libs/x64" -lStanHeaders -L"C:/Program Files/R/R-4.1.2/library/RcppParallel/lib/x64" -ltbb -LC:/PROGRA~1/R/R-41~1.2/library/RCPPPA~1/lib/x64 -ltbb -ltbbmalloc   -L"C:/PROGRA~1/R/R-41~1.2/bin/x64" -lR ; \
    rm -f tmp.def; \
  fi \
fi
 Error in compileCode(f, code, language = language, verbose = verbose) : 
sh: line 1: C:/rtools40/ucrt64/mingw_64/bin/g++: No such file or directorymake: *** [C:/PROGRA~1/R/R-41~1.2/etc/x64/Makeconf:245: file1bf099c2961.o] Error 127
 Error in sink(type = "output") : invalid connection

Can you try the steps in the RStan Getting Started guide here: Configuring C Toolchain for Windows · stan-dev/rstan Wiki · GitHub

I did and now I has different error message

Error in compileCode(f, code, language = language, verbose = verbose) : 
C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file3c8c35cd4678.o:file3c8c35cd4678.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D0Ev[_ZN3tbb8internal26task_scheduler_observer_v3D0Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file3c8c35cd4678.o:file3c8c35cd4678.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x1d): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file3c8c35cd4678.o:file3c8c35cd4678.cpp:(.text$_ZN3tbb10interface623task_scheduler_observerD1Ev[_ZN3tbb10interface623task_scheduler_observerD1Ev]+0x3a): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'C:\rtools42\x86_64-w64-mingw32.static.posix\bin/ld.exe: file3c8c35cd4678.o:file3c8c35cd4678.cpp:(.text$_ZN3tbb10interface
 Error in sink(type = "output") : invalid connection

We’re having some compatibility issues with R4.2 and the CRAN rstan, can I get you to restart your R session and then install the preview of the next rstan version using:

remove.packages(c("rstan","StanHeaders"))
install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))

It works now. Thank you very much.