format icon indicating copy to clipboard operation
format copied to clipboard

System.Exception: Restore operation failed.

Open kthompson opened this issue 2 years ago • 10 comments

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?

kthompson avatar Mar 25 '22 20:03 kthompson

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()

BrightSoul avatar Apr 03 '22 11:04 BrightSoul

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.

analogrelay avatar Apr 07 '22 23:04 analogrelay

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.

kardiojack avatar Apr 30 '22 22:04 kardiojack

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.

JoeRobich avatar May 03 '22 17:05 JoeRobich

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

wzchua avatar Aug 12 '22 02:08 wzchua

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.

ctyar-qc avatar Feb 20 '23 15:02 ctyar-qc

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

dimaaan avatar Feb 23 '23 11:02 dimaaan

--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).

jspraul avatar Jun 16 '23 16:06 jspraul

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.

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?)

nico-abram avatar Apr 18 '24 14:04 nico-abram