dotnet / format

Home for the dotnet-format command
MIT License
1.94k stars 172 forks source link

System.Exception: Restore operation failed. #1560

Open kthompson opened 2 years ago

kthompson commented 2 years ago

When running dotnet format style -v diag i get an exception as follows:

  The dotnet runtime version is '6.0.3'.
  The dotnet CLI version is '6.0.103'.
  Using MSBuild.exe located in 'C:\Program Files\dotnet\sdk\6.0.201\'.
  Formatting code files in workspace 'D:\code\work\platform\apps\ciri\Ciri.sln'.
  Loading workspace.

Unhandled exception: System.Exception: Restore operation failed.
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.OpenMSBuildWorkspaceAsync(String solutionOrProjectPath, WorkspaceType workspaceType, Boolean noRestore, Boolean requiresSemantics, String binaryLogPath, Boolean logWorkspaceWarnings, ILogger logger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken, String binaryLogPath)
   at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.FormatAsync(FormatOptions formatOptions, ILogger`1 logger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.Commands.FormatStyleCommand.FormatStyleHandler.InvokeAsync(InvocationContext context)   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<<UseVersionOption>b__1>d.MoveNext()--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__24_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseDebugDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext()

dotnet format --version results with 6.0.252703+68bc36719088c86b0ff01334039b0611741b8276

Also, this issue does not appear when running under CI with the same repo.

Any guidance on how I might be able to investigate further?

BrightSoul commented 2 years ago

It was working fine for me with SDK 6.0.100 but then, when I updated to 6.0.201, it started giving out errors. I then installed the global tool dotnet-format and it gave more errors. Now, even if I uninstalled the global tool and reverted my global.json to 6.0.100, it wouldn't work anymore. Same error as this issue.

$> dotnet format
Unhandled exception: System.Exception: Restore operation failed.
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.OpenMSBuildWorkspaceAsync(String solutionOrProjectPath, WorkspaceType workspaceType, Boolean noRestore, Boolean requiresSemantics, String binaryLogPath, Boolean logWorkspaceWarnings, ILogger logger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(FormatOptions formatOptions, ILogger logger, CancellationToken cancellationToken, String binaryLogPath)
   at Microsoft.CodeAnalysis.Tools.FormatCommandCommon.FormatAsync(FormatOptions formatOptions, ILogger`1 logger, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.Commands.RootFormatCommand.FormatCommandDefaultHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseParseErrorReporting>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass27_0.<<UseVersionOption>b__1>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__24_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseParseDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass11_0.<<UseDebugDirective>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext()
analogrelay commented 2 years ago

Same problem here. On installing SDK 6.0.201, I started getting this error. It persisted even after uninstalling that SDK version.

I was able to workaround it by removing all installed .NET SDKs (I'm on macOS so I did sudo rm -Rf /usr/local/share/dotnet) and then reinstalling just SDK 6.0.103. Not ideal at all, but a possible workaround for others affected by this.

kardiojack commented 2 years ago

Any update on this? dotnet format works locally with 6.0.202 and

dotnet format --version
6.2.315104+eb87be219a075ad9401cd5dd3cab14288a55b59b

but I have had to take it out of my GH Actions CI because it throws the aforementioned exception.

The GH Action runner has the same versions of dotnet CLI/SDK and dotnet format as above.

JoeRobich commented 2 years ago

Does running dotnet restore work from the commandline? At the moment format is invoking that command as the restore operation.

As a workaround you can pass the --no-restore flag to dotnet-format. You may need to add a CI step to perform the dotnet restore prior to running format.

wzchua commented 2 years ago

From the logs, it seems to be using the msbuild from the latest sdk instead of the one in the sdk defined in global.json

ctyar-qc commented 1 year ago

We were suddenly seeing the same error message. The underlying issue was something else. dotnet restore was failing because of a duplicate reference in a csproj file. Removing that fixed the issue. I think the dotnet format can show a better error message here.

dimaaan commented 1 year ago

In my case it was failing because of a reference to non-existing version of the package in a csproj file

jspraul commented 1 year ago

--no-restore also was enough for me

Maybe dotnet format could display a warning message ("restore failed and may need to be run separately beforehand") instead of crashing?

This started after updating Visual Studio 2022 and affects the previous SDK (eg. 6.0.118 fails, 6.0.313 works).

richard-collette-precisely commented 9 months ago

This just started happening to me. My docker build worked fine locally but when running in gitlab agent using Kaniko on a Platform9 cluster, this started popping up.

nico-abram commented 7 months ago

I was getting this error, figured out that it was because I have msvc's vcvarsall.bat in my powershell profile (startup script) which sets the PLATFORM env var to x64 which results in this error (I have no .sln.metaproj file, it was inferring the config from that env var I think?)