Get cargo environment without running code #14045
Labels
C-feature-request
Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
S-triage
Status: This issue is waiting on initial triage.
Problem
Currently it’s only possible to see the environment by running an executable (#4879). It would be excellent if this could be extended to print/save the environment without launching anything.
My use case is to improve the debug workflow in IDEs. For example, this test ran fine without debug but panicked in debug because outside the context of
cargo test
the environment is not available and it can’t find a shared object file (see note 2 below for details).This could be resolved if we saved the environment as part of
build
,test --no-run
, or another subcommand so the environment could be handed over to the debugger, or be used as a template for a developer to work with.Proposed Solution
One solution would be adding a new a flag to save environment to a text file when built, e.g.
cargo build --save-env=<filename>
cargo test --no-run --save-env=<filename>
--save-env
would be useful from some non-build commands too?)Ideally the save env output would quote each value. For example, some vars from my current conifig:
(If those were unquoted, then sourcing would have issues parsing the
%s
. There's a note on this here easier: https://stackoverflow.com/questions/15235729/sourcing-env-output.)The saved environment could then be parsed or picked up directly by external tooling. For example, all you would need to do in VSCode is add this to your settings.json:
"lldb.launch.envFile": "<filename>"
.I'm not sure if this solution is idiomatic so I’m very open to suggestions. A longer-term solution would be commands for interrogating environment/settings in detail, but that's big feature, probably a separate ticket entirely.
Notes
1. Previous related work
In #4879, @joshtriplett requested output for
LD_LIBRARY_PATH
duringcargo run
so developers can run their cargo-built executables outside cargo. This was resolved in PR #12498 and nowcargo run -vv
andcargo test -vv
print the environment. The problem is this is always part of a run, e.g.cargo test -vv
it's in the outputRunning 'CARGO=foo CARGO_CRATE_NAME=etc...'
. To prep for debug in this case, nothing should be launched, just the environment is needed.2. How binary is picked up in example above
In my main example, the project uses a crate that has a custom
build.rs
with a C shared lib; the build file has acargo:rustc-link-search
pointing to theout/lib
folder where the.so
lives. When running a regular test in the IDE, it works fine because under the hood it’s just running a regularcargo test
which sets upLD_LIBRARY_PATH
(and any other CARGO environment vars) from any search paths.However, when running in debug, the IDE can’t just run
cargo test
, it first has tocargo build
(orcargo test --no-run
) to generate an executable and then launch the debugger with it. The problem is there is no neat way extract the environment to hand over to the debugger; the fix to #4879 allows you to see the environment but it’s mixed in with other build/run output, and of course the code is launched which you want to avoid if going into a debug session immediately afterwards.3. Comparison with and without debug
If using VSCode, copy this code and you'll see a "Run Tests | Debug" hover annotation above a test. If you click on "Run Tests" you'll see different output to "Debug"
Example "Run Test" snippet:
Example "Debug" snippet:
i.e. none of the
CARGO_
orLD_LIBRARY_PATH
envs are set in debug.4. Workarounds
cargo test -vv
.LD_LIBRARY_PATH
so VSCode/lldb can find the shared lib, debug settings (launch.json if VSCode).Alternatively, temporarily add a
dump_env
function to your project to save the environment, and add that to yourlldb.launch.envFile
or similar setting.The text was updated successfully, but these errors were encountered: