extendr / rextendr

An R package that helps scaffolding extendr-enabled packages or compiling Rust code dynamically
https://extendr.github.io/rextendr/
Other
175 stars 25 forks source link

basic example compliation error #207

Open mjbroerman opened 1 year ago

mjbroerman commented 1 year ago

Hi! Claus Wilke's nyhackr talk inspired me to try this. Thanks for all your hard work!

I got rust to build with libR-sys in the Cargo.toml, though I used curl rather than rustup for installation as indicated, with:

cargo build
cargo test

Then I installed rextendr. I tried your basic example:

library(rextendr)

# create a Rust function
rust_function("fn add(a:f64, b:f64) -> f64 { a + b }")

# call it from R
add(2.5, 4.7)

and got this:

> rust_function("fn add(a:f64, b:f64) -> f64 { a + b }")
ℹ build directory: /private/var/folders/bp/l5qt50g13sndlz0x48jqqv300000gn/T/RtmpCsHcVC/file4add4132734f
    Updating crates.io index
 Downloading crates ...
  Downloaded lazy_static v1.4.0
  Downloaded proc-macro2 v1.0.43
  Downloaded quote v1.0.21
  Downloaded unicode-ident v1.0.3
  Downloaded paste v1.0.9
  Downloaded syn v1.0.99
  Downloaded extendr-engine v0.3.1
  Downloaded extendr-macros v0.3.1
  Downloaded extendr-api v0.3.1
   Compiling libR-sys v0.3.0
   Compiling proc-macro2 v1.0.43
   Compiling unicode-ident v1.0.3
   Compiling quote v1.0.21
   Compiling syn v1.0.99
   Compiling extendr-engine v0.3.1
   Compiling extendr-api v0.3.1
   Compiling paste v1.0.9
   Compiling lazy_static v1.4.0
   Compiling extendr-macros v0.3.1
   Compiling rextendr1 v0.0.1 (/private/var/folders/bp/l5qt50g13sndlz0x48jqqv300000gn/T/RtmpCsHcVC/file4add4132734f)
error: could not compile `rextendr1` due to 2 previous errors
Error in `invoke_cargo()`:
! Rust code could not be compiled successfully. Aborting.
ℹ 2 compiler messages not shown.
Run `rlang::last_error()` to see where the error occurred.
> rlang::last_error()
<error/rextendr_error>
Error in `invoke_cargo()`:
! Rust code could not be compiled successfully. Aborting.
ℹ 2 compiler messages not shown.
---
Backtrace:
 1. rextendr::rust_function("fn add(a:f64, b:f64) -> f64 { a + b }")
 2. rextendr::rust_source(code = code, env = env, ...)
 3. rextendr:::invoke_cargo(toolchain = toolchain, specific_target = specific_target, dir = dir, profile = profile, quiet = quiet,
  use_rtools = use_rtools)
Run `rlang::last_trace()` to see the full context.
> rlang::last_trace()
<error/rextendr_error>
Error in `invoke_cargo()`:
! Rust code could not be compiled successfully. Aborting.
ℹ 2 compiler messages not shown.
---
Backtrace:
    ▆
 1. └─rextendr::rust_function("fn add(a:f64, b:f64) -> f64 { a + b }")
 2.   └─rextendr::rust_source(code = code, env = env, ...)
 3.     └─rextendr:::invoke_cargo(toolchain = toolchain, specific_target = specific_target, dir = dir, profile = profile, quiet = quiet,
  use_rtools = use_rtools)
 4.       └─rextendr:::check_cargo_output(...)
 5.         └─rextendr:::ui_throw(...)
 6.           ├─withr::with_options(...)
 7.           │ └─base::force(code)
 8.           └─rlang::abort(message, class = "rextendr_error", call = call)

I have both x86_64 and arm64 versions of R installed. But my R.Version() is x86_64, the one I want. I did not do the step here because the lookup looked correct.

Here's a reprex, but I thought the above was more informative.

library(rextendr)

# create a Rust function
rust_function("fn add(a:f64, b:f64) -> f64 { a + b }")
#> ℹ build directory: '/private/var/folders/bp/l5qt50g13sndlz0x48jqqv300000gn/T/
#> RtmpfsEAYJ/file5e2b2fdef9a1'
#> Error in `invoke_cargo()`:
#> ! Rust code could not be compiled successfully. Aborting.
#> ℹ 2 compiler messages not shown.

#> Backtrace:
#>     ▆
#>  1. └─rextendr::rust_function("fn add(a:f64, b:f64) -> f64 { a + b }")
#>  2.   └─rextendr::rust_source(code = code, env = env, ...)
#>  3.     └─rextendr:::invoke_cargo(toolchain = toolchain, specific_target = specific_target, dir = dir, profile = profile,
#>   quiet = quiet, use_rtools = use_rtools)
#>  4.       └─rextendr:::check_cargo_output(...)
#>  5.         └─rextendr:::ui_throw(...)
#>  6.           ├─withr::with_options(...)
#>  7.           │ └─base::force(code)
#>  8.           └─rlang::abort(message, class = "rextendr_error", call = call)

# call it from R
add(2.5, 4.7)
#> Error in add(2.5, 4.7): could not find function "add"

Created on 2022-09-07 with reprex v2.0.2

Session info ``` r sessionInfo() #> R version 4.2.1 (2022-06-23) #> Platform: x86_64-apple-darwin17.0 (64-bit) #> Running under: macOS Big Sur ... 10.16 #> #> Matrix products: default #> BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib #> LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib #> #> locale: #> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 #> #> attached base packages: #> [1] stats graphics grDevices utils datasets methods base #> #> other attached packages: #> [1] rextendr_0.2.0.9000 #> #> loaded via a namespace (and not attached): #> [1] pillar_1.8.1 compiler_4.2.1 highr_0.9 R.methodsS3_1.8.2 #> [5] R.utils_2.12.0 tools_4.2.1 digest_0.6.29 jsonlite_1.8.0 #> [9] evaluate_0.16 lifecycle_1.0.1 tibble_3.1.8 R.cache_0.16.0 #> [13] pkgconfig_2.0.3 rlang_1.0.5 reprex_2.0.2 cli_3.3.0 #> [17] rstudioapi_0.14 yaml_2.3.5 xfun_0.32 fastmap_1.1.0 #> [21] withr_2.5.0 styler_1.7.0 stringr_1.4.1 dplyr_1.0.10 #> [25] knitr_1.40 generics_0.1.3 fs_1.5.2 vctrs_0.4.1 #> [29] tidyselect_1.1.2 glue_1.6.2 R6_2.5.1 processx_3.7.0 #> [33] fansi_1.0.3 rmarkdown_2.16 purrr_0.3.4 magrittr_2.0.3 #> [37] ps_1.7.1 htmltools_0.5.3 utf8_1.2.2 stringi_1.7.8 #> [41] brio_1.1.3 R.oo_1.25.0 ```
beniaminogreen commented 1 year ago

Can't see the compiler errors in this example, but I also encountered an "invoke_cargo" error when installing the package on a new computer this afternoon. Not an ideal solution, but I managed to get working by switching to the nightly rust build with:

rust_function("fn add(a:f64, b:f64) -> f64 { a + b }", toolchain="nightly")

If you don't have the nightly build, you can install it with rustup toolchain install nightly

Ilia-Kosenkov commented 1 year ago

Hm, that is strange. So you are using Macs, Macs are sometimes fragile. I remember there was an issue with cargo -- somehow it was not available from within the session. There also might be some issue with transferring environment variables to another process, as we compile Rust code out-of-process using {processx}.

Let's first diagnose the simplest problem, see for example this thread https://github.com/extendr/libR-sys/issues/54. Check if your system("cargo --verion") works from within R.

beniaminogreen commented 1 year ago

Can't see the compiler errors in this example, but I also encountered an "invoke_cargo" error when installing the package on a new computer this afternoon. Not an ideal solution, but I managed to get working by switching to the nightly rust build with:

rust_function("fn add(a:f64, b:f64) -> f64 { a + b }", toolchain="nightly")

If you don't have the nightly build, you can install it with rustup toolchain install nightly

Just realized my other version of Rust was hopelessly outdated - switching to the nightly (or a newer version) probably won't do much for anyone else.

mjbroerman commented 1 year ago

rust_function("fn add(a:f64, b:f64) -> f64 { a + b }", toolchain="nightly") produces the same error for me.

system("cargo --version") does not work!

sh: cargo: command not found
Warning message:
In system("cargo --version") : error in running command

But it does and returns cargo 1.63.0 (fd9c4297c 2022-07-01) in the RStudio terminal and everywhere else.

I'm not sure how this works with R versions and Rosetta for the arm M1 Mac. Whereas I have :/Users/matt/.cargo/bin in my $PATH everywhere I have the command, with system("echo $PATH") I have

/Users/matt/opt/miniconda3/bin:/Users/matt/opt/miniconda3/condabin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/opt/X11/bin:/Users/matt/Applications/quarto/bin:/Library/TeX/texbin:/usr/texbin:/Applications/RStudio.app/Contents/MacOS/quarto/bin:/Applications/RStudio.app/Contents/MacOS

So that at least seems to be a problem with different shell environments.

I tried this to get you compilation errors (attached) by navigating what I think is the build directory:

cd /private/var/folders/bp/l5qt50g13sndlz0x48jqqv300000gn/T/RtmpCsHcVC/file4add4132734f
cargo build

output.txt

yutannihilation commented 1 year ago

On Windows, I think we add it to PATH here. Probably we can do the same on macOS?

https://github.com/extendr/rextendr/blob/05b4d4574b7f4e26fe9bc8c88df994a1001b17dc/R/helpers.r#L28

yutannihilation commented 1 year ago

Ah, sorry, it's on non-Windows. Why doesn't this work?

Ilia-Kosenkov commented 1 year ago

I linked an issue from libR-sys. Something weird happens to R sessions on Mac, cargo is not always available on path. One needs to manually add it to path inside R session and then try compiling code. The basic system("cargo --version") verifies that cargo is present.

yutannihilation commented 1 year ago

Didn't we fix that in https://github.com/extendr/rextendr/pull/166?

JosiahParry commented 11 months ago

Is this fixed?