-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge deps of related crates in Rust Analyzer support (#781)
It can happen a single source file is present in multiple crates - there can be a `rust_library` with a `lib.rs` file, and a `rust_test` for the `test` module in that file. Tests can declare more dependencies than what library had. Therefore we had to collect all `RustAnalyzerInfo`s for a given crate and take deps from all of them. There's one exception - if the dependency is the same crate name as the the crate being processed, we don't add it as a dependency to itself. This is common and expected - `rust_test.crate` pointing to the `rust_library`.
- Loading branch information
Showing
18 changed files
with
196 additions
and
39 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
load("//rust:defs.bzl", "rust_analyzer", "rust_library", "rust_test") | ||
|
||
rust_library( | ||
name = "mylib", | ||
srcs = ["mylib.rs"], | ||
deps = [":lib_dep"], | ||
) | ||
|
||
rust_library( | ||
name = "lib_dep", | ||
srcs = ["lib_dep.rs"], | ||
) | ||
|
||
rust_test( | ||
name = "mylib_test", | ||
crate = ":mylib", | ||
deps = [":extra_test_dep"], | ||
) | ||
|
||
rust_library( | ||
name = "extra_test_dep", | ||
srcs = ["extra_test_dep.rs"], | ||
) | ||
|
||
rust_analyzer( | ||
name = "rust_analyzer", | ||
testonly = True, | ||
targets = [ | ||
# it's significant that `mylib` goes before `mylib_test`. | ||
":mylib", | ||
":mylib_test", | ||
], | ||
) | ||
|
||
rust_test( | ||
name = "rust_project_json_test", | ||
srcs = ["rust_project_json_test.rs"], | ||
data = [":rust-project.json"], | ||
edition = "2018", | ||
deps = ["//tools/runfiles"], | ||
) |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
|
20 changes: 20 additions & 0 deletions
20
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use runfiles::Runfiles; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_deps_of_crate_and_its_test_are_merged() { | ||
let r = Runfiles::create().unwrap(); | ||
let rust_project_path = | ||
r.rlocation("rules_rust/test/rust_analyzer/merging_crates_test/rust-project.json"); | ||
|
||
let content = std::fs::read_to_string(&rust_project_path) | ||
.expect(&format!("couldn't open {:?}", &rust_project_path)); | ||
|
||
assert!( | ||
content.contains(r#""root_module":"test/rust_analyzer/merging_crates_test/mylib.rs","deps":[{"crate":0,"name":"lib_dep"},{"crate":2,"name":"extra_test_dep"}]"#), | ||
"expected rust-project.json to contain both lib_dep and extra_test_dep in deps of mylib.rs."); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
test/rust_analyzer/merging_crates_test_reversed/BUILD.bazel
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
load("//rust:defs.bzl", "rust_analyzer", "rust_library", "rust_test") | ||
|
||
rust_library( | ||
name = "mylib", | ||
srcs = ["mylib.rs"], | ||
deps = [":lib_dep"], | ||
) | ||
|
||
rust_library( | ||
name = "lib_dep", | ||
srcs = ["lib_dep.rs"], | ||
) | ||
|
||
rust_test( | ||
name = "mylib_test", | ||
crate = ":mylib", | ||
deps = [":extra_test_dep"], | ||
) | ||
|
||
rust_library( | ||
name = "extra_test_dep", | ||
srcs = ["extra_test_dep.rs"], | ||
) | ||
|
||
rust_analyzer( | ||
name = "rust_analyzer", | ||
testonly = True, | ||
targets = [ | ||
# it's significant that `mylib_test` goes before `mylib`. | ||
":mylib_test", | ||
":mylib", | ||
], | ||
) | ||
|
||
rust_test( | ||
name = "rust_project_json_test", | ||
srcs = ["rust_project_json_test.rs"], | ||
data = [":rust-project.json"], | ||
edition = "2018", | ||
deps = ["//tools/runfiles"], | ||
) |
1 change: 1 addition & 0 deletions
1
test/rust_analyzer/merging_crates_test_reversed/extra_test_dep.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 |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
|
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
|
21 changes: 21 additions & 0 deletions
21
test/rust_analyzer/merging_crates_test_reversed/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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use runfiles::Runfiles; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_deps_of_crate_and_its_test_are_merged() { | ||
let r = Runfiles::create().unwrap(); | ||
let rust_project_path = r.rlocation( | ||
"rules_rust/test/rust_analyzer/merging_crates_test_reversed/rust-project.json", | ||
); | ||
|
||
let content = std::fs::read_to_string(&rust_project_path) | ||
.expect(&format!("couldn't open {:?}", &rust_project_path)); | ||
|
||
assert!( | ||
content.contains(r#""root_module":"test/rust_analyzer/merging_crates_test_reversed/mylib.rs","deps":[{"crate":0,"name":"lib_dep"},{"crate":1,"name":"extra_test_dep"}]"#), | ||
"expected rust-project.json to contain both lib_dep and extra_test_dep in deps of mylib.rs."); | ||
} | ||
} |