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

How to format code without a solution / csproj file in the latest (6.0+) version #1317

Closed
mikeage opened this issue Sep 2, 2021 · 8 comments
Labels
Question This issue is requesting information about dotnet-format

Comments

@mikeage
Copy link

mikeage commented Sep 2, 2021

Hi,

I recently updated from v5.0.211103 to the tip, and it seems like --folder is no longer supported. Whether I run dotnet format (6.0.100-preview.7.21379.14) or dotnet-format (7.0.245108+2c6ded76878d585a4f037c32ba0fbb2685f27b05), it doesn't recognize the --folder command, and without it, it complains about a missing workspace (I actually don't have an .sln or .csproj file at all; this is a Unity project).

What is the proper syntax in the latest versions?

@mikeage
Copy link
Author

mikeage commented Sep 2, 2021

To be clear, running manually, I get the following:

$ dotnet format --folder --include Assets/Editor/BrushDescriptorEditor.cs
Cannot specify the '--folder' option when fixing style.
Cannot specify the '--folder' option when running analyzers.

dotnet-format
  dotnet-format

Usage:
  dotnet-format [options] [<workspace>]

Arguments:
  <workspace>  A path to a solution file, a project file, or a folder containing a solution or project file. If a path is not specified then the current directory is used. [default: ]

Options:
  --no-restore                                                             Doesn't execute an implicit restore before formatting.
  -f, --folder                                                             Whether to treat the `<workspace>` argument as a simple folder of files.
  -w, --fix-whitespace                                                     Run whitespace formatting. Run by default when not applying fixes.
  -s, --fix-style <error|info|warn>                                        Run code style analyzers and apply fixes.
  -a, --fix-analyzers <error|info|warn>                                    Run 3rd party analyzers and apply fixes.
  --diagnostics <diagnostics>                                              A space separated list of diagnostic ids to use as a filter when fixing code style or 3rd party issues. [default: ]
  --include <include>                                                      A list of relative file or folder paths to include in formatting. All files are formatted if empty. [default: ]
  --exclude <exclude>                                                      A list of relative file or folder paths to exclude from formatting. [default: ]
  --check                                                                  Formats files without saving changes to disk. Terminates with a non-zero exit code if any files were formatted.
  --report <report-path>                                                   Accepts a file path, which if provided, will produce a json report in the given directory.
  -v, --verbosity <d|detailed|diag|diagnostic|m|minimal|n|normal|q|quiet>  Set the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]
  --binarylog <binary-log-path>                                            Log all project or solution load information to a binary log file.
  --version                                                                Show version information
  -?, -h, --help                                                           Show help and usage information

or

$ dotnet tool install -g dotnet-format --version "7.0.*" --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json

Welcome to .NET 6.0!
---------------------
SDK Version: 6.0.100-preview.7.21379.14

Telemetry
---------
The .NET tools collect usage data in order to help us improve your experience. It is collected by Microsoft and shared with the community. You can opt-out of telemetry by setting the DOTNET_CLI_TELEMETRY_OPTOUT environment variable to '1' or 'true' using your favorite shell.

Read more about .NET CLI Tools telemetry: https://aka.ms/dotnet-cli-telemetry

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
You can invoke the tool using the following command: dotnet-format
Tool 'dotnet-format' (version '7.0.245108') was successfully installed.

$ dotnet-format --folder --include Assets/Editor/BrushDescriptorEditor.cs
Unhandled exception: System.IO.FileNotFoundException: The file '--folder' does not appear to be a valid project or solution file.
   at Microsoft.CodeAnalysis.Tools.Workspaces.MSBuildWorkspaceFinder.FindFile(String workspacePath) in /_/src/Workspaces/MSBuildWorkspaceFinder.cs:line 70
   at Microsoft.CodeAnalysis.Tools.Workspaces.MSBuildWorkspaceFinder.FindWorkspace(String searchDirectory, String workspacePath) in /_/src/Workspaces/MSBuildWorkspaceFinder.cs:line 34
   at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.ParseWorkspaceOptions(ParseResult parseResult, FormatOptions formatOptions) in /_/src/Commands/FormatCommandCommon.cs:line 297
   at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(InvocationContext context) in /_/src/Commands/RootFormatCommand.cs:line 40
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600063a+0xa5
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseParseErrorReporting>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000659+0x9d
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000653+0x85
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<<UseVersionOption>b__1>d.MoveNext() in System.CommandLine.dll:token 0x600065d+0xd3
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseTypoCorrections>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600065b+0xca
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__24_0>d.MoveNext() in System.CommandLine.dll:token 0x600064b+0xcd
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseParseDirective>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000657+0xa1
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseDebugDirective>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600064f+0x231
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext() in System.CommandLine.dll:token 0x6000649+0xf0
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000651+0x70

Here's my .pre-commit-config.yaml file:

  - repo: https://github.com/dotnet/format
    rev: "2c6ded76878d585a4f037c32ba0fbb2685f27b05"  # master as of 2021-09-02
    hooks:
      - id: dotnet-format
        exclude: ^(Assets/ThirdParty)|(Packages/)
        args:
          - --folder
          - --include

When I run in pre-commit (from a github action -- I can't run it locally in pre-commit because of #1318), I get the following logs

2021-09-02T07:03:53.4962419Z [command]/opt/hostedtoolcache/Python/3.9.6/x64/bin/pre-commit run --show-diff-on-failure --color=always --all-files
2021-09-02T07:03:53.8071988Z [INFO]�[m Initializing environment for https://github.com/adrienverge/yamllint.git.
2021-09-02T07:03:54.1066920Z [INFO]�[m Initializing environment for https://github.com/psf/black.
2021-09-02T07:03:54.5569336Z [INFO]�[m Initializing environment for https://github.com/pycqa/flake8.
2021-09-02T07:03:54.9300529Z [INFO]�[m Initializing environment for https://github.com/PyCQA/pylint.git.
2021-09-02T07:03:55.4817170Z [INFO]�[m Initializing environment for https://github.com/dotnet/format.
2021-09-02T07:03:55.9044031Z [INFO]�[m Installing environment for https://github.com/adrienverge/yamllint.git.
2021-09-02T07:03:55.9045523Z [INFO]�[m Once installed this environment will be reused.
2021-09-02T07:03:55.9046486Z [INFO]�[m This may take a few minutes...
2021-09-02T07:04:00.9097225Z [INFO]�[m Installing environment for https://github.com/psf/black.
2021-09-02T07:04:00.9098868Z [INFO]�[m Once installed this environment will be reused.
2021-09-02T07:04:00.9099854Z [INFO]�[m This may take a few minutes...
2021-09-02T07:04:08.7850625Z [INFO]�[m Installing environment for https://github.com/pycqa/flake8.
2021-09-02T07:04:08.7852173Z [INFO]�[m Once installed this environment will be reused.
2021-09-02T07:04:08.7853148Z [INFO]�[m This may take a few minutes...
2021-09-02T07:04:14.2133389Z [INFO]�[m Installing environment for https://github.com/PyCQA/pylint.git.
2021-09-02T07:04:14.2143077Z [INFO]�[m Once installed this environment will be reused.
2021-09-02T07:04:14.2144457Z [INFO]�[m This may take a few minutes...
2021-09-02T07:04:21.0680238Z [INFO]�[m Installing environment for https://github.com/dotnet/format.
2021-09-02T07:04:21.0681879Z [INFO]�[m Once installed this environment will be reused.
2021-09-02T07:04:21.0682844Z [INFO]�[m This may take a few minutes...
2021-09-02T07:04:54.5781631Z yamllint.................................................................�[42mPassed�[m
2021-09-02T07:04:56.3256976Z black....................................................................�[42mPassed�[m
2021-09-02T07:04:57.7502027Z flake8...................................................................�[42mPassed�[m
2021-09-02T07:05:07.0545348Z pylint...................................................................�[42mPassed�[m
2021-09-02T07:05:07.4547698Z dotnet format............................................................�[41mFailed�[m
2021-09-02T07:05:07.4549112Z �[2m- hook id: dotnet-format�[m
2021-09-02T07:05:07.4549890Z �[2m- exit code: 1�[m
2021-09-02T07:05:07.4550349Z 
2021-09-02T07:05:07.4552079Z Unhandled exception: System.IO.FileNotFoundException: Could not find a MSBuild project file or solution file in '/home/runner/work/open-brush/open-brush/'. Specify which to use with the <workspace> argument.
2021-09-02T07:05:07.4555322Z    at Microsoft.CodeAnalysis.Tools.Workspaces.MSBuildWorkspaceFinder.FindWorkspace(String searchDirectory, String workspacePath) in /_/src/Workspaces/MSBuildWorkspaceFinder.cs:line 57
2021-09-02T07:05:07.4559345Z    at Microsoft.CodeAnalysis.Tools.Workspaces.MSBuildWorkspaceFinder.FindWorkspace(String searchDirectory, String workspacePath) in /_/src/Workspaces/MSBuildWorkspaceFinder.cs:line 34
2021-09-02T07:05:07.4563160Z    at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.ParseWorkspaceOptions(ParseResult parseResult, FormatOptions formatOptions) in /_/src/Commands/FormatCommandCommon.cs:line 297
2021-09-02T07:05:07.4567864Z    at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(InvocationContext context) in /_/src/Commands/RootFormatCommand.cs:line 40
2021-09-02T07:05:07.4574062Z    at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600063a+0xa5
2021-09-02T07:05:07.4575807Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4577482Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseParseErrorReporting>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000659+0x9d
2021-09-02T07:05:07.4579293Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4580824Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000653+0x85
2021-09-02T07:05:07.4582499Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4584272Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<<UseVersionOption>b__1>d.MoveNext() in System.CommandLine.dll:token 0x600065d+0xd3
2021-09-02T07:05:07.4586067Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4587669Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseTypoCorrections>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600065b+0xca
2021-09-02T07:05:07.4589447Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4590991Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__24_0>d.MoveNext() in System.CommandLine.dll:token 0x600064b+0xcd
2021-09-02T07:05:07.4592957Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4594573Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseParseDirective>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000657+0xa1
2021-09-02T07:05:07.4596345Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4597961Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseDebugDirective>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600064f+0x231
2021-09-02T07:05:07.4599724Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4601354Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext() in System.CommandLine.dll:token 0x6000649+0xf0
2021-09-02T07:05:07.4603145Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4604760Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000651+0x70
2021-09-02T07:05:07.4607456Z Unhandled exception: System.IO.FileNotFoundException: Could not find a MSBuild project file or solution file in '/home/runner/work/open-brush/open-brush/'. Specify which to use with the <workspace> argument.
2021-09-02T07:05:07.4610317Z    at Microsoft.CodeAnalysis.Tools.Workspaces.MSBuildWorkspaceFinder.FindWorkspace(String searchDirectory, String workspacePath) in /_/src/Workspaces/MSBuildWorkspaceFinder.cs:line 57
2021-09-02T07:05:07.4614118Z    at Microsoft.CodeAnalysis.Tools.Workspaces.MSBuildWorkspaceFinder.FindWorkspace(String searchDirectory, String workspacePath) in /_/src/Workspaces/MSBuildWorkspaceFinder.cs:line 34
2021-09-02T07:05:07.4617758Z    at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.ParseWorkspaceOptions(ParseResult parseResult, FormatOptions formatOptions) in /_/src/Commands/FormatCommandCommon.cs:line 297
2021-09-02T07:05:07.4621942Z    at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(InvocationContext context) in /_/src/Commands/RootFormatCommand.cs:line 40
2021-09-02T07:05:07.4625527Z    at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600063a+0xa5
2021-09-02T07:05:07.4627329Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4628980Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseParseErrorReporting>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000659+0x9d
2021-09-02T07:05:07.4630797Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4632583Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000653+0x85
2021-09-02T07:05:07.4634291Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4635892Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<<UseVersionOption>b__1>d.MoveNext() in System.CommandLine.dll:token 0x600065d+0xd3
2021-09-02T07:05:07.4637632Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4639338Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseTypoCorrections>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600065b+0xca
2021-09-02T07:05:07.4641132Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4642687Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__24_0>d.MoveNext() in System.CommandLine.dll:token 0x600064b+0xcd
2021-09-02T07:05:07.4644406Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4645996Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseParseDirective>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000657+0xa1
2021-09-02T07:05:07.4647758Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4649369Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseDebugDirective>b__0>d.MoveNext() in System.CommandLine.dll:token 0x600064f+0x231
2021-09-02T07:05:07.4651122Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4652740Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext() in System.CommandLine.dll:token 0x6000649+0xf0
2021-09-02T07:05:07.4654515Z --- End of stack trace from previous location ---
2021-09-02T07:05:07.4656164Z    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext() in System.CommandLine.dll:token 0x6000651+0x70
2021-09-02T07:05:07.4657541Z 
2021-09-02T07:05:07.4867119Z ##[error]The process '/opt/hostedtoolcache/Python/3.9.6/x64/bin/pre-commit' failed with exit code 1
2021-09-02T07:05:07.4964749Z Post job cleanup.

@jmarolf
Copy link
Contributor

jmarolf commented Sep 2, 2021

I believe dotnet format whitespace --folder is the intended replacement in version 6

@JoeRobich
Copy link
Member

As Jon said, --folder is a unique options to the whitespace subcommand (see https://github.com/dotnet/format#unique-options-for-dotnet-format-whitespace). We are now running Code Style and 3rd Party analyzers by default which require a solution or project file for semantic analysis. As with previous versions, the --folder option is only supported for whitespace formatting.

@JoeRobich JoeRobich added the Question This issue is requesting information about dotnet-format label Sep 2, 2021
@mikeage
Copy link
Author

mikeage commented Sep 2, 2021

Thank you, that makes sense! Indeed, I only wanted the formatting, and this is perfect.

@mikeage mikeage closed this as completed Sep 2, 2021
@mikeage
Copy link
Author

mikeage commented Sep 2, 2021

Sorry, re-opening for one more question. The pre-commit hook always runs --include; see https://github.com/dotnet/format/blob/main/.pre-commit-hooks.yaml#L7. It seems like this is a problem since it means we can't run just the whitespace subcommand. On the other hand, I think it always needs to be included for pre-commit, which by default runs with a subset of files, and it's probably not right to ask people to all include it just because someone might want to pass a different command.

Should there be multiple IDs in the pre-commit hook, so that someone could select either dotnet-format (like today) or dotnet-format-whitespace to only run the whitespace command? Perhaps also dotnet-format-style and dotnet-format-analyzers? (can multiple commands be combined, or is it always either "all three" (default) or just one?

@mikeage mikeage reopened this Sep 2, 2021
@mikeage
Copy link
Author

mikeage commented Sep 2, 2021

Closing again; it can be overridden if needed in pre-commit-config.yaml:

  - repo: https://github.com/dotnet/format
    rev: "2c6ded76878d585a4f037c32ba0fbb2685f27b05"  # master as of 2021-09-02
    hooks:
      - id: dotnet-format
        exclude: ^(Assets/ThirdParty)|(Packages/)
        entry: dotnet-format whitespace
        args:
          - --folder
          - --include

@mikeage mikeage closed this as completed Sep 2, 2021
@JoeRobich
Copy link
Member

@mikeage If you think additional IDs and configurations would be generally useful, we would be open to PRs that improve the pre-commit functionality.

@mikeage
Copy link
Author

mikeage commented Sep 3, 2021

Thanks. I no longer think that any changes are required, but I will be opening a PR in the next few days to improve the pre-commit integration documentation. Thanks for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question This issue is requesting information about dotnet-format
Projects
None yet
Development

No branches or pull requests

3 participants