forked from bazelbuild/rules_rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rust_analyzer: make all paths in rust-project.json absolute
This is a step towards supporting automatic project discovery (bazelbuild#2755). Relative paths are resolved against the location of rust-project.json, but in auto-discovery this is the location of the BUILD file being discovered, not the workspace root. We already use absolute paths for generated files, so rust-project.json is not workspace-independent today. The alternatives seem more complex for little gain: - Only use absolute paths for auto-discovery mode, relative otherwise. - Use ../../ to express all workspace paths relative to the BUILD. See bazelbuild#2755 (comment)
- Loading branch information
1 parent
4e54d34
commit 68307e3
Showing
9 changed files
with
121 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 36 additions & 29 deletions
65
test/rust_analyzer/generated_srcs_test/rust_project_json_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,50 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use serde::Deserialize; | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Deserialize)] | ||
struct Project { | ||
sysroot_src: String, | ||
crates: Vec<Crate>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Crate { | ||
display_name: String, | ||
root_module: String, | ||
source: Option<Source>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Source { | ||
include_dirs: Vec<String>, | ||
} | ||
|
||
#[test] | ||
fn test_deps_of_crate_and_its_test_are_merged() { | ||
fn test_generated_srcs() { | ||
let rust_project_path = PathBuf::from(env::var("RUST_PROJECT_JSON").unwrap()); | ||
|
||
let content = std::fs::read_to_string(&rust_project_path) | ||
.unwrap_or_else(|_| panic!("couldn't open {:?}", &rust_project_path)); | ||
println!("{}", content); | ||
let project: Project = | ||
serde_json::from_str(&content).expect("Failed to deserialize project JSON"); | ||
|
||
let output_base = content | ||
.lines() | ||
.find(|text| text.trim_start().starts_with("\"sysroot_src\":")) | ||
.map(|text| { | ||
let mut split = text.splitn(2, "\"sysroot_src\": "); | ||
let mut with_hash = split.nth(1).unwrap().trim().splitn(2, "/external/"); | ||
let mut output = with_hash.next().unwrap().rsplitn(2, '/'); | ||
output.nth(1).unwrap() | ||
}) | ||
.expect("Failed to find sysroot entry."); | ||
// /tmp/_bazel/12345678/external/tools/rustlib/library => /tmp/bazel/12345678 | ||
let output_base = project.sysroot_src.rsplitn(2, "/external/").last().unwrap(); | ||
println!("output_base: {output_base}"); | ||
|
||
let expected = r#"{ | ||
"display_name": "generated_srcs", | ||
"root_module": "lib.rs", | ||
"edition": "2021", | ||
"deps": [], | ||
"is_workspace_member": true, | ||
"source": { | ||
"include_dirs": [ | ||
"# | ||
.to_owned() | ||
+ output_base; | ||
let gen = project | ||
.crates | ||
.iter() | ||
.find(|c| &c.display_name == "generated_srcs") | ||
.unwrap(); | ||
assert!(gen.root_module.starts_with("/")); | ||
assert!(gen.root_module.ends_with("/lib.rs")); | ||
|
||
println!("{}", content); | ||
assert!( | ||
content.contains(&expected), | ||
"expected rust-project.json to contain the following block:\n{}", | ||
expected | ||
); | ||
let include_dirs = &gen.source.as_ref().unwrap().include_dirs; | ||
assert!(include_dirs.len() == 1); | ||
assert!(include_dirs[0].starts_with(output_base)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 28 additions & 20 deletions
48
test/rust_analyzer/merging_crates_test/rust_project_json_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,41 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use serde::Deserialize; | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Deserialize)] | ||
struct Project { | ||
crates: Vec<Crate>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Crate { | ||
display_name: String, | ||
deps: Vec<Dep>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Dep { | ||
name: String, | ||
} | ||
|
||
#[test] | ||
fn test_deps_of_crate_and_its_test_are_merged() { | ||
let rust_project_path = PathBuf::from(env::var("RUST_PROJECT_JSON").unwrap()); | ||
|
||
let content = std::fs::read_to_string(&rust_project_path) | ||
.unwrap_or_else(|_| panic!("couldn't open {:?}", &rust_project_path)); | ||
|
||
let expected = r#"{ | ||
"display_name": "mylib", | ||
"root_module": "mylib.rs", | ||
"edition": "2018", | ||
"deps": [ | ||
{ | ||
"crate": 0, | ||
"name": "extra_test_dep" | ||
}, | ||
{ | ||
"crate": 1, | ||
"name": "lib_dep" | ||
} | ||
],"#; | ||
|
||
println!("{}", content); | ||
assert!( | ||
content.contains(expected), | ||
"expected rust-project.json to contain both lib_dep and extra_test_dep in deps of mylib.rs."); | ||
let project: Project = | ||
serde_json::from_str(&content).expect("Failed to deserialize project JSON"); | ||
|
||
let lib = project | ||
.crates | ||
.iter() | ||
.find(|c| &c.display_name == "mylib") | ||
.unwrap(); | ||
let mut deps = lib.deps.iter().map(|d| &d.name).collect::<Vec<_>>(); | ||
deps.sort(); | ||
assert!(deps == vec!["extra_test_dep", "lib_dep"]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 27 additions & 17 deletions
44
test/rust_analyzer/static_and_shared_lib_test/rust_project_json_test.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,41 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use serde::Deserialize; | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Deserialize)] | ||
struct Project { | ||
crates: Vec<Crate>, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct Crate { | ||
display_name: String, | ||
root_module: String, | ||
} | ||
|
||
#[test] | ||
fn test_deps_of_crate_and_its_test_are_merged() { | ||
fn test_static_and_shared_lib() { | ||
let rust_project_path = PathBuf::from(env::var("RUST_PROJECT_JSON").unwrap()); | ||
|
||
let content = std::fs::read_to_string(&rust_project_path) | ||
.unwrap_or_else(|_| panic!("couldn't open {:?}", &rust_project_path)); | ||
|
||
println!("{}", content); | ||
let project: Project = | ||
serde_json::from_str(&content).expect("Failed to deserialize project JSON"); | ||
|
||
let expected_cdylib = r#"{ | ||
"display_name": "greeter_cdylib", | ||
"root_module": "shared_lib.rs","#; | ||
assert!( | ||
content.contains(expected_cdylib), | ||
"expected rust-project.json to contain a rust_shared_library target." | ||
); | ||
let cdylib = project | ||
.crates | ||
.iter() | ||
.find(|c| &c.display_name == "greeter_cdylib") | ||
.unwrap(); | ||
assert!(cdylib.root_module.ends_with("/shared_lib.rs")); | ||
|
||
let expected_staticlib = r#"{ | ||
"display_name": "greeter_staticlib", | ||
"root_module": "static_lib.rs","#; | ||
assert!( | ||
content.contains(expected_staticlib), | ||
"expected rust-project.json to contain a rust_static_library target." | ||
); | ||
let staticlib = project | ||
.crates | ||
.iter() | ||
.find(|c| &c.display_name == "greeter_staticlib") | ||
.unwrap(); | ||
assert!(staticlib.root_module.ends_with("/static_lib.rs")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters