rust-analyzer / expect-test

Minimalistic snapshot testing for Rust.
Apache License 2.0
248 stars 22 forks source link

`to_abs_ws_path` goes too far up #33

Closed fasterthanlime closed 2 years ago

fasterthanlime commented 2 years ago

Running RA tests from within a rust-lang/rust checkout fails, because expect-test climbs all the way up to rust/Cargo.toml instead of being happy with rust/src/tools/rust-analyzer/Cargo.toml, and then the paths are all wrong.

See https://github.com/rust-lang/rust/pull/99444#issuecomment-1188844202 for real-world details.

fasterthanlime commented 2 years ago

Relevant code:

https://github.com/rust-analyzer/expect-test/blob/21f04f35eb1d6d9dbcbd1f81a0a8c526432c5c0a/src/lib.rs#L644-L668

fasterthanlime commented 2 years ago

A clean fix for that would require https://github.com/rust-lang/cargo/issues/3946 to be tackled, but the workaround suggested in https://github.com/rust-lang/cargo/issues/3946#issuecomment-749645286 doesn't seem terrible to me:

$ pwd
/home/amos/bearcove/rust/src/tools/rust-analyzer/crates/ide/src/syntax_highlighting
$ cargo locate-project --workspace --message-format plain
/home/amos/bearcove/rust/src/tools/rust-analyzer/Cargo.toml
Veykril commented 2 years ago

Running cargo from a OnceCell initialization sounds a bit weird, though from the issue it does look like the only viable option... Given this is stuff that only runs in test environments it should be fine to swap to that nevertheless.

flodiebold commented 2 years ago

An alternative fix for our specific situation would be to simply set an environment variable (maybe named CARGO_WORKSPACE) from x.py and use that if present :thinking:

Veykril commented 2 years ago

That would also be a nice alternative, assuming the people responsible for x.py are fine with adding that

fasterthanlime commented 2 years ago

As another alternative, this hack doesn't involve calling out to cargo: https://github.com/rust-lang/cargo/issues/3946#issuecomment-973132993

The idea would be to create a rust-analyzer/.cargo/config.toml file with this:

[env]
CARGO_WORKSPACE_DIR = { value = "", relative = true }

Then the env var would be set when running cargo test.

edit: This would still involve changes to expect-test, but not to x.py.

flodiebold commented 2 years ago

Yeah, I like that a bit more than changing x.py since it will also work when you just run cargo test inside the rust-analyzer submodule.

fasterthanlime commented 2 years ago

Yeah, I like that a bit more than changing x.py since it will also work when you just run cargo test inside the rust-analyzer submodule.

Sounds good, I'll open an expect-test PR.

flodiebold commented 2 years ago

... or we could merge #30, I guess? :thinking:

Veykril commented 2 years ago

I don't think that PR fixes the issue, if I understand this right the CARGO_MANIFEST_DIR env var is set to a cargo toml that is a lot further up the file tree which manifests this issue here? Nevermind

fasterthanlime commented 2 years ago

... or we could merge #30, I guess? 🤔

Mhh I don't love the adhoc-toml-parsing there, it'll fail for (admittedly fringe) cases like this:

workspace = { members = [
    "xtask/",
    "lib/*",
    "crates/*",
], exclude = [
    "crates/proc-macro-test/imp",
] }
fasterthanlime commented 2 years ago

Mhh I don't love the adhoc-toml-parsing there

I'd like that PR more if it brought a real TOML parser with it but now that's overkill, and that energy is better spent pushing for the cargo bug to be fixed instead imho.

flodiebold commented 2 years ago

That's true. I'm fine with either (or both).

Veykril commented 2 years ago

I think I prefer the .cargo/config.toml approach, our heuristic should work fine for most usages of the lib, and where it breaks its a simple one line addition to the config.toml to get around the heuristic.

fasterthanlime commented 2 years ago

Follow-up RA PR: