question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:34
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

18reactions
SammyROCKcommented, Feb 21, 2022

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
4reactions
inkysquidcommented, Sep 2, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

dotnet format command - .NET CLI
dotnet format is a code formatter that applies style preferences to a project or solution. Preferences will be read from an .editorconfig ...
Read more >
Using dotnet format Command to Format the C#/.NET Code
In this article we'll discuss the use of dotnet format, to enforce consistent coding standards across operating systems and IDEs.
Read more >
NETSDK1045: The current .NET SDK does not support ...
This error occurs when the build tools can't find the version of the .NET SDK that's needed to build a project. This is...
Read more >
Download .NET 6.0 (Linux, macOS, and Windows)
NET 6.0 downloads for Linux, macOS, and Windows. .NET is a free, cross-platform, open-source developer platform for building many different types of ...
Read more >
Untitled
NET 6.0 SDKs not showing with dotnet --info or dotnet --list-sdks and command depeding on SDK failes ... … 🎉 🥳 dotnet-format is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found