colcon-cargo icon indicating copy to clipboard operation
colcon-cargo copied to clipboard

Provide mechanism to opt out of `cargo fmt`

Open cottsay opened this issue 10 months ago • 1 comments

It isn't currently possible to pass colcon test on a cargo package which doesn't conform to cargo fmt. Although I certainly think that declaring and aligning with a standard code formatter is probably a good idea, I don't think it should be something we should force package developers to do.

In other colcon packages we optionally add extra functionality during the test job by looking at the dependencies the package has declared. For example, if a package has a dependency on pytest-cov, we automatically enable code coverage. Unfortunately I don't see a way to do that here. I thought that maybe we could add rustfmt to the dev-dependencies if we wanted to enable the format check, but as I understand it, the crate dependencies are exclusively for libraries so it isn't appropriate (or possible?) to use dev-dependencies for this.

The best I can come up with is that we should only implicitly run cargo fmt if we find a rustfmt.toml or .rustfmt.toml in any directories up the tree, following the behavior of rustfmt itself for configuration discovery. If a developer wants to implicitly enable cargo fmt for all packages in their workspace, they can place an empty rustfmt config file in their workspace.

Hoping for feedback from rust developers on this.

cottsay avatar Feb 24 '25 21:02 cottsay

One option could be to take advantage of the [package.metadata] section of Cargo.toml.

E.g. we could have

[package.metadata.colcon]
rustfmt = true

to tell colcon that this package wants its format to be tested.

I think there are probably a substantial number of packages that would want their format tested but don't contain a .rustfmt.toml.

mxgrey avatar Feb 25 '25 09:02 mxgrey