bazelbuild / bazelbuild/rules_rust

Unable to compile rust_library when sources come from a Bazel rule emitting directory

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

Description

# Goal

We at @vaticle try to combine the usage of [`rules_antlr`](https://github.com/marcohu/rules_antlr) and [`rules_rust`](https://github.com/bazelbuild/rules_rust). `rules_antlr` run [ANTLR](https://www.antlr.org/) source code generator which generates a bunch of `*.rs` file in a directory (declared with [`ctx.actions.declare_directory`](https://docs.bazel.build/versions/main/skylark/lib/actions.html#declare_directory)). Unfortunately, it's currently impossible to consume the generated files by depending on that target directly and attempting to do so throws an error:

```
Traceback (most recent call last):
File "/home/vmax/.cache/bazel/_bazel_vmax/5777a3c137f4a070f0bdae4b608c03aa/external/rules_rust/rust/private/rust.bzl", line 182, column 32, in _rust_library_impl
return _rust_library_common(ctx, "rlib")
File "/home/vmax/.cache/bazel/_bazel_vmax/5777a3c137f4a070f0bdae4b608c03aa/external/rules_rust/rust/private/rust.bzl", line 253, column 32, in _rust_library_common
return rustc_compile_action(
File "/home/vmax/.cache/bazel/_bazel_vmax/5777a3c137f4a070f0bdae4b608c03aa/external/rules_rust/rust/private/rustc.bzl", line 643, column 36, in rustc_compile_action
args, env = construct_arguments(
File "/home/vmax/.cache/bazel/_bazel_vmax/5777a3c137f4a070f0bdae4b608c03aa/external/rules_rust/rust/private/rustc.bzl", line 496, column 20, in construct_arguments
rustc_flags.add(crate_info.root)
Error in add: Cannot add directories to Args#add since they may expand to multiple values. Either use Args#add_all (if you want expansion) or args.add(directory.path) (if you do not).
```

# Reproducible example

`WORKSPACE`:

```
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "rules_rust",
strip_prefix = "rules_rust-fe5ddf3458907b186ebe16af4f3edac22b094af0",
urls = [
"https://github.com/bazelbuild/rules_rust/archive/fe5ddf3458907b186ebe16af4f3edac22b094af0.tar.gz",
],
)
load("@rules_rust//rust:repositories.bzl", "rust_repositories")
rust_repositories()
```

`rules.bzl`:

```
def directory_emitting_rule_impl(ctx):
dir = ctx.actions.declare_directory(ctx.attr.name)
ctx.actions.run_shell(
inputs = [],
outputs = [dir],
command = "echo 'pub mod mylib {{ fn hello() {{ println!(\"HELLO\") }} }}' > {}/lib.rs".format(dir.path)
)
return [
DefaultInfo(files = depset([dir]))
]

directory_emitting_rule = rule(
implementation = directory_emitting_rule_impl,
)
```

`BUILD`:

```
load("//:rules.bzl", "directory_emitting_rule")
load("@rules_rust//rust:rust.bzl", "rust_library")

directory_emitting_rule(
name = "qq"
)

rust_library(
name = "lib",
srcs = [":qq"]
)
```

Command: `bazel build //:lib`

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.