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

dotnet-format: accept a list of files to format (instead of a full workspace) #9

Closed
BhaaLseN opened this issue Feb 20, 2019 · 9 comments
Labels
Feature Request This issue is requesting an enhancement or new feature Good First Issue This issue is good for newcomers

Comments

@BhaaLseN
Copy link

Version Used: dotnet-format 3.0.0-beta4-19105-10 (70a2f514eb3932648f771b086834cb0a376fee20)

Steps to Reproduce:

  1. Change a number of files (so they appear in VCS output, such as git diff --name-only)
  2. Pass that file list as command line parameters into dotnet format (or try to pipe them into stdin)

Expected Behavior:
dotnet format only changes the passed files instead of the whole project (or solution)

Actual Behavior:
dotnet format errors out with either "Both a MSBuild project file and solution file found in $path. Specify which one to use with the --workspace option" or "Unrecognized command or argument 'path/to/file.cs'". Or somewhat...not sure if I should call it worse or just different: it runs successfully, formatting all files referenced by the project or solution (in case only a single project or solution file is found in the current working directory).

Intended Use-Case:
We run a few things as part of our CI (such as static analysis), and we'd also like to use dotnet format as a way to enforce code style before it even reaches the first review by a human. Right now this isn't too feasible as dotnet format takes quite a while on larger solutions with a lot of files; and running it only on the changed files would significantly improve the time to feedback (with the intention to simply error out in case it produces any changes in the CI working copy).

For smaller change sets, passing file names as command line parameter is likely a usable solution, but the larger the change set the more likely it would be to reach a length limit for arguments; so piping it into stdin would probably be the better approach there.

Or, alternatively (if it makes sense to do so), support this use case from within dotnet format itself (or a tool derived from it), along with the --dry-run switch: stop processing as soon as a change would have been made and return an error code that can be checked by a caller.

Thoughts and ideas are more than welcome, especially in case I missed some other tooling that already does this.

@jmarolf
Copy link
Contributor

jmarolf commented Feb 21, 2019

@JoeRobich or @jinujoseph can you move this to https://github.com/dotnet/format?

@jinujoseph jinujoseph transferred this issue from dotnet/roslyn Feb 21, 2019
@BhaaLseN
Copy link
Author

Oh, nice, didn't know there was a repository dedicated to dotnet format; created the issue on roslyn since it had other related issues.
Thanks for moving it 👍

@jmarolf
Copy link
Contributor

jmarolf commented Feb 21, 2019

@BhaaLseN you did the right thing. I just put this repo together recently

@jmarolf jmarolf added Feature Request This issue is requesting an enhancement or new feature Good First Issue This issue is good for newcomers labels Mar 1, 2019
@ghost
Copy link

ghost commented Mar 13, 2019

@jmarolf @JoeRobich I was thinking of looking into this, as it's something I could use too. Is the way it's written in the above intended use case the way to move forward? Is a --files option necessary or would providing paths as arguments to the command be enough?

@BhaaLseN
Copy link
Author

Personally, my gut tells me dotnet format Program.cs should just format Program.cs without requiring a parameter. dotnet format Sample.csproj doesn't work and requires the use of -w/--workspace, so the parameter-less invocation seems free for grabs (unless this is a limitation of invoking commands using dotnet).
I'm fine with either though; but I'm not sure if passing filenames as command line will ever run into issues on any of the supported OS (due to length restrictions or whatever).

@JoeRobich
Copy link
Member

@BhaaLseN When thinking long-term, I agree with you. Currently we get some benefits from using the MSBuildWorkspace to enumerate projects and documents as well as saving changes, which requires a project or solution file.

@challen0 I think the easiest path to get this in would be to:

  • Require that a workspace (a single project or solution file) be discoverable in the current directory or specified with the -w flag (as currently is the case)
  • Add a --files flag that accepts a comma separated list of file paths
  • Globs are not currently supported
  • When processing documents in a project, if --files were specified, check that the document path ends with one of the file paths before formatting.

@ghost ghost mentioned this issue Mar 14, 2019
@JoeRobich
Copy link
Member

This is available on myget version 3.0.4-prerelease.19171.3 and later

dotnet tool install -g dotnet-format --version 3.0.4-prerelease.19171.3 --add-source https://dotnet.myget.org/F/format/api/v3/index.json

@am11
Copy link
Member

am11 commented Oct 11, 2019

Pass that file list as command line parameters into dotnet format (or try to pipe them into stdin)

In order to format list of files that I have just committed, I am using the following command before pushing my changes upstream:

dotnet-format --files $(git show --name-only --pretty="" | tr '\n' ',')

(followed by git add . && git commit --amend)

It would be great if we can pipe the files one by one:

git show --name-only --pretty="" | dotnet-format --files

or simply

git show --name-only --pretty="" | dotnet-format

and/or even: git show --name-only --pretty="" | dotnet-format - (minus sign in the end implying read-from-stdin)

i.e. without needing to join the list as comma separated and read path from stdin.

@sharwell
Copy link
Member

@am11 can you file a new issue for that request? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request This issue is requesting an enhancement or new feature Good First Issue This issue is good for newcomers
Projects
None yet
Development

No branches or pull requests

5 participants