codelldb icon indicating copy to clipboard operation
codelldb copied to clipboard

Set environment of `cargo test --no-run`

Open koutheir opened this issue 3 years ago • 4 comments

In the context of Rust development, debugging unit tests happens in two steps:

  1. Building unit tests via cargo, usually via cargo test --no-run.
  2. Running built unit tests in the lldb debugger.

This plugin supports specifying environment variables for step (2), so that unit tests run in a specific environment. However, there is currently no support for specifying environment variables for step (1).

Specifying environment variables for step (1) is useful when this step involves building some static libraries via build.rs, so that the correct environment variables are provided to these build scripts. The absence of this feature makes it impossible to debug unit tests because they are built incorrectly due to missing environment variables during cargo test --no-run.

Consider the following launch.json configuration:

{
    "type": "lldb",
    "request": "launch",
    "name": "debug unit tests",
    "cargo": {
        "filter": {
            "name": "the-binary",
            "kind": "bin"
        },
        "args": [
            "test",
            "--no-run",
        ],
    },
    "program": "${cargo:program}",
    "env": {
        "RUST_BACKTRACE": "full",
        "SOME_STATIC_LIB_DIR": ".../path/to/specially/compiled/static/lib",
    },
    "args": [
        "--test-threads",
        "1",
        "--nocapture",
        "test_to_debug",
    ]
}

The environment variable SOME_STATIC_LIB_DIR is specified when the test is run in the debugger, but it is not specified in step (1) when cargo test --no-run is executed.

One approach, to solve this, could add an "env" key to the "cargo" contents, such as:

{
    "type": "lldb",
    "request": "launch",
    "name": "debug unit tests",
    "cargo": {
        "filter": {
            "name": "the-binary",
            "kind": "bin"
        },
        "env": {
            "SOME_STATIC_LIB_DIR": ".../path/to/specially/compiled/static/lib",
        },
        "args": [
            "test",
            "--no-run",
        ],
    },
    "program": "${cargo:program}",
    "env": {
        "RUST_BACKTRACE": "full",
        "SOME_STATIC_LIB_DIR": ".../path/to/specially/compiled/static/lib",
    },
    "args": [
        "--test-threads",
        "1",
        "--nocapture",
        "test_to_debug",
    ]
},

Another approach that seem acceptable would be to use the same environment variables specified in "env" also in step (1).

PS.: Thank you for this pretty good plugin!

koutheir avatar Nov 11 '20 12:11 koutheir

Thanks, I'll consider your suggestion. It's unfortunate that every Rust tool ends up implementing this separately :unamused: I wish there was a way to do this via .config/cargo.toml...

Meanwhile, you can try to work around it by adding this environment variable to "lldb.adapterEnv" in workspace settings (not in launch config). A little undocumented feature is that "adapterEnv" is used not just for the debug adapter, but also for cargo invocations.

vadimcn avatar Nov 11 '20 22:11 vadimcn

I also need this, except for the normal build, not the tests.

LIBRARY_PATH=/usr/local/lib cargo run

If run without the env variable it complains about not managing to find my custom compiled C dependency. But putting it in the "env" section of config doesn't seem to send it to the build step.

zumoshi avatar Jan 09 '21 02:01 zumoshi

I came with the same need, but from a slightly different requirement. We have defaulted to dev builds having debug = false in Cargo, as that speeds up compilation time by 2.5x in the kind of common incremental changes we often make. But that doesn't generate debug info, so one way to fix the issue is to set CARGO_PROFILE_DEV_DEBUG to true when compiling just for debugging, e.g. in launch.json. Here, having the feature added to the underlying Cargo spec doesn't help, because we want a specific override only for the debugger.

ndmitchell avatar Feb 02 '21 09:02 ndmitchell

I also need this. If cannot add "env" in "cargo", could you give another solution? thanks.

traitmeta avatar Nov 09 '21 14:11 traitmeta