Skip to content

Commit a066bfe

Browse files
authored
Replace slashes with underscores in default crate names. (#1336)
This is another attempt at #1334, which sought to make "/" characters legal in Rust target names. This PR differs from #1334 in how it handles build artifacts that are based on `crate_info.output`. There are a few places that currently assume that calling `ctx.actions.declare_file(...)` will always declare a file in the same directory as `crate_info.output`, i.e. as `ctx.actions.declare_file(ctx.label.name + ...)`. In a world where label names may include slashes (which is the case today!), this is not a safe assumption, since a slash in the target name causes bazel to create a subdirectory for the output. Specifically, this PR fixes incorrect locations for the following items (when the target name includes a slash): * PDB files * dSYM files * .lib files This leaves a few things that look like they make the same assumption, but I'm not sure how they're used or if they should be updated: * `"_ambiguous_libs/" + crate_info.output.basename + "/" + symlink_name` for ambiguous libraries * The linkstamp output path (`"_objs/" + crate_info.output.basename + "/" + ...`)
1 parent 1b91e59 commit a066bfe

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

rust/private/rustc.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ def rustc_compile_action(
923923
if toolchain.os == "windows" and crate_info.type == "cdylib":
924924
# Rustc generates the import library with a `.dll.lib` extension rather than the usual `.lib` one that msvc
925925
# expects (see https://github.com/rust-lang/rust/pull/29520 for more context).
926-
interface_library = ctx.actions.declare_file(crate_info.output.basename + ".lib")
926+
interface_library = ctx.actions.declare_file(crate_info.output.basename + ".lib", sibling = crate_info.output)
927927
outputs.append(interface_library)
928928

929929
# The action might generate extra output that we don't want to include in the `DefaultInfo` files.
@@ -935,10 +935,10 @@ def rustc_compile_action(
935935
dsym_folder = None
936936
if crate_info.type in ("cdylib", "bin") and not crate_info.is_test:
937937
if toolchain.os == "windows":
938-
pdb_file = ctx.actions.declare_file(crate_info.output.basename[:-len(crate_info.output.extension)] + "pdb")
938+
pdb_file = ctx.actions.declare_file(crate_info.output.basename[:-len(crate_info.output.extension)] + "pdb", sibling = crate_info.output)
939939
action_outputs.append(pdb_file)
940940
elif toolchain.os == "darwin":
941-
dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM")
941+
dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM", sibling = crate_info.output)
942942
action_outputs.append(dsym_folder)
943943

944944
if ctx.executable._process_wrapper:

rust/private/utils.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ def name_to_crate_name(name):
272272
Returns:
273273
str: The name of the crate for this target.
274274
"""
275-
return name.replace("-", "_")
275+
for illegal in ("-", "/"):
276+
name = name.replace(illegal, "_")
277+
return name
276278

277279
def _invalid_chars_in_crate_name(name):
278280
"""Returns any invalid chars in the given crate name.

test/unit/crate_name/crate_name_test.bzl

+9-9
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def _custom_crate_name_test_test_impl(ctx):
4848

4949
def _invalid_default_crate_name_test_impl(ctx):
5050
env = analysistest.begin(ctx)
51-
asserts.expect_failure(env, "contains invalid character(s): /")
51+
asserts.expect_failure(env, "contains invalid character(s): @")
5252
return analysistest.end(env)
5353

5454
def _invalid_custom_crate_name_test_impl(ctx):
@@ -119,7 +119,7 @@ no_extra_filename_test = analysistest.make(
119119

120120
def _crate_name_test():
121121
rust_library(
122-
name = "default-crate-name-library",
122+
name = "default/crate-name-library",
123123
srcs = ["lib.rs"],
124124
edition = "2018",
125125
)
@@ -132,7 +132,7 @@ def _crate_name_test():
132132
)
133133

134134
rust_binary(
135-
name = "default-crate-name-binary",
135+
name = "default/crate-name-binary",
136136
srcs = ["main.rs"],
137137
edition = "2018",
138138
)
@@ -145,7 +145,7 @@ def _crate_name_test():
145145
)
146146

147147
rust_test(
148-
name = "default-crate-name-test",
148+
name = "default/crate-name-test",
149149
srcs = ["main.rs"],
150150
edition = "2018",
151151
)
@@ -158,7 +158,7 @@ def _crate_name_test():
158158
)
159159

160160
rust_library(
161-
name = "invalid/default-crate-name",
161+
name = "invalid@default-crate-name",
162162
srcs = ["lib.rs"],
163163
edition = "2018",
164164
tags = ["manual", "norustfmt"],
@@ -197,7 +197,7 @@ def _crate_name_test():
197197

198198
default_crate_name_library_test(
199199
name = "default_crate_name_library_test",
200-
target_under_test = ":default-crate-name-library",
200+
target_under_test = ":default/crate-name-library",
201201
)
202202

203203
custom_crate_name_library_test(
@@ -207,7 +207,7 @@ def _crate_name_test():
207207

208208
default_crate_name_binary_test(
209209
name = "default_crate_name_binary_test",
210-
target_under_test = ":default-crate-name-binary",
210+
target_under_test = ":default/crate-name-binary",
211211
)
212212

213213
custom_crate_name_binary_test(
@@ -217,7 +217,7 @@ def _crate_name_test():
217217

218218
default_crate_name_test_test(
219219
name = "default_crate_name_test_test",
220-
target_under_test = ":default-crate-name-test",
220+
target_under_test = ":default/crate-name-test",
221221
)
222222

223223
custom_crate_name_test_test(
@@ -227,7 +227,7 @@ def _crate_name_test():
227227

228228
invalid_default_crate_name_test(
229229
name = "invalid_default_crate_name_test",
230-
target_under_test = ":invalid/default-crate-name",
230+
target_under_test = ":invalid@default-crate-name",
231231
)
232232

233233
invalid_custom_crate_name_test(

0 commit comments

Comments
 (0)