r-universe-org / help

Support and bug tracker for R-universe
https://docs.r-universe.dev/
9 stars 2 forks source link

Try to get rust package to work #325

Open jeroen opened 10 months ago

jeroen commented 10 months ago

There is an issue right now with xz being masked on the path by a js lib: https://github.com/r-universe/r-rust/actions/runs/6832942573

JosiahParry commented 9 months ago

Hey! I came to post this question. I suspect there is something that needs to be modified in libR-sys which provides bindings to R's C API. My understanding is that at build time, the OS is determined and the bindings are matched to the ones provided here.

To get webR to build for extendr / rust packages, I suspect that bindings need to be generated for WASM. I'd be happy to lend a hand to get this working / off the ground. CC @cgmossa.

Do you have an idea where to start looking and poking at?

jeroen commented 9 months ago

https://fosstodon.org/@gws@mstdn.social/111494461763849811

JosiahParry commented 9 months ago

Let's gooooo!!!!! 🚀

yutannihilation commented 9 months ago

The first error you'll encounter after the xz problem is probably this, as r-wasm's Docker image doesn't install Rust toolchain (yet?):

/bin/bash: line 1: cargo: command not found

(cf. https://github.com/yutannihilation/savvy-webr-test/actions/runs/7276057828/job/19825071976#step:5:251)

It seems they are preparing the mechanism for customizing the build for webR, but It found no clue other than this:

https://github.com/georgestagg/hellorust-wasm/commit/7383d37ee1c28fc3a86cd941aafc9ac563978c20

jeroen commented 9 months ago

This is still very experimental because threading and exceptions don't work properly in wasm.

yutannihilation commented 9 months ago

Thanks for the reminder. For exceptions, I think it's rare that Rust catches C++'s exception because R's API is C, so I think it won't be a big problem, but I might be wrong. For threading, I agree it's a problem that we have to wait for wasm to have the spec.

Note that, I already succeeded to compile a toy package with Rust on my local (because, as I described above, their GitHub Action is not ready for Rust) and confirms it can pass values between R and Rust, and handle Rust errors.

> webr::install("savvyExamples", repos = "https://yutannihilation.github.io/savvy-webr-test/repo")
Downloading webR package: savvyExamples
> savvyExamples::to_upper(1)
Error: Unexpected type: Cannot convert double to string

Unfortunately, it freezes when it tries to return a new R object. It's far from a success, but I believe making compilation success is possible at the moment.

> # this freezes
> savvyExamples::to_upper("a")

Some advice for extendr:

yutannihilation commented 9 months ago

Here's what I figured out to compile a Rust-powered R package. Hope this helps.

https://github.com/yutannihilation/webr-rust-docker-image#how-to-build-and-test-a-rust-powered-r-package

yutannihilation commented 7 months ago

@jeroen Sorry to bother you. Could you try updating the r-universe-org/build-wasm image? The r-wasm/webr image now has the Rust tool chain installed (https://github.com/r-wasm/webr/pull/358), so some of the Rust packages should work.

jeroen commented 7 months ago

Sure I triggered a build, it should be live in 5 min: https://github.com/r-universe-org/build-wasm/actions/runs/7972291810/job/21763748288

yutannihilation commented 7 months ago

Thank you so much! I'll check if my package can compile later.

yutannihilation commented 7 months ago

Confirmed now R-universe can build a Rust-powered R package and it works on webR!! 🎉

https://yutannihilation.r-universe.dev/savvyExamples

image

jeroen commented 7 months ago

Thanks!

yutannihilation commented 7 months ago

Thank you!

Note that, I didn't address the xz problem in question for simplicity because it's avoidable by not using the vendored tar file conditionally. I hope we can improve the setup gradually. Anyway, I think this is a big step.

georgestagg commented 7 months ago

The xz problem should (hopefully) go away once https://github.com/r-wasm/webr/issues/327 has been addressed.