Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_abs_ws_path goes too far up #33

Closed
fasterthanlime opened this issue Jul 19, 2022 · 15 comments
Closed

to_abs_ws_path goes too far up #33

fasterthanlime opened this issue Jul 19, 2022 · 15 comments

Comments

@fasterthanlime
Copy link
Contributor

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 rust-lang/rust#99444 (comment) for real-world details.

@fasterthanlime
Copy link
Contributor Author

Relevant code:

expect-test/src/lib.rs

Lines 644 to 668 in 21f04f3

fn to_abs_ws_path(path: &Path) -> PathBuf {
if path.is_absolute() {
return path.to_owned();
}
static WORKSPACE_ROOT: OnceCell<PathBuf> = OnceCell::new();
WORKSPACE_ROOT
.get_or_try_init(|| {
let my_manifest = env::var("CARGO_MANIFEST_DIR")?;
// Heuristic, see https://github.com/rust-lang/cargo/issues/3946
let workspace_root = Path::new(&my_manifest)
.ancestors()
.filter(|it| it.join("Cargo.toml").exists())
.last()
.unwrap()
.to_path_buf();
Ok(workspace_root)
})
.unwrap_or_else(|_: env::VarError| {
panic!("No CARGO_MANIFEST_DIR env var and the path is relative: {}", path.display())
})
.join(path)
}

@fasterthanlime
Copy link
Contributor Author

A clean fix for that would require rust-lang/cargo#3946 to be tackled, but the workaround suggested in rust-lang/cargo#3946 (comment) 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
Copy link
Member

Veykril commented Jul 19, 2022

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
Copy link
Member

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 🤔

@Veykril
Copy link
Member

Veykril commented Jul 19, 2022

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

@fasterthanlime
Copy link
Contributor Author

fasterthanlime commented Jul 19, 2022

As another alternative, this hack doesn't involve calling out to cargo: rust-lang/cargo#3946 (comment)

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
Copy link
Member

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
Copy link
Contributor Author

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
Copy link
Member

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

@Veykril
Copy link
Member

Veykril commented Jul 19, 2022

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
Copy link
Contributor Author

... 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
Copy link
Contributor Author

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.

fasterthanlime added a commit to fasterthanlime/expect-test that referenced this issue Jul 19, 2022
@flodiebold
Copy link
Member

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

@Veykril
Copy link
Member

Veykril commented Jul 19, 2022

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 added a commit to fasterthanlime/rust-analyzer that referenced this issue Jul 19, 2022
fasterthanlime added a commit to fasterthanlime/rust-analyzer that referenced this issue Jul 19, 2022
@fasterthanlime
Copy link
Contributor Author

fasterthanlime commented Jul 19, 2022

fasterthanlime added a commit to fasterthanlime/rust that referenced this issue Jul 19, 2022
This adds support for testing `rust-analyzer` in-tree.

Test rust-analyzer in CI

Set CARGO_WORKSPACE_DIR directly in `prepare_tool_cargo`

cf. rust-lang#99444 (comment)

cf. rust-analyzer/expect-test#33

Various x.py changes for Submodule => InTree, and removing toolstate

Introduce `rust-analyzer/in-rust-tree` cargo feature

This allows skipping ra's "check_merge_commits" test for now.

Later, it might be used to link against `extern proc_macro` directly,
cf. rust-lang/rust-analyzer#12803

More cleanups around the RustAnalyzer tool in bootstrap

Start fixing lints

Deny the same warnings x.py does in all RA crates

Warn on x.py warnings, don't deny

cf. https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Frust-analyzer/topic/rust-analyzer.20as.20a.20subtree.3A.20experimental.20PR.20status/near/290114101

Fix rust_2018_idioms with cargo fix

Add warning groups for more RA crates

Fix more 2015 idioms (with 2018_idioms, naming is fun)

RA passes all x.py warnings

Fix formatting after fixing idioms/additional warnings

Enable in-rust-tree feature, RA tests pass

Print stdout+stderr when nested cargo invocation fails
Veykril pushed a commit to Veykril/rust-analyzer that referenced this issue Aug 9, 2022
Veykril pushed a commit to Veykril/rust-analyzer that referenced this issue Aug 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants