Tommy Jones
Tommy Jones

Reputation: 390

Calling Rust cargo from R results in "cargo not found" error on macOS

Running R from RStudio on macOS 11.2.1

When calling cargo from R, as when using the rextendr package or trying to install helloextendr, I receive the errors sh: cargo: command not found and make: cargo: command not found, respectively. However, from the terminal, calling cargo works as expected.

One can see the difference from below.

In R cargo is not found:

> Sys.which("cargo")
# cargo
# ""

From a terminal cargo is found

% which cargo
$HOME/.cargo/bin/cargo

Upvotes: 2

Views: 1114

Answers (1)

Tommy Jones
Tommy Jones

Reputation: 390

The cause is related to this question: Why do which and Sys.which return different paths? Simply put, macOS has a different $PATH variable, depending on whether you use a terminal directly or use an application launched from the dock (as R and RStudio are).

One solution is to use a .Rprofile file. Add the following lines to the .Rprofile in your home directory or project's home directory. (For more on .Rprofile files, see here.)

# R can't find Rust's cargo binary
# issue is because $PATH is different for shell vs. dock launched applications in MacOS
# See: https://stackoverflow.com/questions/48084679/why-do-which-and-sys-which-return-different-paths
Sys.setenv(PATH = paste0("/Users/username/.cargo/bin:", Sys.getenv("PATH")))

Make sure "username" is your username. For example, my path is "/Users/tommy/.cargo/bin:"

This appends the default location of Rust installation to the $PATH variable that R receives on startup.

Upvotes: 2

Related Questions