Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a crate_name attribute to Rust rules #645

Merged
merged 7 commits into from
Mar 31, 2021

Conversation

martinboehme
Copy link
Collaborator

This allows specifying a different crate name than the one derived by default from the Bazel target name.

This is useful, for example, if you want to use a target name that would not be legal as a crate name, e.g. a target name containing slashes.

This commit introduces a function _crate_name() and uses it where we were formerly determining the crate name using name_to_crate_name(ctx.label.name).

There were some additional uses of name_to_crate_name in the test rule where we weren't actually determining a crate name but an output file name for the test binary or the test launcher. It's not clear to me why these needed the hyphen-to-underscore conversion that name_to_crate_name performs, as hyphens are legal in file names. I think these places should be doing one of the following:

  • Create a file name based on the target name, without hyphen-to-underscore substitutions.
  • Create a file name based on the (possibly non-default) crate name.

I think one of these two changes should be made, but I don't want to do so in the commit because either of these changes would create a change in behavior for existing uses of the rust_test rule. Also, the issue of test binary and test launcher file names is largely orthogonal to the purpose of this commit.

Finally, there is also a use of name_to_crate_name in cargo_build_script.bzl to replace hyphens in feature names with
underscores. This has nothing to do with crate names; it's just using an existing function that conveniently happens to be doing the thing the code needs. I think this use of name_to_crate_name should be replaced with a local function that does the same thing, but again, I'll do so in a separate commit.

@google-cla google-cla bot added the cla: yes label Mar 25, 2021
@martinboehme
Copy link
Collaborator Author

@hlopko Can you review?

@martinboehme
Copy link
Collaborator Author

I'm new to writing Bazel rules, so I have a few questions on the test failures.

  • The "Minimum Supported Version" test is failing with the error
    Error in fail: fail() accepts no more than 2 positional arguments but got 4.
    
    I believe this test is trying to make sure that the rules work on the minimum supported version of Bazel... correct? And it looks as if that version does not yet support arbitrarily many arguments to fail(). Should I make sure to pass only a single argument to fail() (by just concatenating the strings myself)?
  • The Windows test is failing with the error
    error: could not write output to bazel-out/x64_windows-fastbuild/bin/test/unit/crate_name\custom_name.hjct7k57wmifsnz.rcgu.o: permission denied". 
    
    The mix of forward and backward slashes in that path looks strange, but I'm not sure how my changes could have caused this, and I don't have a Windows system immediately available that I could debug this on. Any pointers on what could be going wrong here?

Copy link
Collaborator

@illicitonion illicitonion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

This allows specifying a different crate name than the one derived by default from the Bazel target name.

It'd be nice to avoid this feature if possible by changing names, but if that's not possible, this change generally seems reasonable :)

Finally, there is also a use of name_to_crate_name in cargo_build_script.bzl to replace hyphens in feature names with
underscores. This has nothing to do with crate names; it's just using an existing function that conveniently happens to be doing the thing the code needs. I think this use of name_to_crate_name should be replaced with a local function that does the same thing, but again, I'll do so in a separate commit.

I have #643 out which overhauls this - would appreciate your thoughts (and it may change how you're thinking about that separate commit :))

rust/private/rust.bzl Show resolved Hide resolved
rust/private/rust.bzl Outdated Show resolved Hide resolved
@hlopko hlopko self-requested a review March 29, 2021 07:40
@hlopko
Copy link
Member

hlopko commented Mar 29, 2021

@martinboehme
Copy link
Collaborator Author

Thanks for the contribution!

This allows specifying a different crate name than the one derived by default from the Bazel target name.

It'd be nice to avoid this feature if possible by changing names, but if that's not possible, this change generally seems reasonable :)

Finally, there is also a use of name_to_crate_name in cargo_build_script.bzl to replace hyphens in feature names with
underscores. This has nothing to do with crate names; it's just using an existing function that conveniently happens to be doing the thing the code needs. I think this use of name_to_crate_name should be replaced with a local function that does the same thing, but again, I'll do so in a separate commit.

I have #643 out which overhauls this - would appreciate your thoughts (and it may change how you're thinking about that separate commit :))

Thanks for pointing that out -- I've added comments there (rather many of them in fact...).

I do still feel that using the name_to_crate_name function for feature names is a bit dubious -- see detailed rationale in #643. I'd like to submit a PR that adds an _env_var_from_feature_name function -- this would also handle adding the CARGO_FEATURE_ prefix. But I'll postpone this until #643 is in so I can add the corresponding test to the new test files you're adding in #643.

martinboehme added a commit to martinboehme/rules_rust that referenced this pull request Mar 30, 2021
I'm getting test failures on Windows for

bazelbuild#645

where the linker is complaining that it can't write to the output file.
The only reason I can imagine for this is that the test environment is
forbidding writes to anything other than the declared output files.

As I can't test on Windows directly myself, I regard this as an
experimental change that I'm pushing to a draft PR solely for the
purpose of checking whether this fixes the failing test.
martinboehme added a commit to martinboehme/rules_rust that referenced this pull request Mar 30, 2021
This fixes test failures on Windows that I was getting for

bazelbuild#645

where the linker was complaining that it couldn't write to the output file.
I assume that the test environment is preventing writes to anything other than
the declared output files.
martinboehme added a commit to martinboehme/rules_rust that referenced this pull request Mar 30, 2021
This fixes test failures on Windows that I was getting for

bazelbuild#645

where the linker was complaining that it couldn't write to the output file.
I assume that the test environment is preventing writes to anything other than
the declared output files.
Copy link
Member

@hlopko hlopko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for working on this!

rust/private/rustc.bzl Show resolved Hide resolved
test/unit/crate_name/crate_name_test.bzl Outdated Show resolved Hide resolved
test/unit/crate_name/main.rs Show resolved Hide resolved
@martinboehme
Copy link
Collaborator Author

Rebasing to HEAD now.

This allows specifying a different crate name than the one derived by
default from the Bazel target name.

This is useful, for example, if you want to use a target name that would
not be legal as a crate name, e.g. a target name containing slashes.

This commit introduces a function _crate_name() and uses it where we
were formerly determining the crate name using
name_to_crate_name(ctx.label.name).

There were some additional uses of name_to_crate_name in the test rule
where we weren't actually determining a crate name but an output file
name for the test binary or the test launcher. It's not clear to me why
these needed the hyphen-to-underscore conversion that name_to_crate_name
performs, as hyphens are legal in file names. I think these places
should be doing one of the following:

- Create a file name based on the target name, without
  hyphen-to-underscore substitutions.
- Create a file name based on the (possibly non-default) crate name.

I think one of these two changes should be made, but I don't want to do
so in the commit because either of these changes would create a change
in behavior for existing uses of the rust_test rule. Also, the issue of
test binary and test lancher file names is largely orthogonal to the
purpose of this commit.

Finally, there is also a use of name_to_crate_name in
cargo_build_script.bzl to replace hyphens in feature names with
underscores. This has nothing to do with crate names; it's just using
an existing function that conveniently happens to be doing the thing the
code needs. I think this use of name_to_crate_name should be replaced
with a local function that does the same thing, but again, I'll do so in
a separate commit.
This fixes test failures on Windows that I was getting for

bazelbuild#645

where the linker was complaining that it couldn't write to the output file.
I assume that the test environment is preventing writes to anything other than
the declared output files.
The label isn't required, because we can also get the name from the
attributes.
@martinboehme
Copy link
Collaborator Author

I've added a TODO that the CARGO_CRATE_NAME environment variable (added in #643) needs to respect the crate_name attribute.

I originally planned to do this in this PR, but then I stumbled across a bug in the Clippy aspect implementation where it's accessing ctx.attr instead of ctx.rule.attr and so not finding the crate_name attribute. I'll fix this bug in a separate PR and will then handle the outstanding TODO.

@hlopko hlopko merged commit 77fde0f into bazelbuild:main Mar 31, 2021
@martinboehme martinboehme deleted the crate-name branch March 31, 2021 08:26
martinboehme added a commit to martinboehme/rules_rust that referenced this pull request Mar 31, 2021
Normal `rust_binary` targets don't do this, so for consistency, don't do
it in tests either.

The motivation for this change is that I'm trying to whittle down the
number of uses of the `name_to_crate_name` function, as it doesn't
take a possible `crate_name` attribute into account. For context, see
also the changes and discussion in

bazelbuild#645
martinboehme added a commit to martinboehme/rules_rust that referenced this pull request Mar 31, 2021
Normal `rust_binary` targets don't do this, so for consistency, don't do
it in tests either.

An additional motivation for this change is that I'm trying to whittle down
the number of uses of the `name_to_crate_name` function, as it doesn't
take a possible `crate_name` attribute into account. For context, see
also the changes and discussion in

bazelbuild#645
illicitonion pushed a commit that referenced this pull request Mar 31, 2021
Normal `rust_binary` targets don't do this, so for consistency, don't do it in tests either.

An additional motivation for this change is that I'm trying to whittle down the number of uses of the `name_to_crate_name` function, as it doesn't take a possible `crate_name` attribute into account. For context, see also the changes and discussion in

#645
yagehu pushed a commit to yagehu/rules_rust that referenced this pull request Apr 23, 2021
* Move assert_argv_* helpers to a common helper library.

* Add a crate_name attribute to Rust rules.

This allows specifying a different crate name than the one derived by
default from the Bazel target name.

This is useful, for example, if you want to use a target name that would
not be legal as a crate name, e.g. a target name containing slashes.

This commit introduces a function _crate_name() and uses it where we
were formerly determining the crate name using
name_to_crate_name(ctx.label.name).

There were some additional uses of name_to_crate_name in the test rule
where we weren't actually determining a crate name but an output file
name for the test binary or the test launcher. It's not clear to me why
these needed the hyphen-to-underscore conversion that name_to_crate_name
performs, as hyphens are legal in file names. I think these places
should be doing one of the following:

- Create a file name based on the target name, without
  hyphen-to-underscore substitutions.
- Create a file name based on the (possibly non-default) crate name.

I think one of these two changes should be made, but I don't want to do
so in the commit because either of these changes would create a change
in behavior for existing uses of the rust_test rule. Also, the issue of
test binary and test lancher file names is largely orthogonal to the
purpose of this commit.

Finally, there is also a use of name_to_crate_name in
cargo_build_script.bzl to replace hyphens in feature names with
underscores. This has nothing to do with crate names; it's just using
an existing function that conveniently happens to be doing the thing the
code needs. I think this use of name_to_crate_name should be replaced
with a local function that does the same thing, but again, I'll do so in
a separate commit.

* Use single-argument version of fail() for backwards compatibility.

* Emit binary directly with desired name instead of copying.

This fixes test failures on Windows that I was getting for

bazelbuild#645

where the linker was complaining that it couldn't write to the output file.
I assume that the test environment is preventing writes to anything other than
the declared output files.

* In crate_name test, test that invalid characters are reported correctly.

* Simplify _crate_name() function so it only takes the attributes.

The label isn't required, because we can also get the name from the
attributes.

* Add a TODO to respect crate_name attr in CARGO_CRATE_NAME env var.
yagehu pushed a commit to yagehu/rules_rust that referenced this pull request Apr 23, 2021
Normal `rust_binary` targets don't do this, so for consistency, don't do it in tests either.

An additional motivation for this change is that I'm trying to whittle down the number of uses of the `name_to_crate_name` function, as it doesn't take a possible `crate_name` attribute into account. For context, see also the changes and discussion in

bazelbuild#645
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants