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

Add Bazel rules for generating rust-project.json #1019

Merged
merged 1 commit into from
Jun 19, 2024

Conversation

caass
Copy link
Contributor

@caass caass commented Jun 18, 2024

Description

rust-analyzer can parse Cargo.toml files or rust-project.json files to
generate context about the projects it's run on. rust-project.json files
are a format intended to be generated by build tools other than Cargo,
such as Bazel. With this setup we can tell rust-analyzer to build the
project with Bazel instead of Cargo.

Type of change

Please delete options that aren't relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to
    not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

N/A

Checklist

  • Updated documentation if needed
  • Tests added/amended
  • bazel test //... passes locally
  • PR is contained in a single commit, using git amend see some docs

This change is Reviewable

Copy link
Contributor Author

@caass caass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+@aaronmondal

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: 0 of 1 LGTMs obtained, and pending CI: Analyze (javascript-typescript), Analyze (python), Bazel Dev / ubuntu-22.04, Cargo Dev / macos-13, Cargo Dev / ubuntu-22.04, Installation / macos-13, Installation / macos-14, Installation / ubuntu-22.04, Local / ubuntu-22.04, Publish image, Publish nativelink-worker-lre-cc, asan / ubuntu-22.04, docker-compose-compiles-nativelink (20.04), docker-compose-compiles-nativelink (22.04), integration-tests (20.04), integration-tests (22.04), macos-13, pre-commit-checks, ubuntu-20.04 / stable, ubuntu-22.04, ubuntu-22.04 / stable, vale, windows-2022 / stable, and 1 discussions need to be resolved (waiting on @aaronmondal)


BUILD.bazel line 77 at r1 (raw file):

    name = "gen_rust_project",
    actual = "@rules_rust//tools/rust_analyzer:gen_rust_project",
)

This was easier than making a genrule (or, whatever it is I would need to do) to wrap gen_rust_project, but I'm happy to change it if necessary.

Code quote:

alias(
    name = "gen_rust_project",
    actual = "@rules_rust//tools/rust_analyzer:gen_rust_project",
)

Copy link
Member

@aaronmondal aaronmondal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 3 of 3 files at r1, all commit messages.
Reviewable status: 0 of 1 LGTMs obtained, and 2 discussions need to be resolved


BUILD.bazel line 77 at r1 (raw file):

Previously, caass (Cass Fridkin) wrote…

This was easier than making a genrule (or, whatever it is I would need to do) to wrap gen_rust_project, but I'm happy to change it if necessary.

Instead of a target we could just drop a line like this into CONTRIBUTING.md. This way we save one build-level target and have documentation at the same time:

bazel run @rules_rust//tools/rust_analyzer:gen_rust_project

Or am I misunderstanding and this is supposed to run on every build?

FYI generally I'd say the alias approach is fine and actually more desirable than genrules. A genrule introduces an additional target that could potentially cause issues when run in remote execution. With an alias you save one level of indirection.


MODULE.bazel line 80 at r1 (raw file):

use_repo(crate, "crates")

rust_analyzer = use_extension("@rules_rust//tools/rust_analyzer:extension.bzl", "rust_analyzer_dependencies")

Suggestion:

rust_analyzer = use_extension(
    "@rules_rust//tools/rust_analyzer:extension.bzl",
    "rust_analyzer_dependencies",
)

Copy link
Contributor

@adam-singer adam-singer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 3 files at r1, all commit messages.
Reviewable status: 0 of 1 LGTMs obtained, and 2 discussions need to be resolved (waiting on @aaronmondal)

Copy link
Contributor Author

@caass caass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 1 LGTMs obtained, and 2 discussions need to be resolved (waiting on @aaronmondal)


BUILD.bazel line 77 at r1 (raw file):

Previously, aaronmondal (Aaron Siddhartha Mondal) wrote…

Instead of a target we could just drop a line like this into CONTRIBUTING.md. This way we save one build-level target and have documentation at the same time:

bazel run @rules_rust//tools/rust_analyzer:gen_rust_project

Or am I misunderstanding and this is supposed to run on every build?

FYI generally I'd say the alias approach is fine and actually more desirable than genrules. A genrule introduces an additional target that could potentially cause issues when run in remote execution. With an alias you save one level of indirection.

We don't need to have it built in here, but it is nice to be able to rebuild -- basically, every time you add something for bazel to build (i.e. a new rust file, new dependency, etc.) you need to rebuild the rust-project.json


MODULE.bazel line 80 at r1 (raw file):

use_repo(crate, "crates")

rust_analyzer = use_extension("@rules_rust//tools/rust_analyzer:extension.bzl", "rust_analyzer_dependencies")

Done.

@caass caass force-pushed the caass/generate-rust-project-json branch from e568ad9 to 4e51cbe Compare June 18, 2024 19:54
Copy link
Contributor

@adam-singer adam-singer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 2 files at r2, all commit messages.
Reviewable status: 0 of 1 LGTMs obtained, and pending CI: Bazel Dev / ubuntu-22.04, Cargo Dev / macos-13, Cargo Dev / ubuntu-22.04, Installation / macos-13, Installation / macos-14, Installation / ubuntu-22.04, Local / ubuntu-22.04, Publish image, Publish nativelink-worker-lre-cc, asan / ubuntu-22.04, docker-compose-compiles-nativelink (20.04), docker-compose-compiles-nativelink (22.04), integration-tests (20.04), integration-tests (22.04), macos-13, pre-commit-checks, ubuntu-20.04 / stable, ubuntu-22.04, ubuntu-22.04 / stable, windows-2022 / stable, and 3 discussions need to be resolved (waiting on @aaronmondal)


BUILD.bazel line 77 at r1 (raw file):

Previously, caass (Cass Fridkin) wrote…

We don't need to have it built in here, but it is nice to be able to rebuild -- basically, every time you add something for bazel to build (i.e. a new rust file, new dependency, etc.) you need to rebuild the rust-project.json

lets avoid alias for now, in a prior life alias looks innocent and friendly but has caused a world of complexity due to abuse. We don't need pointer redirection atm for this


CONTRIBUTING.md line 305 at r2 (raw file):

### Setting up rust-analyzer

[rust-analyzer](https://rust-analyzer.github.io/) works reasonably well out of the box due to picking up the manifest for the `nativelink` crate, but it isn't integrated with Bazel by default. In order to generate a project configuration for rust-analyzer,

Will we have problems with vscode rust analyzer competing with bazel rust analyzer in vscode?

@caass caass force-pushed the caass/generate-rust-project-json branch from 4e51cbe to ce675f5 Compare June 18, 2024 20:23
Copy link
Contributor Author

@caass caass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2 of 2 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: 0 of 1 LGTMs obtained, and pending CI: Analyze (javascript-typescript), Bazel Dev / ubuntu-22.04, Publish image, Publish nativelink-worker-lre-cc, Vercel, asan / ubuntu-22.04, docker-compose-compiles-nativelink (20.04), pre-commit-checks, ubuntu-20.04 / stable, vale, and 2 discussions need to be resolved (waiting on @aaronmondal)


CONTRIBUTING.md line 305 at r2 (raw file):

Previously, adam-singer (Adam Singer) wrote…

Will we have problems with vscode rust analyzer competing with bazel rust analyzer in vscode?

No, because you'll configure VS Code rust analyzer to talk to bazel by setting .vscode/settings.json#rust-analyzer.linkedProjects to [rust-project.json


BUILD.bazel line 77 at r1 (raw file):

Previously, adam-singer (Adam Singer) wrote…

lets avoid alias for now, in a prior life alias looks innocent and friendly but has caused a world of complexity due to abuse. We don't need pointer redirection atm for this

Totally

Copy link
Contributor

@adam-singer adam-singer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 2 of 2 files at r3, all commit messages.
Reviewable status: 0 of 1 LGTMs obtained, and pending CI: Bazel Dev / ubuntu-22.04, Cargo Dev / macos-13, Cargo Dev / ubuntu-22.04, Installation / macos-13, Installation / macos-14, Installation / ubuntu-22.04, Local / ubuntu-22.04, Publish image, Publish nativelink-worker-lre-cc, asan / ubuntu-22.04, docker-compose-compiles-nativelink (20.04), docker-compose-compiles-nativelink (22.04), integration-tests (20.04), integration-tests (22.04), macos-13, pre-commit-checks, ubuntu-20.04 / stable, ubuntu-22.04, ubuntu-22.04 / stable, windows-2022 / stable, and 1 discussions need to be resolved (waiting on @aaronmondal)

@caass caass dismissed aaronmondal’s stale review June 18, 2024 20:35

Made the requested change, and now it's stale

Copy link
Contributor Author

@caass caass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-@aaronmondal

Reviewable status: 1 of 1 LGTMs obtained, and pending CI: Cargo Dev / macos-13, Installation / macos-13, Installation / macos-14, docker-compose-compiles-nativelink (20.04), docker-compose-compiles-nativelink (22.04), integration-tests (20.04), integration-tests (22.04), macos-13, and 1 discussions need to be resolved

rust-analyzer can parse Cargo.toml files or rust-project.json files to
generate context about the projects it's run on. rust-project.json files
are a format intended to be generated by build tools other than Cargo,
such as Bazel. With this setup we can tell rust-analyzer to build the
project with Bazel instead of Cargo.
@caass caass force-pushed the caass/generate-rust-project-json branch from ce675f5 to e31b5ac Compare June 19, 2024 00:57
@caass caass enabled auto-merge (squash) June 19, 2024 01:27
Copy link
Contributor Author

@caass caass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dismissed @aaronmondal from a discussion.
Reviewable status: :shipit: complete! 1 of 1 LGTMs obtained

@caass caass merged commit bb91fa9 into TraceMachina:main Jun 19, 2024
28 checks passed
Copy link
Member

@aaronmondal aaronmondal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

Reviewed 1 of 2 files at r2, 2 of 2 files at r3, all commit messages.
Reviewable status: :shipit: complete! 2 of 1 LGTMs obtained

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants