-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Ability to specify the output name for a bin target different from the crate name #9627
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Eh2406 (or someone else) soon. Please see the contribution instructions for more information. |
Thanks for the PR, and looks reasonable so far! I think we'll also want documentation updates for Cargo as well along the lines of this, and we'll probably also want this to be gated and unstable to start out, but otherwise it's a good start :) |
Hi @alexcrichton, I have also tried the second approach here: It is much more elegant, I think. |
Hi @alexcrichton / @Eh2406, |
I don't know this part of the code all that well. Sorry. @eric Huss may have more opinions. |
I think the approach of adding to TargetInner looks correct here. There are a few other considerations that should be handled:
One way to get these to work is correctly add the new path to the output tracking. The way Cargo predicts what the output filenames are is kinda complicated, but I'll try to walk through it:
Somewhere in that, it should compute the |
} | ||
} | ||
|
||
/// The filename for this FileType that Cargo should use when "uplifting" | ||
/// it to the destination directory. | ||
pub fn uplift_filename(&self, target: &Target) -> String { | ||
// for binary crate type, `should_replace_hyphens` will always be false | ||
let name = if self.should_replace_hyphens { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is my assumption that should_replace_hyphens
will always be false
for binary crates, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, though this seems a little subtle. Can you test on Windows to see the behavior with PDB files works correctly in the visual studio debugger? There is a brief explanation here as to how that works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would agree that this is a bit subtle and I think it'd be best if this were handled a bit differently where there was an early-return or similar
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give me a little more background and clues on this? TBH, I'm not a Rust expert (yet). Could you elaborate what you mean by early return ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to make it clearer, this could check if get_binary_name
returns a value, and if it does, use that, then check should_replace_hyphens
, and then the final else
would return target.name()
. That should make it more explicit.
Hi @ehuss , |
Hi, I think we can start implementing more tests, now. One for |
} | ||
} | ||
|
||
/// The filename for this FileType that Cargo should use when "uplifting" | ||
/// it to the destination directory. | ||
pub fn uplift_filename(&self, target: &Target) -> String { | ||
// for binary crate type, `should_replace_hyphens` will always be false | ||
let name = if self.should_replace_hyphens { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so, though this seems a little subtle. Can you test on Windows to see the behavior with PDB files works correctly in the visual studio debugger? There is a brief explanation here as to how that works.
Hi @ehuss, can we try running the CI again? |
@ehuss Great! That seems to have taken care of that issue. Since, the windows CI also passed, can we assume that this works on Windows, too? I think, now we can proceed with writing more tests, if needed. @alexcrichton also mentioned feature-gating and some extra documentation... |
@@ -662,6 +667,7 @@ impl Target { | |||
|
|||
pub fn bin_target( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is something that we'll want to handle for at least the cdylib output as well, perhaps the staticlib output too. Overall I think that the filename
handling from the TOML profile may need to happen at a lower layer than as a new method on constructors here (as otherwise it would need to be added as an argument to all constructors)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to re-use the name
field in the same struct, instead of creating a new field as I have done here. But, @ehuss, suggested not to do that as we don't know what kind of implications that might have.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what Alex is referring to here is that it could be generalized for all the target types, and that could be handled in some place like configure
. I don't feel strongly about supporting that in the initial PR, though it would be good to keep that in mind.
} | ||
} | ||
|
||
/// The filename for this FileType that Cargo should use when "uplifting" | ||
/// it to the destination directory. | ||
pub fn uplift_filename(&self, target: &Target) -> String { | ||
// for binary crate type, `should_replace_hyphens` will always be false | ||
let name = if self.should_replace_hyphens { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would agree that this is a bit subtle and I think it'd be best if this were handled a bit differently where there was an early-return or similar
target.name().to_string() | ||
target | ||
.get_binary_name() | ||
.unwrap_or_else(|| target.name().to_string()) | ||
}; | ||
format!("{}{}{}", self.prefix, name, self.suffix) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like it's automatically appending a prefix/suffix such as lib
, .a
, and .exe
I think. If that's the case I'm not sure that filename
is the right name for the key in the manifest since it's only influencing part of the filename, not the whole filename.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about something like binary_name
instead of filename
? would that be more suitable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed some ideas for the name, but none seemed particularly good. file_stem
is a strong option, but it may not work well with libraries. Another option is to allow name
to support arbitrary values, but if it is a non-identifier, require a second field of crate-name
.
We didn't feel strongly about any of the options we thought of, so I think leaving this as filename
for now is fine, and we can maybe consider other options later.
8e95324
to
65194e6
Compare
Hi @ehuss / @alexcrichton , how do you want me to proceed with this, now? |
Reviewers like @ehuss and I have time to help out and provide some guidance in places where necessary, but I don't think either of us (or others) have the time to do 1 on 1 mentoring right now with detailed guidanced and detailed information about how to solve all concerns. Unfortunately that can amount to the reviewer doing most of the work of design and largely implementation, which can be very time consuming. In some sense @whereistejas you'll need to take some liberties in taking feedback on this PR and incorporating the features and requests into the implementation yourself. If you have general questions about Cargo's or need assistance we can try to help, but I don't think either of us is in a position to provide the detailed level of advice you may be looking for on this. |
Hi @alexcrichton, thank you for the candid response. Given, the small team managing this project, it is perfectly understandable, that you can dedicate only a limited amount of time, to helping out people with their PR. No hard feelings (I mean it). The point of doing open-source contributions is to help out maintainers such as yourself save time, not further eat into it. We are all on the same team here. :) I will try to take this PR as close to the Finished state as I can, before asking for a review again. I'm almost finished, here, as I have already finished feature gating and adding the documentation. I just want to add a few more tests. |
Hi @alexcrichton / @ehuss , Tasks:
This is a short description of my implementation:
I have currently added three tests:
I would like your input on deciding what the parameter, we use in
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a few more tests?
- Check that it is feature-gated. That is, try to run
cargo build
with a manifest wherecargo-features
is not set, and make sure it fails. Look for tests calledgated()
for some examples. - Check the JSON output for the artifact message in
--message-format=json
contains the correct path. - Check that fingerprinting works correctly. You can stuff this in one of the other tests, like
simple_binary_name
where after the first build, just runcargo build -v
again and make sure the output says[FRESH] foo [..]
. - Check that
cargo run
is able to execute the binary. - Check that
cargo test
is able to test the binary. I think as written, it still uses the crate name in the filename? - Check that the
CARGO_BIN_EXE_$name
environment variable is set correctly for integration tests. - Check the
CARGO_BIN_NAME
environment variable. I'm uncertain what value this should have. I think based on the original use cases, this might want to be the new filename. I'm a little concerned about the increasing inconsistencies this is introducing though (like the--bin
flag uses the bin name, not the filename). - Add some tests for
cargo install
andcargo uninstall
. Make sure it installs the correct name and is able to uninstall it.
} | ||
} | ||
|
||
/// The filename for this FileType that Cargo should use when "uplifting" | ||
/// it to the destination directory. | ||
pub fn uplift_filename(&self, target: &Target) -> String { | ||
// for binary crate type, `should_replace_hyphens` will always be false | ||
let name = if self.should_replace_hyphens { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to make it clearer, this could check if get_binary_name
returns a value, and if it does, use that, then check should_replace_hyphens
, and then the final else
would return target.name()
. That should make it more explicit.
target.name().to_string() | ||
target | ||
.get_binary_name() | ||
.unwrap_or_else(|| target.name().to_string()) | ||
}; | ||
format!("{}{}{}", self.prefix, name, self.suffix) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed some ideas for the name, but none seemed particularly good. file_stem
is a strong option, but it may not work well with libraries. Another option is to allow name
to support arbitrary values, but if it is a non-identifier, require a second field of crate-name
.
We didn't feel strongly about any of the options we thought of, so I think leaving this as filename
for now is fine, and we can maybe consider other options later.
.unstable_features() | ||
.require(Feature::different_binary_name())?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of an unusual place to put the unstable check, as this is pretty low-level. Usually the unstable checks are placed towards the front end if possible, such as in toml/mod.rs
or toml/targets.rs
or Manifest::feature_gate
. I think toml/targets.rs
would probably be a good place.
@@ -662,6 +667,7 @@ impl Target { | |||
|
|||
pub fn bin_target( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think what Alex is referring to here is that it could be generalized for all the target types, and that could be handled in some place like configure
. I don't feel strongly about supporting that in the initial PR, though it would be good to keep that in mind.
tests/testsuite/binary_name.rs
Outdated
#[cargo_test] | ||
fn check_deps_content() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this test be merged with simple_binary_name
? Each test has a small cost, and we don't want to add them too extravagantly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, basically, I should create new tests only if the content of cargo.toml
changes. Is that correct? We want to perform as many checks as we can in the same test method (within good reason) as long as all of them can use the same cargo.toml
content.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I thought I responded to this, but I think my comment got eaten. There aren't any specific rules. But yea, when different steps of a test have the same Cargo.toml
, I usually place them together in the same test. There is a balance between having too many individual tests (which slows things down), and having too many steps in a single test (which can make the test hard to work on and debug). Different people will have different opinions about exactly how to structure things, and it is mostly just a judgement call.
Just maintaining this list here, for my tracking. Will remove once completed.
|
Hi @ehuss,
Writing the test and fix checking From what I can tell, it is kind of tricky to assign the value we get from diff --git a/src/cargo/core/compiler/mod.rs b/src/cargo/core/compiler/mod.rs
index 1687a5d2a..6501231c9 100644
--- a/src/cargo/core/compiler/mod.rs
+++ b/src/cargo/core/compiler/mod.rs
@@ -980,7 +980,11 @@ fn build_base_args(
let exe_path = cx
.files()
.bin_link_for_target(bin_target, unit.kind, cx.bcx)?;
- let key = format!("CARGO_BIN_EXE_{}", bin_target.name());
+ let name = unit
+ .target
+ .get_binary_name()
+ .unwrap_or(bin_target.name().to_string());
+ let key = format!("CARGO_BIN_EXE_{}", name);
cmd.env(&key, exe_path);
}
} This code is called once for each unit as we are looping over each Would it be problematic if we had One possible alternative, is to use add the |
Just chiming in to say that supporting Cheers |
src/doc/src/reference/unstable.md
Outdated
restrictions placed on crate names. For example, the crate name must use only `alphanumeric` characters | ||
or `-` or `_`, and cannot be empty. | ||
|
||
The `filname` parameter should **not** include the binary extension, `cargo` will figure out the appropriate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The `filname` parameter should **not** include the binary extension, `cargo` will figure out the appropriate | |
The `filename` parameter should **not** include the binary extension, `cargo` will figure out the appropriate |
Sorry, I'm a bit confused about the questions for CARGO_BIN_EXE. Wouldn't the key name be something like Also, looking at the usage of |
@ehuss this is right! Thanks for this, it helped me out a bunch. I have covered all the tests, now. They are all passing. I had some trouble figuring out how to handle windows vs. unix paths, thankfully I saw the The only test I'm a little sceptical about is this: |
882a1d2
to
4f9737b
Compare
☔ The latest upstream changes (presumably #9118) made this pull request unmergeable. Please resolve the merge conflicts. |
Hi @ehuss, I have fixed all the issues you had mentioned in your comments. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, just a few small updates and I think this should be good to go. Can you also squash all the commits?
0fbc30d
to
55de0a0
Compare
Okay, so I tried to I recovered from this mishap, thank God. |
1a93142
to
2d92a9b
Compare
No worries. Git can be finicky sometimes. Thanks! 🎉 @bors r+ |
📌 Commit 9e2790f has been approved by |
☀️ Test successful - checks-actions |
Thanks @ehuss for all of your help! I really appreciate the patience from your side. I just have one last question, what will we have to do make this feature a |
There is not a well established protocol for stabilizing. Generally, features land on nightly for a while, and people should test it out and report their experiences. Preferably we would like to see that a feature has at least some degree of broad interest, and isn't being added for just one project. The unresolved issues listed in #9778 should either get resolved or have some good explanation of why they don't need to be resolved. |
Update cargo 8 commits in b51439fd8b505d4800a257acfecf3c69f81e35cf..e96bdb0c3d0a418e7fcd7fbd69be08abf830b4bc 2021-08-09 18:40:05 +0000 to 2021-08-17 22:58:47 +0000 - Support using rustbot to ping the Windows group (rust-lang/cargo#9802) - Show information about abnormal `fix` errors. (rust-lang/cargo#9799) - Bump jobserver. (rust-lang/cargo#9798) - Render build-std web links as hyperlinks (rust-lang/cargo#9795) - Teach cargo to failfast on recursive/corecursive aliases (rust-lang/cargo#9791) - Fix value-after-table error with profiles. (rust-lang/cargo#9789) - Fix plugin registrar change. (rust-lang/cargo#9790) - Ability to specify the output name for a bin target different from the crate name (rust-lang/cargo#9627)
Hi,
I have opened this to close the following issue #1706 .
I have decided to start by writing a test to outline what behavior is expected from
Cargo
.As of now, this test fails (for obvious reasons). I will now start writing the code needed to pass this test.
This is my first time contributing to cargo. Please, feel free to let me know if there are any protocols/processes that need to be followed. I'm a newbie at this.
Closes issue #1706
Tracking issue #9778