{"id":26717012,"url":"https://github.com/calsign/gazelle_rust","last_synced_at":"2026-05-05T06:07:37.138Z","repository":{"id":75304497,"uuid":"532189065","full_name":"Calsign/gazelle_rust","owner":"Calsign","description":"Gazelle language plugin for Rust.","archived":false,"fork":false,"pushed_at":"2024-10-07T03:56:37.000Z","size":253,"stargazers_count":25,"open_issues_count":3,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-14T01:52:23.031Z","etag":null,"topics":["bazel","gazelle","rust"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Calsign.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-03T07:29:26.000Z","updated_at":"2025-04-13T08:12:32.000Z","dependencies_parsed_at":"2023-06-06T02:15:21.872Z","dependency_job_id":"b275c8fa-ed14-4362-b2ce-d4d408ddae87","html_url":"https://github.com/Calsign/gazelle_rust","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calsign%2Fgazelle_rust","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calsign%2Fgazelle_rust/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calsign%2Fgazelle_rust/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calsign%2Fgazelle_rust/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Calsign","download_url":"https://codeload.github.com/Calsign/gazelle_rust/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248809032,"owners_count":21164895,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bazel","gazelle","rust"],"created_at":"2025-03-27T15:50:43.759Z","updated_at":"2026-05-05T06:07:37.122Z","avatar_url":"https://github.com/Calsign.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## gazelle\\_rust\n\nA Gazelle language plugin for Rust; automatic dependency management for Rust projects built with\nBazel.\n\nReferences:\n - [Gazelle](https://github.com/bazelbuild/bazel-gazelle)\n - [rules\\_rust](https://github.com/bazelbuild/rules_rust)\n - [Bazel](https://bazel.build/)\n\n## Setup\n\n[`example/MODULE.bazel`](./example/MODULE.bazel) shows how to load rules\\_rust and gazelle\\_rust\ntogether. In a real project, you would need to use `archive_override` instead of\n`local_path_override`, for example:\n\n```py\nGAZELLE_RUST_COMMIT = \"\u003ccommit\u003e\"\nGAZELLE_RUST_SHA256 = \"\u003chash\u003e\"\n\nbazel_dep(name = \"gazelle_rust\", version = \"0.1.0\")\n\narchive_override(\n    module_name = \"gazelle_rust\",\n    sha256 = GAZELLE_RUST_SHA256,\n    strip_prefix = \"gazelle_rust-{}\".format(GAZELLE_RUST_COMMIT),\n    url = \"https://github.com/Calsign/gazelle_rust/archive/{}.zip\".format(GAZELLE_RUST_COMMIT),\n)\n```\n\ngazelle\\_rust doesn't have any releases yet, so please just pick the latest commit on `main`. To\ndetermine the sha256, first set the value to `None`, then fill in the sha256 that bazel tells you.\n\ngazelle\\_rust requires rules_rust 0.40.0 or later. Previous versions required a patch to\nrules\\_rust.\n\nThe dependencies for gazelle\\_rust itself are loaded through two repository rule macros, shown in\n[`example/MODULE.bazel`](./example/MODULE.bazel). This includes setting up gazelle, but you may use\na different gazelle version by loading the gazelle repo before calling `gazelle_rust_dependencies*`.\n\ngazelle\\_rust includes a patch to gazelle which allows for reporting unused crate\\_universe\ndependencies. If you do not include the patch, everything else will still work fine but unused\ncrate\\_universe dependencies will not be reported.\n\n## Running gazelle\n\n[`example/BUILD.bazel`](./example/BUILD.bazel) shows\nhow to create the gazelle target. With the gazelle target defined, you can run it:\n\n```sh\nbazel run //:gazelle\n```\n\nThis will modify your build files in-place to create and update rust targets for all of the rust\nfiles in your project.\n\ngazelle\\_rust provides a premade gazelle binary, but you can also create your own `gazelle_binary`\ntarget and add `@gazelle_rust//rust_language` to languages.\n\n## Generated targets\n\n[`example/src/BUILD.bazel`](./example/src/BUILD.bazel)\nshows sample targets generated by gazelle. The following rules are supported:\n\n * `rust_library`\n * `rust_binary`\n * `rust_test`\n * `rust_proc_macro`\n * `rust_shared_library`\n * `rust_static_library`\n\nWhen generating targets for new sources (those not already listed in `srcs` for an existing target),\ngazelle\\_rust will infer the rule kind based on information like whether the file has a `main` and\nthe name of the directory. The full logic is in `inferRuleKind` in\n[`rust_language/generate.go`](./rust_language/generate.go). If you change the rule kind afterward,\ngazelle\\_rust will respect the existing rule kind.\n\nBy default gazelle\\_rust will generate one target per source file. You may change the grouping by\nadding a file to `srcs` for an existing target, and gazelle will respect that existing grouping.\n\ngazelle\\_rust does not currently support sources in subdirectories, and will always place targets\ninto build files adjacent to the sources that they correspond to.\n\n## Generation from Cargo.toml files\n\nIt is possible to instruct gazelle\\_rust to generate `BUILD.bazel` files next to existing\n`Cargo.toml` files, which may be helpful if you would like to keep `Cargo.toml` files around and\nstructure your Bazel definitions around them. This mode of operation is enabled by adding the\nfollowing directive:\n\n```py\n# gazelle:rust_mode generate_from_cargo\n```\n\nIn this mode of operation it is also possible to specify the default Rust edition, like so:\n\n```py\n# gazelle:rust_default_edition 2021\n```\n\nThe value this directive is set to should normally match the\n[`default_edition`](https://bazelbuild.github.io/rules_rust/rust_toolchains.html#rust_toolchain-default_edition)\nattribute on the Rust toolchain. Whenever the edition is directly specified in the `Cargo.toml` file\n(i.e. it is not inherited from the workspace file) and it is different from the default edition, the\ncorresponding `\"edition\"` attribute will be added to the generated targets. If\n`gazelle:rust_default_edition` is not set, the `\"edition\"` attribute will always be added when it is\nspecified.\n\n## Crate features\n\ngazelle\\_rust reads the set of features in the `crate_features` attribute and skips adding\ndependencies which are not needed for the enabled features.\n\nFor `generate_from_cargo` mode, the initial `crate_features` list is generated from the default\nfeatures list in `Cargo.toml`. This behavior for newly-generated targets can be further customized\nwith the following directives which apply to the package in which they are defined and subpackages:\n\n```py\n# gazelle:rust_default_features \u003ctrue|false\u003e\n# gazelle:rust_feature \u003cfeature_name\u003e \u003ctrue|false\u003e\n```\n\n`gazelle:rust_default_features false` disables use of the default features from `Cargo.toml`,\nanalogous to `default_features = false` in cargo.\n\n`gazelle:rust_feature my_feature \u003ctrue|false\u003e` adds an override for a specific feature name to add\nor remove that feature from the set of features added to newly-generated targets. The feature will\nonly be added to a rust target if that feature is present in `Cargo.toml` for that package.\n\n## Assigning dependencies\n\ngazelle\\_rust parses each source file and identifies any path that looks like an external crate\ndependency. For example `some_lib::Foobar` implies a new dependency on `some_lib` unless `some_lib`\nis already in scope.\n\nThis approach is fairly robust. Please see [`rust_parser/parser.rs`](./rust_parser/parser.rs) for\nimplementation details and the [parser\ntests](https://github.com/Calsign/gazelle_rust/tree/main/rust_parser/test_data) for the range of\ncases covered.\n\nFor each dependency, gazelle\\_rust identifies the crate in the project (or crate universe\ndependency) providing that crate name. gazelle\\_rust raises an error if the crate could not be found\nor more than one crate with that name was found.\n\nThis means there is a global namespace of crates within the project. If this poses an issue for you,\nyou can use the gazelle [`resolve`\ndirective](https://github.com/bazelbuild/bazel-gazelle#directives) to configure which target is\nselected on a per-directory basis.\n\n## Crate universe\n\nThe example shows how to handle crate universe dependencies with gazelle\\_rust.\n[`example/MODULE.bazel`](./example/MODULE.bazel) shows how to load crate universe dependencies,\nplease refer to the [rules\\_rust\ndocumentation](http://bazelbuild.github.io/rules_rust/crate_universe.html) for more information. The\nexample shows the repository rule approach, but gazelle\\_rust also supports the vendored approach.\n\n[`example/BUILD.bazel`](./example/BUILD.bazel) shows\nhow to configure gazelle\\_rust to resolve crate universe dependencies.\n\nDifferent configurations of crate universe use either a cargo lockfile (`Cargo.lock`) or a custom\nlockfile (`Cargo.Bazel.lock`), and gazelle\\_rust supports both. Use the directive\n`gazelle:rust_cargo_lockfile` to indicate a cargo lockfile and `gazelle:rust_lockfile` to indicate a\ncustom lockfile. These options are mutually exclusive.\n\nAdditionally, you must tell rules\\_rust the prefix for all crate universe labels using the\n`gazelle:rust_crates_prefix` directive, e.g. `@crates//:` for a repository rule approach or\n`//3rdparty/crates:` for a vendored approach.\n\n## Ignoring dependencies\n\nSome situations are too complex for gazelle\\_rust to handle, such as platform-conditional\ndependencies. It is possible that fancy support could be added in the future, but for now you must\nhandle this manually by ignoring the dependency in the source file and potentially adding [`# keep`\ncomments](https://github.com/bazelbuild/bazel-gazelle#keep-comments) in the build file.\n\nTo tell gazelle\\_rust to ignore a dependency, you can add the `#[gazelle::ignore]` attribute macro\nto a use item. For example:\n\n```rust\n// the tokio runtime is not supported in wasm\n#[cfg(not(target_arch = \"wasm32\"))]\n#[gazelle::ignore]\nuse tokio::runtime::Runtime;\n```\n\nThen in the build file:\n\n```py\nrust_library(\n    name = \"maybe_tokio\",\n    deps = select({\n        \"@platforms//cpu:wasm32\": [],\n        \"//conditions:default\": [\n            \"//3rdparty/crates:tokio\",\n        ],\n    }),\n)\n\nrust_library(\n    name = \"some_cool_cross_platform_thing\",\n    deps = [\n        \":maybe_tokio\",  # keep\n    ],\n    ...\n)\n```\n\nSee the [macro crate](./macro) for more information about the ignore macro.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalsign%2Fgazelle_rust","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalsign%2Fgazelle_rust","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalsign%2Fgazelle_rust/lists"}