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

feat: add Rust support in 'aspect init' #125

Merged
merged 14 commits into from
Oct 2, 2024
23 changes: 23 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ jobs:
- go
- py
- cpp
- rust
- minimal
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -74,6 +75,28 @@ jobs:
working-directory: "${{ steps.scaffold.outputs.dir }}"
if: "${{ matrix.preset == 'js' }}"

- name: Rust smoke test
run: |
mkdir -p hello_world/src

cat >hello_world/BUILD.bazel <<EOF
load("@rules_rust//rust:defs.bzl", "rust_binary")
alexeagle marked this conversation as resolved.
Show resolved Hide resolved

rust_binary(
name = "hello_world",
srcs = ["src/main.rs"],
)
EOF

cat >hello_world/src/main.rs <<EOF
fn main() { println!("Hello from Rust"); }
EOF

output="$(bazel run //hello_world)"
[[ "${output}" == "Hello from Rust" ]] || { echo >&2 "Wanted output 'Hello from Rust' but got '${output}'" ; exit 1 ; }
working-directory: "${{ steps.scaffold.outputs.dir }}"
if: "${{ matrix.preset == 'rust' }}"

- run: bazel lint ...
working-directory: "${{ steps.scaffold.outputs.dir }}"

Expand Down
13 changes: 11 additions & 2 deletions scaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ questions:
- Go
- Java
- C & C++
- Rust
- name: copier
prompt:
confirm: Setup code generation?
Expand Down Expand Up @@ -103,19 +104,24 @@ features:
globs:
- "*/.aspect/cli/go_image.star"
- "*/tools/oci/go_image.bzl"

- value: "{{ .Computed.rust }}"
globs:
- "*/Cargo.toml"
- "*/tools/cargo"
- "*/src/main.rs"

computed:
javascript: "{{ has \"JavaScript & TypeScript\" .Scaffold.langs }}"
python: "{{ has \"Python\" .Scaffold.langs }}"
go: "{{ has \"Go\" .Scaffold.langs }}"
java: "{{ has \"Java\" .Scaffold.langs }}"
cpp: "{{ has \"C & C++\" .Scaffold.langs }}"
rust: "{{ has \"Rust\" .Scaffold.langs }}"

presets:
kitchen-sink:
lint: true
langs: ['C & C++', 'JavaScript & TypeScript', 'Python', 'Go', 'Java']
langs: ['C & C++', 'JavaScript & TypeScript', 'Python', 'Go', 'Java', 'Rust']
copier: true
stamp: true
protobuf: true
Expand All @@ -136,5 +142,8 @@ presets:
cpp:
langs: ['C & C++']
lint: true
rust:
langs: ['Rust']
lint: true
minimal:
langs: []
8 changes: 8 additions & 0 deletions {{ .ProjectSnake }}/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "{{ .ProjectSnake }}"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
23 changes: 21 additions & 2 deletions {{ .ProjectSnake }}/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ bazel_dep(name = "rules_python_gazelle_plugin", version = "0.36.0")
bazel_dep(name = "aspect_rules_py", version = "0.8.0")
bazel_dep(name = "rules_uv", version = "0.31.0")
{{- end }}
{{- if .Computed.rust }}
bazel_dep(name = "rules_rust", version = "0.50.1")
{{- end }}
{{- if .Computed.go }}
bazel_dep(name = "rules_go", version = "0.50.1")
bazel_dep(name = "gazelle", version = "0.39.0")
Expand All @@ -38,6 +41,8 @@ multitool.hub(lockfile = "//tools:tools.lock.json")
use_repo(multitool, "multitool")
{{- if .Computed.go }}

#########################
# Go
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")

Expand All @@ -47,6 +52,8 @@ use_repo(go_deps)
{{- end }}
{{- if .Computed.javascript }}

#########################
# JavaScript and pnpm package manager
pnpm = use_extension("@aspect_rules_js//npm:extensions.bzl", "pnpm")

# Allows developers to run the same pnpm version that Bazel manages
Expand All @@ -71,6 +78,8 @@ use_repo(rules_ts_ext, "npm_typescript")
{{- end}}
{{- if .Computed.python }}

#########################
# Hermetic Python interpreter and pip dependencies
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
Expand All @@ -93,9 +102,9 @@ register_toolchains(
)
{{- end }}
{{- if .Computed.cpp }}

#########################
# Hermetic C++ toolchain, to relieve the dependency on a locally installed CC etc.

llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
llvm.toolchain(
# NB: llvm doesn't release for all platforms on every patch release
Expand All @@ -109,15 +118,25 @@ use_repo(llvm, "llvm_toolchain", "llvm_toolchain_llvm")

register_toolchains("@llvm_toolchain//:all")
{{- end }}
{{- if .Computed.rust }}

#########################
# Support for Rust, see https://github.com/bazelbuild/rules_rust
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = "2021",
versions = ["1.75.0"],
)
{{- end }}
{{- if .Computed.java }}

#########################
# Java and other JVM languages:
# https://github.com/bazelbuild/rules_jvm_external/blob/master/examples/bzlmod/MODULE.bazel
# https://github.com/bazelbuild/rules_jvm_external#pinning-artifacts-and-integration-with-bazels-downloader

# In the Maven support variation, the dependencies are conveniently listed here
# in the MODULE.bazel file.

maven = use_extension("@rules_jvm_external//:extensions.bzl", "maven")
maven.install(
artifacts = ["io.grpc:grpc-all:1.51.1"],
Expand Down
22 changes: 22 additions & 0 deletions {{ .ProjectSnake }}/README.bazel.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,25 @@ Available keys are listed in `/tools/workspace_status.sh` and may include:

To request stamped build outputs, add the flag `--config=release`.
{{ end }}

{{ if .Computed.rust }}
## Working with Cargo

If you need to run `cargo` outside of Bazel, you can do so by running `./tools/cargo`, e.g.

```console
% ./tools/cargo add reqwest
Updating crates.io index
Adding reqwest v0.12.7 to dependencies.
Features:
+ __tls
+ charset
+ default-tls
+ h2
+ http2
+ macos-system-configuration
25 deactivated features
Updating crates.io index
```

{{ end }}
6 changes: 5 additions & 1 deletion {{ .ProjectSnake }}/tools/_run_under_cwd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ case "$(basename "$0")" in
target="//tools:copier"
;;
{{- end }}
cargo)
# Being documented in https://github.com/bazelbuild/rules_rust/pull/2890
target="@rules_rust//tools/upstream_wrapper:cargo"
;;
go)
# https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/bzlmod.md#using-a-go-sdk
target="@rules_go//go"
Expand All @@ -23,4 +27,4 @@ case "$(basename "$0")" in
esac

# NB: we don't use 'bazel run' because it may leave behind zombie processes under ibazel
bazel 2>/dev/null build --build_runfile_links "$target" && BAZEL_BINDIR=. exec $(bazel info execution_root)/$(bazel 2>/dev/null cquery --output=files "$target") "$@"
bazel 2>/dev/null build --build_runfile_links "$target" && BAZEL_BINDIR=. exec $(bazel 2>/dev/null info execution_root)/$(bazel 2>/dev/null cquery --output=files "$target") "$@"
1 change: 1 addition & 0 deletions {{ .ProjectSnake }}/tools/cargo
3 changes: 3 additions & 0 deletions {{ .ProjectSnake }}/tools/format/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ format_multirun(
{{- end }}
{{- if .Computed.python }}
python = "@aspect_rules_lint//format:ruff",
{{- end }}
{{- if .Computed.rust }}
rust = "@rules_rust//tools/rustfmt:upstream_rustfmt",
{{- end }}
starlark = "@buildifier_prebuilt//:buildifier",
)
Loading