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

cargo-fmt in (virtual) workspace member directories fails #2694

Closed
CAD97 opened this issue May 9, 2018 · 7 comments · Fixed by #3652
Closed

cargo-fmt in (virtual) workspace member directories fails #2694

CAD97 opened this issue May 9, 2018 · 7 comments · Fixed by #3652
Labels
bug Panic, non-idempotency, invalid code, etc. os-windows

Comments

@CAD97
Copy link
Contributor

CAD97 commented May 9, 2018

/Cargo.toml

[workspace]
members = ["member"]

/member/Cargo.toml

[package]
name = "member"
version = "0.0.0"

/member/src/lib.rs

#[cfg(test)]
mod tests                                                                                   {
    #[test]
    fn it_works()                                                                           {
        assert_eq!(2 + 2, 4)                                                              ;}}

(I'm sorry)

Run

member>cargo check
   Compiling member v0.0.0 (file:///D:/TEMP/member)
    Finished dev [unoptimized + debuginfo] target(s) in 0.18 secs

member>cargo fmt
Failed to find targets
usage: cargo fmt [options]

Options:
    -h, --help          show this message
    -q, --quiet         no output printed to stdout
    -v, --verbose       use verbose output
    -p, --package <package>
                        specify package to format (only usable in workspaces)
        --version       print rustfmt version and exit
        --all           format all packages (only usable in workspaces)

This utility formats all bin and lib files of the current crate using rustfmt.
Arguments after `--` are passed to rustfmt.

member>type src\lib.rs
#[cfg(test)]
mod tests                                                                                   {
    #[test]
    fn it_works()                                                                           {
        assert_eq!(2 + 2, 4)                                                              ;}}

member>cargo fmt --all

member>type src\lib.rs
#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}
>cargo fmt --version
rustfmt 0.6.1-nightly (5dba81bb 2018-05-09)
@nrc nrc added the bug Panic, non-idempotency, invalid code, etc. label May 10, 2018
@Stargateur
Copy link

That still happen, quite annoying.

@jakoschiko
Copy link

$ cargo fmt -p member doesn't work either.

$ cargo fmt --package member works.

$ cargo fmt -pmember works, too. See #1244.

$ cargo fmt --version
rustfmt 1.0.0-stable (43206f41 2018-11-30)

@chris-morgan
Copy link
Member

chris-morgan commented Jan 19, 2019

C:\workspace\member>cargo fmt
Failed to find targets
[usage]

C:\workspace\member>cargo fmt -p member
Invalid argument: `member`.
[usage]

C:\workspace\member>cargo fmt --package member
package `member` is not a member of the workspace
[usage]

C:\workspace\member>cargo fmt -pmember
package `member` is not a member of the workspace
[usage]

I think we have two problems here:

  1. -p member doesn’t work, since -p is requiring the absence of a space after it.
  2. cargo fmt of individual packages in a workspace is just completely broken—on Windows only, I presume, given that @CAD97 and I are both having it fail on Windows, and there has been no hue and cry such as I’m confident would occur if it were broken everywhere.

I’m using rustfmt 1.0.1-nightly (be13559 2018-12-10), the build that comes from the rustfmt component on the nightly toolchain.

(It is entirely coincidental that I am posting an hour after @jakoschiko; I just attempted to run cargo fmt inside a newly created workspace for the first time.)

@sphynx
Copy link
Contributor

sphynx commented May 22, 2019

@chris-morgan @jakoschiko @Stargateur Can someone on Windows please check whether it is still a problem, after PR #3569 has been merged?

@jakoschiko
Copy link

D:\Code\rust\workspace\member>cargo check
    Checking member v0.0.0 (D:\Code\rust\workspace\member)
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s

D:\Code\rust\workspace\member>cargo fmt --version
rustfmt 1.2.2-nightly (27871389 2019-05-22)

D:\Code\rust\workspace\member>cargo fmt
Failed to find targets
cargo-fmt 1.2.2
This utility formats all bin and lib files of the current crate using rustfmt.

USAGE:
    cargo fmt [FLAGS] [OPTIONS] [-- <rustfmt_options>...]

FLAGS:
        --all        Format all packages (only usable in workspaces)
    -q, --quiet      No output printed to stdout
    -v, --verbose    Use verbose output
        --version    Print rustfmt version and exit

OPTIONS:
    -p, --package <package>...    Specify package to format (only usable in workspaces)

ARGS:
    <rustfmt_options>...    Options passed to rustfmt

@sphynx
Copy link
Contributor

sphynx commented May 23, 2019

@jakoschiko Ok, thank you!

It looks like this problem with Failed to find targets is not directly related to command line arguments parsing. I don't have a way to debug it (I am on Mac), so I am just looking at the code :)

This error comes from get_targets function. Targets are produced by CargoFmtStrategy which has three cases. By default, when no arguments are provided to cargo fmt – it's CargoFmtStrategy::Root, so the problem must be in get_targets_root_only function.

The only suspicious thing I see there is that manifest_path is not canonicalized in this line:

if in_workspace_root || PathBuf::from(&package.manifest_path) == current_dir_manifest {

Can it be that? Can we canonicalize it and check if it helps? Or at least print some debug info in that function? Thank you!

@jakoschiko
Copy link

@sphynx
I already tried it, see #3309 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. os-windows
Projects
None yet
7 participants