Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using a local rust crate with extendr #398

Open
sanghoonio opened this issue Nov 19, 2024 · 5 comments
Open

Using a local rust crate with extendr #398

sanghoonio opened this issue Nov 19, 2024 · 5 comments

Comments

@sanghoonio
Copy link

I am working on a Rust tool with this directory structure:

image

I want to add R bindings in bindings/r using rextendr that can call functions from my main Rust crate.

The specific issue I'm running into is with Cargo being unable to find the main crate during package installation. While an absolute path in bindings/r/src/rust/Cargo.toml works:

[dependencies]
gtars = { path = "/Users/sam/Documents/Work/gtars/gtars" }

This obviously isn't suitable for distribution. Using a relative path fails during R package installation:

[dependencies]
gtars = { path = "../../../../gtars" }

Error during installation:

error: failed to get `gtars` as a dependency of package `gtars-r v0.1.0`
Caused by:
  failed to load source for dependency `gtars`

How can I properly set up R bindings that:

  1. Can access my main Rust crate
  2. Works with relative paths
  3. Can be distributed to other users

I've tried:

  • Relative paths in Cargo.toml
  • Using --no-staged-install

Any guidance on the correct way to address this would be appreciated.

@JosiahParry
Copy link
Contributor

Thanks for the issue! Do you have a repo available that we can look at?

thanks!

@nleroy917
Copy link

Hey @JosiahParry! I'm not @sanghoonio but I work on the project too. Here is a link to the repo: https://github.com/databio/gtars

The branch is r_bindings

@JosiahParry
Copy link
Contributor

Thanks @nleroy917, I'm playing with this now. I've got something working but it isn't ideal.

Out of curiosity where would you like to publish your R package? BioConductor?

For context, the challenge is that gtars is 2 levels up from the R package source meaning that the R package source is not portable.

The entirety of an R package's source should be contained within it. When using this approach that is not the case. What we need to do is vendor the package that is being used which isn't ideal because then you lose the nice developer experience of a change in one place is reflected elsewhere.

I can get the package to install by using

fs::dir_copy("../../gtars", "src/rust")
devtools::install()

and modifying the Cargo.toml to be

[dependencies]
extendr-api = "*"
gtars = { path = "gtars" }

again, this is undesirable.

@CGMossa do you have thoughts on how we can improve this? I personally would like to figure this out for rnetmatch rust crate. Robin Lovelace is interested in this pattern too.

I'm trying to think through how we can include the relative directory in the vendor.tar.gz but cargo vendor doesn't support using crates like this.

I also wonder if a workspace would be able to help here

@nleroy917
Copy link

@JosiahParry thank you for this thorough response! This is super awesome. I am wondering if it would be easier to just create a new repo for this? It just nice to have it all in one place

@JosiahParry
Copy link
Contributor

@nleroy917 technically, yes it may be easier, but it is still something I'd like to have work nicely. One alternative is to use

gtars = { git = "https://...." }

in the R package this would be a close alternative but would require pushing commits to have them reflected in your R bindings package.

@JosiahParry JosiahParry changed the title Creating R bindings for an existing Rust tool using rextendr Using a local rust crate with extendr Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants