format icon indicating copy to clipboard operation
format copied to clipboard

🎉🥳 dotnet-format is now part of the .NET 6 SDK

Open JoeRobich opened this issue 3 years ago • 14 comments

Starting with the .NET 6 Preview 7, which is available now, dotnet format is a built-in SDK command. As part of this move the options have changed a bit.

Invoking the SDKs dotnet format command will fix whitespace, code style, and analyzer issues by default.

There are also 3 subcommands to target specific scenarios:

  • dotnet format whitespace: fixes whitespace
  • dotnet format style: runs code style analyzers
  • dotnet format analyzers: runs third party analyzers

Common options for all commands

  • --no-restore: Doesn't execute an implicit restore before formatting.
  • --include-generated: Format files generated by the SDK
  • --include: A list of relative file or folder paths to include in formatting. All files are formatted if empty.
  • --exclude: A list of relative file or folder paths to exclude from formatting.
  • --verbosity: Set the verbosity level. Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic]
  • --report: Accepts a file path, which if provided, will produce a json report in the given directory.
  • --binarylog: Log all project or solution load information to a binary log file.
  • --verify-no-changes: Verify no formatting changes would be performed. Terminates with a non-zero exit code if any files would have been formatted.

Unique options for dotnet format

note, this is equivalent to dotnet format <project or solution> --fix-whitespace --fix-style <severity> --fix-analyzers <severity> --diagnostics <diagnostics> in the global tool today.

Note: if the user specifies a severity here it is used for both style and analyzers.

Unique options for dotnet format whitespace

note, this is equivalent to dotnet format <project or solution> --fix-whitespace in the global tool today.

Unique options for dotnet format style

  • --severity: The severity of diagnostics to fix. Allowed values are info, warn, and error.

note, this is equivalent to dotnet format <project or solution> --fix-style <severity> in the global tool today.

Unique options for dotnet cleanup analyzers

  • --diagnostics: A space separated list of diagnostic ids to use as a filter when fixing code style or 3rd party issues.
  • --severity: The severity of diagnostics to fix. Allowed values are info, warn, and error.

note, this is equivalent to dotnet format <project or solution> --fix-analyzers <severity> --diagnostics <diagnostics> in the global tool today.

JoeRobich avatar Aug 12 '21 16:08 JoeRobich

Great news!

Will it be integrated with dotnet watch in the future?

marinasundstrom avatar Aug 18 '21 07:08 marinasundstrom

I use dotnet format --check a lot in my CI builds, is this option still available?

dnperfors avatar Aug 18 '21 14:08 dnperfors

I use dotnet format --check a lot in my CI builds, is this option still available?

@dnperfors I missed it when writing up the new options, but --verify-no-changes is the new name of --check. Some options were given new names in order to fit in better with the other CLI commands.

JoeRobich avatar Aug 18 '21 17:08 JoeRobich

see the docs here for the full list of command names. I'm working this week on updating things in this repo to match

jmarolf avatar Aug 18 '21 17:08 jmarolf

Small issue in case anyone else is experiencing it (not sure if it's by design). If I have a specific version installed locally e.g. dotnet tool install dotnet-format --version 5.1.225507, the command dotnet format will run the global .NET 6 version. To get around this I now run dotnet tool run dotnet-format.

inkysquid avatar Sep 02 '21 14:09 inkysquid

How can I tell which version of the dotnet format is bundled with the SDK?

mihai-unity avatar Oct 04 '21 20:10 mihai-unity

Ok, I think I'm going to answer this one myself. First you need to locate the location of the sdk

dotnet --list-sdk

CD into that directory and then look find the location of dotnet-format.dll with:

find . -type f -name "dotnet-format.dll"

then you extract the version information with PowerShell Core which in my case yields:

PS /usr/local/share/dotnet> (Get-Item ./sdk/6.0.100-rc.1.21463.6/DotnetTools/dotnet-format/dotnet-format.dll).VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
6.0.241702+727a… 6.0.4.41702      /usr/local/share/dotnet/sdk/6.0.100-rc.1.21463.6/DotnetTools/dotnet-format/dotnet-format.dll

mihai-unity avatar Oct 04 '21 20:10 mihai-unity

It does seem odd that dotnet format --version or similar doesn't work.

RikkiGibson avatar Oct 04 '21 20:10 RikkiGibson

It would be nice if --check was still available (likely as an alias of --verify-no-changes), this will break CI pipelines for several people.

AgentEnder avatar Oct 05 '21 17:10 AgentEnder

@AgentEnder We are tracking breaking changes in issue https://github.com/dotnet/format/issues/1292. There are several changes that would likely break users who do not update their pipelines.

JoeRobich avatar Oct 05 '21 19:10 JoeRobich

There are multiple breaking changes and some are hard to be silently fixed by aliases. For instance, the --folder flag lost it's alias -f but was also moved under a new verb whitespace. Users would need to fix their pipelines by reading the documentation. This is to be expected though since the major version was bumped.

mihai-unity avatar Oct 06 '21 09:10 mihai-unity

Hey there, congratulations! I'm trying to port my current setup over to .NET Core 6, and I'm finding that dotnet format does not auto-format Stylecop rules anymore. E.g. we had dotnet format --fix-analyzers warn before which would do that, and dotnet format analyzers --severity warn does not do anything, but we're getting all the style warnings on build.

Is there a way to achieve the same behaviour in .NET Core 6?

beatrichartz avatar Dec 20 '21 23:12 beatrichartz

Even though I can run dotnet format without installing the tool using mcr.microsoft.com/dotnet/sdk:6.0 as base, I still have this issue in my pipeline. I'm having to downgrade the image to mcr.microsoft.com/dotnet/sdk:6.0.102 in order to fix the issue without giving up on format validation at build.

#16 [build  8/11] RUN dotnet format --verify-no-changes
#16 sha256:741173a3ddf68bcd53518c987cc31d28c83d2bc115367551e6cd50fa1436a0a0
#16 16.02 Unhandled exception: System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
#16 16.03 Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
#16 16.03
#16 16.03 Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
#16 16.03
#16 16.03    at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
#16 16.03    at System.Reflection.Assembly.GetTypes()
#16 16.03    at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFinderHelpers.<>c.<LoadAnalyzersAndFixers>b__0_0(Assembly assembly)
#16 16.03    at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
#16 16.03    at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
#16 16.03    at System.Linq.Enumerable.OfTypeIterator[TResult](IEnumerable source)+MoveNext()
#16 16.03    at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
#16 16.03    at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
#16 16.03    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
#16 16.03    at System.Collections.Immutable.ImmutableArray.CreateRange[T](IEnumerable`1 items)
#16 16.03    at System.Collections.Immutable.ImmutableArray.ToImmutableArray[TSource](IEnumerable`1 items)
#16 16.03    at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFinderHelpers.LoadAnalyzersAndFixers(IEnumerable`1 assemblies)
#16 16.03    at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerReferenceInformationProvider.GetAnalyzersAndFixers(Project project)
#16 16.03    at System.Collections.Immutable.ImmutableDictionary.<>c__DisplayClass9_0`3.<ToImmutableDictionary>b__0(TSource element)
#16 16.03    at System.Linq.Utilities.<>c__DisplayClass2_0`3.<CombineSelectors>b__0(TSource x)
#16 16.03    at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
#16 16.03    at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 items, MutationInput origin, KeyCollisionBehavior collisionBehavior)
#16 16.03    at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs, Boolean avoidToHashMap)
#16 16.03    at System.Collections.Immutable.ImmutableDictionary`2.AddRange(IEnumerable`1 pairs)
#16 16.03    at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TSource,TKey,TValue](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 keyComparer, IEqualityComparer`1 valueComparer)
#16 16.03    at System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary[TSource,TKey,TValue](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
#16 16.03    at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerReferenceInformationProvider.GetAnalyzersAndFixers(Solution solution, FormatOptions formatOptions, ILogger logger)
#16 16.03    at Microsoft.CodeAnalysis.Tools.Analyzers.AnalyzerFormatter.FormatAsync(Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
#16 16.03    at Microsoft.CodeAnalysis.Tools.CodeFormatter.RunCodeFormattersAsync(Solution solution, ImmutableArray`1 formattableDocuments, FormatOptions formatOptions, ILogger logger, List`1 formattedFiles, CancellationToken cancellationToken)
#16 16.03    at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken, String binaryLogPath)
#16 16.03    at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.FormatAsync(FormatOptions formatOptions, ILogger`1 logger, CancellationToken cancellationToken)
#16 16.03    at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(InvocationContext context)
#16 16.03    at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseParseErrorReporting>b__0>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<<UseVersionOption>b__1>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseTypoCorrections>b__0>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__24_0>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseParseDirective>b__0>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseDebugDirective>b__0>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
#16 16.03 --- End of stack trace from previous location ---
#16 16.03    at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext()
#16 16.03 System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
#16 16.03
#16 16.03 File name: 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
#16 16.03 System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
#16 16.03
#16 16.03 File name: 'Microsoft.CodeAnalysis, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
#16 ERROR: executor failed running [/bin/sh -c dotnet format --verify-no-changes]: exit code: 1
------
 > [build  8/11] RUN dotnet format --verify-no-changes:
------
executor failed running [/bin/sh -c dotnet format --verify-no-changes]: exit code: 1

SammyROCK avatar Feb 21 '22 19:02 SammyROCK

@JoeRobich When will the fix for #1800 be available in the SDK?

Edit: See also https://github.com/dotnet/sdk/issues/32598.

glen-84 avatar May 22 '23 15:05 glen-84