### Bug description

I'm working on some course materials where I would like to embed stan models in a quarto document but currently models that I can successfully compile interactively will fail when rendering the document. The issue appears to be that since quarto is an x86 executable it is causing confusion with stan / clang such that they attempt to build the model as x86 instead of arm64 causing an issue with the model's shared library object being loaded. The same issues do not occur when compiling the same document using RMarkdown or on a linux x86 machine.

A basic reprex is possible with the following document when rendered on an m1 machine,

````
---
title: "test"
format: html
---

```{r}
model = "
data {
  int<lower=0> N;
  vector[N] x;
  vector[N] y;
}
parameters {
  real a;
  real b;
  real<lower=0> sigma;
}
model {
  y ~ normal(a + b * x, sigma);
}
"

N = 10
x = runif(N)
y = x + rnorm(N)

rstan::stan(model_code = model, data = list(N=N, x=x, y=y), chains=1)
```
````

which produces the following output when rendered on my M1 mac

````
processing file: test.qmd
  |...................................                                   |  50%
  ordinary text without R code

  |......................................................................| 100%
label: unnamed-chunk-1
Quitting from lines 9-30 (test.qmd) 
Error in dyn.load(libLFile) : 
  unable to load shared object '/var/folders/ds/8sqz2v4d355btthn6r88kdc00000gn/T//RtmpU2OC1K/file153642d93e83d.so':
  dlopen(/var/folders/ds/8sqz2v4d355btthn6r88kdc00000gn/T//RtmpU2OC1K/file153642d93e83d.so, 0x0006): tried: '/var/folders/ds/8sqz2v4d355btthn6r88kdc00000gn/T//RtmpU2OC1K/file153642d93e83d.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/var/folders/ds/8sqz2v4d355btthn6r88kdc00000gn/T//RtmpU2OC1K/file153642d93e83d.so' (no such file), '/var/folders/ds/8sqz2v4d355btthn6r88kdc00000gn/T//RtmpU2OC1K/file153642d93e83d.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/private/var/folders/ds/8sqz2v4d355btthn6r88kdc00000gn/T/RtmpU2OC1K/file153642d93e83d.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/private/var/folders/ds/8sqz2v4d355btthn6r88kdc00000gn/T/RtmpU2OC1K/file153642d93e83d.so' (
Calls: .main ... cxxfunctionplus -> <Anonymous> -> cxxfunction -> dyn.load
Quitting from lines 11-32 (test.qmd) 
Error in sink(type = "output") : invalid connection
Calls: .main ... <Anonymous> -> stan_model -> cxxfunctionplus -> sink
Execution halted

 *** caught segfault ***
address 0x61706d6f636e6920, cause 'invalid permissions'

Traceback:
 1: getLoadedDLLs()
 2: f %in% names(getLoadedDLLs())
 3: (function (env) {    if (f %in% names(getLoadedDLLs()))         dyn.unload(libLFile)    unlink(libLFile)})(<environment>)
An irrecoverable exception occurred. R is aborting now ...

````

My hope is that this is fixable via overriding the appropriate compiler flags when invoking the R session / knitr.

This is occurring with quarto v1.1.167 and v1.2.15.

<details>
<summary>Session Info</summary>

````
> sessioninfo::session_info()
─ Session info ──────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.1 (2022-06-23)
 os       macOS Ventura 13.0
 system   aarch64, darwin21.5.0
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2022-09-01
 rstudio  2022.07.1+554 Spotted Wakerobin (desktop)
 pandoc   2.19.2 @ /opt/homebrew/bin/ (via rmarkdown)

─ Packages ──────────────────────────────────────────────────────────────────────
 package      * version  date (UTC) lib source
 assertthat     0.2.1    2019-03-21 [1] CRAN (R 4.2.1)
 callr          3.7.2    2022-08-22 [1] CRAN (R 4.2.1)
 cli            3.3.0    2022-04-25 [1] CRAN (R 4.2.1)
 codetools      0.2-18   2020-11-04 [1] CRAN (R 4.2.1)
 colorspace     2.0-3    2022-02-21 [1] CRAN (R 4.2.1)
 crayon         1.5.1    2022-03-26 [1] CRAN (R 4.2.1)
 DBI            1.1.3    2022-06-18 [1] CRAN (R 4.2.1)
 digest         0.6.29   2021-12-01 [1] CRAN (R 4.2.1)
 dplyr          1.0.9    2022-04-28 [1] CRAN (R 4.2.1)
 evaluate       0.16     2022-08-09 [1] CRAN (R 4.2.1)
 fansi          1.0.3    2022-03-24 [1] CRAN (R 4.2.1)
 fastmap        1.1.0    2021-01-25 [1] CRAN (R 4.2.1)
 generics       0.1.3    2022-07-05 [1] CRAN (R 4.2.1)
 ggplot2      * 3.3.6    2022-05-03 [1] CRAN (R 4.2.1)
 glue           1.6.2    2022-02-24 [1] CRAN (R 4.2.1)
 gridExtra      2.3      2017-09-09 [1] CRAN (R 4.2.1)
 gtable         0.3.0    2019-03-25 [1] CRAN (R 4.2.1)
 htmltools      0.5.3    2022-07-18 [1] CRAN (R 4.2.1)
 inline         0.3.19   2021-05-31 [1] CRAN (R 4.2.1)
 knitr          1.40     2022-08-24 [1] CRAN (R 4.2.1)
 lifecycle      1.0.1    2021-09-24 [1] CRAN (R 4.2.1)
 loo            2.5.1    2022-03-24 [1] CRAN (R 4.2.1)
 magrittr       2.0.3    2022-03-30 [1] CRAN (R 4.2.1)
 matrixStats    0.62.0   2022-04-19 [1] CRAN (R 4.2.1)
 munsell        0.5.0    2018-06-12 [1] CRAN (R 4.2.1)
 pillar         1.8.1    2022-08-19 [1] CRAN (R 4.2.1)
 pkgbuild       1.3.1    2021-12-20 [1] CRAN (R 4.2.1)
 pkgconfig      2.0.3    2019-09-22 [1] CRAN (R 4.2.1)
 prettyunits    1.1.1    2020-01-24 [1] CRAN (R 4.2.1)
 processx       3.7.0    2022-07-07 [1] CRAN (R 4.2.1)
 ps             1.7.1    2022-06-18 [1] CRAN (R 4.2.1)
 purrr          0.3.4    2020-04-17 [1] CRAN (R 4.2.1)
 R6             2.5.1    2021-08-19 [1] CRAN (R 4.2.1)
 Rcpp           1.0.9    2022-07-08 [1] CRAN (R 4.2.1)
 RcppParallel   5.1.5    2022-01-05 [1] CRAN (R 4.2.1)
 rlang          1.0.4    2022-07-12 [1] CRAN (R 4.2.1)
 rmarkdown      2.16     2022-08-24 [1] CRAN (R 4.2.1)
 rstan        * 2.21.5   2022-04-11 [1] CRAN (R 4.2.1)
 rstudioapi     0.14     2022-08-22 [1] CRAN (R 4.2.1)
 scales         1.2.1    2022-08-20 [1] CRAN (R 4.2.1)
 sessioninfo    1.2.2    2021-12-06 [1] CRAN (R 4.2.1)
 StanHeaders  * 2.21.0-7 2020-12-17 [1] CRAN (R 4.2.1)
 tibble         3.1.8    2022-07-22 [1] CRAN (R 4.2.1)
 tidyselect     1.1.2    2022-02-21 [1] CRAN (R 4.2.1)
 utf8           1.2.2    2021-07-24 [1] CRAN (R 4.2.1)
 vctrs          0.4.1    2022-04-13 [1] CRAN (R 4.2.1)
 withr          2.5.0    2022-03-03 [1] CRAN (R 4.2.1)
 xfun           0.32     2022-08-10 [1] CRAN (R 4.2.1)

 [1] /Users/rundel/Library/R/arm64/4.2/library
 [2] /opt/homebrew/Cellar/r/4.2.1_2/lib/R/library

─────────────────────────────────────────────────────────────────────────────────
````

</details>

### Checklist

- [X] Please include a minimal, fully reproducible example in a single .qmd file? Please provide the whole file rather than the snippet you believe is causing the issue.
- [X] Please [format your issue](https://yihui.org/issue/#please-format-your-issue-correctly) so it is easier for us to read the bug report.
- [X] Please document the RStudio IDE version you're running (if applicable), by providing the value displayed in the "About RStudio" main menu dialog?
- [X] Please document the operating system you're running. If on Linux, please provide the specific distribution.
cscheid
2022-09-01T16:48:28Z
rundel
bug needs-repro
v1.2
closed
quarto + stan + m1 compilation issue
https://github.com/quarto-dev/quarto-cli/issues/2258