Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Debug unit test code lens fails with a cargo linking failure #887

Closed
adaszko opened this issue Feb 2, 2021 · 7 comments
Closed

Debug unit test code lens fails with a cargo linking failure #887

adaszko opened this issue Feb 2, 2021 · 7 comments

Comments

@adaszko
Copy link

adaszko commented Feb 2, 2021

Hi! 👋

When I click the "Debug" code lens over a unit test, it fails with a linking error:

error: linking with `cc` failed: exit code: 1
[...]
  = note: /usr/bin/ld: cannot find -lpython3.6m
          collect2: error: ld returned 1 exit status

I actually know what the source of the problem is: The PATH environment variable needs to include the /opt/miniconda/bin directory. The problem is, even when I put in the env section in launch.json, it's seems like it's not being respected because it doesn't fix the problem:

{
    "type": "lldb",
    "request": "launch",
    "name": "Debug benchmark 'bench_main'",
    "env": {
        "PATH": ".:/root/.cargo/bin:/opt/miniconda/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin"
    },
    "cargo": {
        "args": [
            "test",
            "--no-run",
            "--bench=bench_main",
            "--package=ddx"
        ],
        "filter": {
            "name": "bench_main",
            "kind": "bench"
        }
    },
    "args": [],
    "cwd": "${workspaceFolder}"
}

OTOH, if I build the code via this launch configuration:

{
	"type": "cargo",
	"command": "build",
	"env": {
		"PATH": ".:/root/.cargo/bin:/opt/miniconda/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin"
	},
	"problemMatcher": [
		"$rustc"
	],
	"group": "build",
	"label": "rust: cargo build"
}

there isn't any linking problem and the code builds fine. So I thought I could use the preLaunchTask in launch.json:

"preLaunchTask": "rust: cargo build",

but it also is being ignored and doesn't address the linking problem.

It looks like this is caused by #373 and #5.

Is this env and preLaunchTask config items handling simply not implemented yet?

@lnicola
Copy link
Member

lnicola commented Feb 2, 2021

Can you try adding it to rust-analyzer.server.extraEnv?

@adaszko
Copy link
Author

adaszko commented Feb 2, 2021

It didn't help unfortunately. I've tried adding it to the project's workspace.code-workspace file and also to my user's settings.json.

I should add that it's a project running in a container on a remote machine, but everything else works fine.

@lnicola
Copy link
Member

lnicola commented Feb 2, 2021

If I understand this correctly, you need that variable to be set for cargo build (or test). Setting it for lldb doesn't help, since the test needs to be built before launching the debugger.

I normally set environment variables via ~/.pam_environment, but rust-analyzer.server.extraEnv should also work. If you want to debug that, try setting it, then check the environment of rust-analyzer:

$ cat /proc/self/environ | tr '\000' $'\n' | grep PATH

I don't remember very well, but there's a local and a remote settings.json. Adding it to ~/.bash_profile, or using SetEnv in your ~/.ssh/config might also work.

@adaszko
Copy link
Author

adaszko commented Feb 2, 2021

If I understand this correctly, you need that variable to be set for cargo build (or test). Setting it for lldb doesn't help, since the test needs to be built before launching the debugger.

Definitely! I already have that configured and the build task by itself works perfectly fine. Here it is:

{
	"type": "cargo",
	"command": "build",
	"env": {
		"PATH": ".:/root/.cargo/bin:/opt/miniconda/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin"
	},
	"problemMatcher": [
		"$rustc"
	],
	"group": "build",
	"label": "rust: cargo build"
}

But the "Debug unit test" code lens seems to use other build task which is beyond my control. I'd expect the "preLaunchTask": "rust: cargo build" to invoke the build task above, but it doesn't.

@adaszko
Copy link
Author

adaszko commented Feb 3, 2021

I've checked the rust-analyzer process running within the container via the /proc/PID/environ mechanism. It's PATH value does include the crucial /opt/miniconda/bin directory. It looks like though, rust-analyzer environment isn't being passed down to be build job started by "Debug unit test" code lens.

Environment variables set in ~/.bash_profile and ~/.bashrc do not show up in the environment of the rust-analyzer process. They also do no affect the failing linking step.

So the crucial question is: Where does rust-analyzer take the recipe to build from for the "Debug unit test" code lens? Is it automatically inferred based on Cargo.toml/cargo metadata and the selected unit test? Is there any way to control this?

@lnicola
Copy link
Member

lnicola commented Feb 3, 2021

@adaszko wait, I didn't even notice which repo this was in. Are you using the rust-lang.rust extension, or matklad.rust-analyzer? If the latter, this should be filed against https://github.com/rust-analyzer/rust-analyzer/. If the former, I'm surprised it works.

@adaszko adaszko closed this as completed Feb 4, 2021
@adaszko
Copy link
Author

adaszko commented Feb 4, 2021

I'm on matklad's actually. I'm going to open an issue there. Thank you for your help @lnicola !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants