Rustfmt: cargo-fmt in (virtual) workspace member directories fails

Created on 10 May 2018  Â·  7Comments  Â·  Source: rust-lang/rustfmt

/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)
bug os-windows

Most helpful comment

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 (be135599 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.)

All 7 comments

That still happen, quite annoying.

$ 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)
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 (be135599 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.)

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

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

@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!

@sphynx
I already tried it, see https://github.com/rust-lang/rustfmt/pull/3309#issuecomment-459907591.

Was this page helpful?
0 / 5 - 0 ratings