Skip to content

Commit

Permalink
Rust Analyzer: Generate rust-project.json without explicit target list (
Browse files Browse the repository at this point in the history
#1010)

* Emit rust_analyzer_crate_spec files for all workspace crates

* Regenerated cargo-raze outputs

* Address PR feedback

* Increase Bazel min test version

* Addressed PR feedback

* Make sysroot_src a data dependency

* Fix runfiles path specifier

* Use bazel-like space separated target specification

* Fix out-of-date comment

* Addressed PR feedback

* Added additional testing

* Added test comment

Co-authored-by: David Marcin <david@metawork.com>
  • Loading branch information
UebelAndre and djmarcin committed Nov 17, 2021
1 parent 95d29b4 commit 8c05ac7
Show file tree
Hide file tree
Showing 67 changed files with 2,666 additions and 483 deletions.
9 changes: 7 additions & 2 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ tasks:
bazel: "rolling"
ubuntu1804:
name: "Min Bazel Version"
bazel: "3.5.0"
bazel: "4.0.0"
platform: ubuntu1804
build_targets: *default_linux_targets
test_targets: *default_linux_targets
ubuntu1804_with_aspects:
name: "Min Bazel Version With Aspects"
bazel: "3.5.0"
bazel: "4.0.0"
platform: ubuntu1804
build_targets: *default_linux_targets
test_targets: *default_linux_targets
Expand Down Expand Up @@ -172,6 +172,11 @@ tasks:
platform: ubuntu2004
run_targets:
- "//test/rustfmt:test_runner"
rust_analyzer_tests:
name: Rust-Analyzer Tests
platform: ubuntu2004
run_targets:
- "//test/rust_analyzer:rust_analyzer_test"
ubuntu2004_examples:
name: Examples
platform: ubuntu2004
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@

# BazelCI
bazelci.py

# rust-analyzer
rust-project.json
2 changes: 1 addition & 1 deletion docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ A rule defining an incompatible flag.
rust_analyzer(<a href="#rust_analyzer-name">name</a>, <a href="#rust_analyzer-targets">targets</a>)
</pre>

Produces a rust-project.json for the given targets. Configure rust-analyzer to load the generated file via the linked projects mechanism.
Deprecated: gen_rust_project can now create a rust-project.json without a rust_analyzer rule.


**ATTRIBUTES**
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ rust_repositories()
```

The rules are under active development, as such the lastest commit on the
`main` branch should be used. `main` is only tested against `3.5.0` as the
`main` branch should be used. `main` is only tested against `4.0.0` as the
minimum supported version of Bazel. Though previous versions may still be
functional in certain environments.

Expand Down
35 changes: 7 additions & 28 deletions docs/rust_analyzer.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

For [non-Cargo projects](https://rust-analyzer.github.io/manual.html#non-cargo-based-projects),
[rust-analyzer](https://rust-analyzer.github.io/) depends on a `rust-project.json` file at the
root of the project that describes its structure. The `rust_analyzer` rule facilitates generating
root of the project that describes its structure. The `rust_analyzer` rule facilitates generating
such a file.

### Setup
Expand All @@ -32,32 +32,11 @@ can also set `include_rustc_srcs = True` on any `rust_repository` or
`rust_repositories` calls in the workspace but the environment variable
has higher priority and can override the attribute.

Finally, add a rule to the root `BUILD` file like the following.

```python
load("@rules_rust//rust:defs.bzl", "rust_analyzer")

rust_analyzer(
name = "rust_analyzer",
targets = [
# all the binary/library targets you want in the rust-project.json
],
)
```

A list of `rust_analyzer` compatible targets can be found by usign the following query:

```bash
bazel query 'kind("rust_*library|rust_binary", //...:all)'
```

Note: __All `rust_*` targets provided to the root rust_analyzer must have `//visibility:public`.__

Run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project` whenever
dependencies change to regenerate the `rust-project.json` file. It should be
added to `.gitignore` because it is effectively a build artifact. Once the
`rust-project.json` has been generated in the project root, rust-analyzer can
pick it up upon restart.
Finally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`
whenever dependencies change to regenerate the `rust-project.json` file. It
should be added to `.gitignore` because it is effectively a build artifact.
Once the `rust-project.json` has been generated in the project root,
rust-analyzer can pick it up upon restart.

#### VSCode

Expand Down Expand Up @@ -100,7 +79,7 @@ to ensure a `rust-project.json` file is created and up to date when the editor i
rust_analyzer(<a href="#rust_analyzer-name">name</a>, <a href="#rust_analyzer-targets">targets</a>)
</pre>

Produces a rust-project.json for the given targets. Configure rust-analyzer to load the generated file via the linked projects mechanism.
Deprecated: gen_rust_project can now create a rust-project.json without a rust_analyzer rule.


**ATTRIBUTES**
Expand Down
33 changes: 6 additions & 27 deletions docs/rust_analyzer.vm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

For [non-Cargo projects](https://rust-analyzer.github.io/manual.html#non-cargo-based-projects),
[rust-analyzer](https://rust-analyzer.github.io/) depends on a `rust-project.json` file at the
root of the project that describes its structure. The `rust_analyzer` rule facilitates generating
root of the project that describes its structure. The `rust_analyzer` rule facilitates generating
such a file.

### Setup
Expand All @@ -26,32 +26,11 @@ can also set `include_rustc_srcs = True` on any `rust_repository` or
`rust_repositories` calls in the workspace but the environment variable
has higher priority and can override the attribute.

Finally, add a rule to the root `BUILD` file like the following.

```python
load("@rules_rust//rust:defs.bzl", "rust_analyzer")

rust_analyzer(
name = "rust_analyzer",
targets = [
# all the binary/library targets you want in the rust-project.json
],
)
```

A list of `rust_analyzer` compatible targets can be found by usign the following query:

```bash
bazel query 'kind("rust_*library|rust_binary", //...:all)'
```

Note: __All `rust_*` targets provided to the root rust_analyzer must have `//visibility:public`.__

Run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project` whenever
dependencies change to regenerate the `rust-project.json` file. It should be
added to `.gitignore` because it is effectively a build artifact. Once the
`rust-project.json` has been generated in the project root, rust-analyzer can
pick it up upon restart.
Finally, run `bazel run @rules_rust//tools/rust_analyzer:gen_rust_project`
whenever dependencies change to regenerate the `rust-project.json` file. It
should be added to `.gitignore` because it is effectively a build artifact.
Once the `rust-project.json` has been generated in the project root,
rust-analyzer can pick it up upon restart.

#### VSCode

Expand Down
6 changes: 6 additions & 0 deletions rust/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//rust/private:rust_analyzer.bzl", "rust_analyzer_detect_sysroot")
load("//rust/private:stamp.bzl", "stamp_build_setting")

bzl_library(
Expand All @@ -13,3 +14,8 @@ bzl_library(
)

stamp_build_setting(name = "stamp")

rust_analyzer_detect_sysroot(
name = "rust_analyzer_detect_sysroot",
visibility = ["//visibility:public"],
)
Loading

0 comments on commit 8c05ac7

Please sign in to comment.