Skip to content

Add ignore flag to be used with workspace option#118

Open
Hoziax wants to merge 4 commits intoDevinR528:mainfrom
Hoziax:main
Open

Add ignore flag to be used with workspace option#118
Hoziax wants to merge 4 commits intoDevinR528:mainfrom
Hoziax:main

Conversation

@Hoziax
Copy link

@Hoziax Hoziax commented Nov 4, 2025

Sometimes it can be useful to exclude certain modules/Cargo.toml files when running cargo sort on a complete workspace. For example if you use a workspace-hack module.

This pull requests adds a ignore flag (-i/--ignore) that can be used in combination with the workspace flag ( -w/ --workspace) that lets a user specify modules that should be skipped when running cargo sort. Example usage:

cargo sort -w -i "workspace-hack" my_project

The ignore flag also supports a list of values and the ? and * glob patterns:

cargo sort -w -i "workspace-hack","ignore*" my_project

@thomaseizinger
Copy link
Collaborator

I am not opposed to adding this although I do wonder why you want to skip it. There isn't any harm in sorting that file too, is there? What is the motivation as to why it should be skipped?

@Hoziax
Copy link
Author

Hoziax commented Nov 11, 2025

The original idea/problem as to why I opened this pull request was the following. We have a CI pipeline that uses hakari to speed up builds. The idea now is to add cargo sort to that. However (our) hakari expects a very specific order for the workspace-hack module. Not doing anything would result in the two passing the workspace-hack/Cargo.toml between each other and both sorting the same file differently.

So as you mentioned one option would be to define an explicit order that sorts all Cargo.tomls in such a way that everyone is happy, but then do not necessarily want one auto-generated module to dictate how every other Toml should be sorted (which would be 80+ in our case). By skipping the workspace-hack one we could keep the others that people in my Team actually use, in the format/order that we would prefer and just leave the one auto generated one out.

fn parse_workspace_member(member: &str, dir: &str) -> IoResult<Vec<String>> {
let base_path = format!("{dir}/{member}");
if member.contains('*') || member.contains('?') {
let parsed_members = glob::glob(&base_path)?
Copy link
Collaborator

@jplatte jplatte Nov 11, 2025

Choose a reason for hiding this comment

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

This hits the filesystem for each member passed on the command line, right? Isn't all we have to do conceptually wildcard matching? The wildmatch crate offers that, though without ** support – which I think is okay to not have here?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is not easy to see from the commit history but this code mostly got moved and it was already this way before.

@thomaseizinger
Copy link
Collaborator

Thank you for explaining your usecase. That makes sense to me, tools fighting each other is no fun :)

I'll give this a review in the upcoming days.

Comment on lines +120 to +121
If your workspace contains a Cargo.toml that should not be sorted, for example if it has been automatically generated, it can
be ignored with the ignore flag:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file doesn't use hard-wraps from what I can see.

Suggested change
If your workspace contains a Cargo.toml that should not be sorted, for example if it has been automatically generated, it can
be ignored with the ignore flag:
If your workspace contains a Cargo.toml that should not be sorted, for example if it has been automatically generated, it can be ignored with the ignore flag:

/// workspace section or that are specified in the `ignore` parameter will be removed.
/// Returns an error if [`parse_workspace_member`] failed to parse a glob pattern in any workspace
/// member.
fn parse_and_filter_workspace_members(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please define (utility) functions below their usage (roughly in order of how they are used).

fn parse_workspace_member(member: &str, dir: &str) -> IoResult<Vec<String>> {
let base_path = format!("{dir}/{member}");
if member.contains('*') || member.contains('?') {
let parsed_members = glob::glob(&base_path)?
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is not easy to see from the commit history but this code mostly got moved and it was already this way before.

if let Some(Item::Table(ws)) = workspace {
// The workspace excludes, used to filter members by
let excludes: Vec<&str> =
ws.get("exclude").map_or_else(Vec::new, array_string_members);
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Hoziax Can't we just append our values from ignore to this vector? Why did we have to change so much of the code to support the feature? It makes it hard to review as it is not clear what is old and what is new code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants