-
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
fix WorkspaceSource's issues with rename and default-features #10697
Conversation
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.
My first thought with this is: is this important enough that we should be focusing on this right now?
if let Source::Workspace(_) = source { | ||
self.default_features = None; | ||
self.rename = None; | ||
} | ||
self.source = Some(source.into()); |
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 .into()
isn't needed
let source = source.into(); | ||
if let Source::Workspace(_) = source { | ||
self.default_features = None; | ||
self.rename = None; |
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 clears rename
which means that toml_key
will change when setting the source.
Is that what we should expect?
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 debated heavily between clearing the fields or using bail!
. I decided against clearing the fields as if you want to set a new source the idea is that you do not care about those fields. I can see how a CargoResult<Self>
could be better and that should be an easy change to make if you feel it is better
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 left out another options: move rename
into name
so that the dependency table is as if the package
field was dropped. I was wondering about that vs what is here
if self.source != Some(Source::Workspace(WorkspaceSource)) { | ||
key.fmt(); | ||
} |
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 in the same commit as everything else without an explanation
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.
There was a problem with formatting after removal and this fixed it. Do you think it would be better in a separate commit or described in the PR?
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.
Separate PR would mean its not blocked on everything else
@@ -1047,12 +1094,12 @@ mod tests { | |||
} | |||
|
|||
#[test] | |||
fn to_toml_renamed_dep() { | |||
fn to_toml_renamed_dep() -> CargoResult<()> { |
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 does it take for this to show a stack trace? If the experience is sub-par wrt backtraces, I'd just unwrap
instead
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'm not entirely sure what it would take, so I will change it to unwrap
@@ -565,7 +566,7 @@ fn populate_dependency(mut dependency: Dependency, arg: &DepOp) -> Dependency { | |||
} | |||
|
|||
if let Some(rename) = &arg.rename { | |||
dependency = dependency.set_rename(rename); | |||
dependency = dependency.set_rename(rename).unwrap(); |
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.
If you have a reason to believe an unwrap
is justified, it should be documented, ideally as an except
. Otherwise, chain the error
@@ -284,6 +284,7 @@ fn resolve_dependency( | |||
// Checking for a workspace dependency happens first since a member could be specified | |||
// in the workspace dependencies table as a dependency | |||
if let Some(_dep) = find_workspace_dep(dependency.toml_key(), ws.root_manifest()).ok() { | |||
check_invalid_ws_keys(dependency.toml_key(), arg)?; |
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 is the reason this was added? This is done in the same commit as everything else but its not clear how its related?
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 missed adding this to the PR, if this was not here it changes the error message. It prints the error message according to the toml_key
which changes in the next line (potentially)
if let Some(inherit_features) = &self.inherited_features { | ||
inherit_features.iter().for_each(|f| { | ||
features.remove(f.as_str()); | ||
}); | ||
} | ||
if !features.is_empty() { | ||
let features = | ||
toml_edit::value(features.into_iter().collect::<toml_edit::Value>()); | ||
table.set_dotted(false); | ||
table.insert("features", features); | ||
} else { | ||
table.remove("features"); | ||
} |
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.
Not quite convinced this is the right route to go. inherit_features
is just advisory before this change
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 my bad, I accidentally added this. It should've been moved to my shelf
I could go either way on the importance of this right now. I think it is a change that should be made at some point but it is not needed right at this moment. If it should be closed until a better time just let me know! |
For me, the issue is this jumps straight to solving it one way when there are multiple ways without any discussion of the trade offs. Having the discussion on which way to go was what I didn't feel was important enough to have right now. |
That works for me, I'll close this and open a PR for the key formatting issue! |
fix key formatting when switching to a dotted `WorkspaceSource` This fell out of #10697 see [this comment](#10697 (comment)) There was a small issue where changing the source of a `cargo_add::Dependency` to a `WorkspaceSource` would cause the dotted version to have extra space. ```toml dep .workspace = true dep.workspace = true ``` This makes sure the key is formatted as well as adds a unit test to make sure this doesn't come back up in the future. r? `@epage`
Motivations and Context
As talked about in #10685, there were some issues with the
cargo_add::Dependency
methods. They revolved around setting a source to aWorkspaceSource
from anotherSource
. They would leave remnants of the other source that conflicts with aWorkspaceSource
(rename
anddefault-features
). This fixes that issue by making sure you cannot setrename
ordefault-features
if the source is aWorkspaceSource
. Additionally, it clears the conflicting fields in aDependency
so they cannot show up even if you switched theSource
.Changes
set_rename
andset_default_features
return aCargoResult
WorkspaceSource
set_source
clear conflicting fields when called with aWorkspaceSource
r? @epage