Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass RUST_BACKTRACE=1 when running a Rust binary installed in Debian?

When I run a binary using cargo, I have the option to run it as follows -

bash -c "RUST_BACKTRACE=1 cargo run --bin my_binary"

This gives me a stack trace when the binary hits an error. But when I create a Debian package for the same binary, how do I get stack traces on failure?

Is there some way to enable backtrace there too, if the source is implemented in Rust?

Edit:

I create a debian package for my cargo project using

cargo deb // Produces a my_binary.deb

This my_binary.deb can then be installed on a Debian machine as -

 dpkg -i /tmp/my_binary*.deb || true \
 && apt-get -f -y install
like image 919
Rajeev Ranjan Avatar asked Jan 05 '19 18:01

Rajeev Ranjan


2 Answers

I had the same issue (error message) on Linux Mint 19 after installation of alacritty (0.5.0-dev).

In terminal just run:

RUST_BACKTRACE=1RUST_BACKTRACE=1 alacritty 

or RUST_BACKTRACE=full for a verbose backtrace.

RUST_BACKTRACE=1RUST_BACKTRACE=full alacritty 
like image 93
Alex.K. Avatar answered Sep 18 '22 19:09

Alex.K.


Just in case someone is looking for setting environment variable from source code, here is how you do it:

use std::env;
fn main() {
  // this method needs to be inside main() method
  env::set_var("RUST_BACKTRACE", "1");
}

The benefit of this approach -- in contrast to manually setting the env variable from PowerShell-- is that you won't need to turn this variable off after you run this program. That is to say, 'RUST_BACKTRACE=1' is set only for this program, not others.

like image 41
Rajesh Timilsina Avatar answered Sep 18 '22 19:09

Rajesh Timilsina