Skip to content

Commit 963c921

Browse files
Contrib docs: note how to run a binary directly
Because it took me a moment to figure this out when I was wanting to run `rustfmt` through a debugger. Setting `LD_LIBRARY_PATH` uses some Bash parameter expansion, specifically the form `${parameter:+word}`[1], to avoid potentially adding an empty element to `LD_LIBRARY_PATH` that could be a security issue (see[2]) Some history: there used to be a note in `README.md` about setting this variable, but that was removed with 2e75f23. However, with the addition of `rustc_driver` with d7fa2ee (original upstream[3]) this is required again. Link: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html [1] Link: #2923 [2] Link: rust-lang/rust@8c000a6 [3]
1 parent 1e6ce1d commit 963c921

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Contributing.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,30 @@ If you want to test modified `cargo-fmt`, or run `rustfmt` on the whole project
116116
RUSTFMT="./target/debug/rustfmt" cargo run --bin cargo-fmt -- --manifest-path path/to/project/you/want2test/Cargo.toml
117117
```
118118

119+
#### Running a binary directly
120+
121+
You may want to run one of the built binaries directly, for example to connect
122+
it to a debugger. Since `rustfmt` uses `rustc_driver` it needs to be linked
123+
against the version of that library for the current toolchain, without
124+
configuring anything you are likely to run into errors like:
125+
126+
$ ./target/debug/rustfmt
127+
./target/debug/rustfmt: error while loading shared libraries: librustc_driver-63b8deb6c23747dd.so: cannot open shared object file: No such file or directory
128+
129+
This library will be in the sysroot of the current toolchain, so we can fix this
130+
by telling the linker to look there. For example on Linux with Bash:
131+
132+
LD_LIBRARY_PATH="$(rustc --print sysroot)/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}" ./target/debug/rustfmt
133+
134+
Equivalently, you could set the variable for the current environment:
135+
136+
$ export LD_LIBRARY_PATH="$(rustc --print sysroot)/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
137+
$ ./target/debug/rustfm
138+
139+
Then, you can invoke a debugger like `rust-gdb`:
140+
141+
LD_LIBRARY_PATH="$(rustc --print sysroot)/lib${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}" rust-gdb --args ./target/debug/rustfmt --check some_file.rs
142+
119143
### Gate formatting changes
120144

121145
A change that introduces a different code-formatting must be gated on the

0 commit comments

Comments
 (0)