rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

Cannot use `crate.select` inside certain crate.annotations (e.g. build_script_data)

Open tshaynik opened this issue 1 year ago • 0 comments

It seems like crate.select can't be used in a variety of crate.annotations, including build_script_data.

There is this example in the rules_rust repo of using the results of a crate.select in a list for rustc_flags, it is out of date. To reproduce:

  crates_repository(
    name = "rust_crates",
    annotations = {
      "zstd-sys": [ 
                 crate.annotation(
                       build_script_data = crate.select(
                            selects = {
                                    "@rules_rust//rust/platform:myplatform": [
                                           "//:mytarget",
                                ],
                        },
                        common = [],
                    ),
                    build_script_env = crate.select(
                        selects = {
                                "@rules_rust//rust/platform:myplatform": {
                                        "ZSTD_SYS_USE_PKG_CONFIG": "1",
                                },
                        },
                        common = {},
                    ),
                ),
        ],
    })

The build_script_env annotation works, but the addition of the crate.select in build_script_data causes the following error.

	File "/home/ubuntu/.bazel-output-cache/0f174b5402e5e096a67deb6762266f43/external/rules_rust/crate_universe/private/crate.bzl", line 188, column 48, in _annotation
		build_script_data = _stringify_list(build_script_data),
	File "/home/ubuntu/.bazel-output-cache/0f174b5402e5e096a67deb6762266f43/external/rules_rust/crate_universe/private/crate.bzl", line 230, column 29, in _stringify_list
		return [str(x) for x in values]
Error: type 'struct' is not iterable

The issue seems to be here in the rules_rust codebase. The call to _stringify_list in crate.bzl breaks on a crate.build_script_data, since the result of crate.select is a struct.

Is it possible to remove or rework the invocation of _stringify_list there, so that the select struct can be passed along?

Relevant post in Bazel Slack

tshaynik avatar Oct 15 '24 20:10 tshaynik