bazelbuild / bazelbuild/rules_rust

crate_name field should allow `-` for consistency

Open
#2,455 0 comments 3 reactions 0 assignees View on GitHub
enhancement needs-triage
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

The current documentation reads:

```
Crate name to use for this target.

This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores.
```

It is common to use hyphens in crate names in Cargo.toml, and that is handled gracefully both by cargo and rules_rust. However, if you have a binary and library in the same crate, it's not unusual to want to do:

```bazel
rust_binary(
# Produces a kebab-case binary
name = "my-cli"
# ...
)

rust_library(
name = "my-cli-lib" # avoid name collision
# Inferred crate name will be my_cli_lib per the docs, so copy the name from Cargo.toml
crate_name = "my-cli" # allows code to use my_cli::Blah to compile both with Bazel & Cargo
# ...
)
```

However, this is not allowed, because if you specify this field, you need to convert the hyphens to underscores yourself.

```
rust_library(
name = "my-cli-lib"
crate_name = "my_cli" # OK
# ...
)
```

But the problem with this is that the crate's name no longer matches up with the name as specified in Cargo.toml, which is unnecessarily confusing.

How about instead converting `-` to `_` before doing the character check?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.