cli-lab icon indicating copy to clipboard operation
cli-lab copied to clipboard

System.Reflection.TargetInvocationException

Open kairlec opened this issue 4 years ago • 11 comments

我使用的版本是1.0.115603 当我使用dotnet-core-uninstall list命令时,事实上我使用任意正确命令,其都会报该错误

Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.DotNet.Tools.Uninstall.Windows.RegistryQuery.IsDotNetCoreBundle(RegistryKey registryKey)
   at Microsoft.DotNet.Tools.Uninstall.Windows.RegistryQuery.<>c.<GetAllInstalledBundles>b__1_1(RegistryKey bundle)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToList()
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Microsoft.DotNet.Tools.Uninstall.Windows.RegistryQuery.GetAllInstalledBundles()
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.ListCommandExec.Execute(IBundleCollector bundleCollector)
   at Microsoft.DotNet.Tools.Uninstall.Shared.Configs.CommandLineConfigs.<>c__DisplayClass40_0.<.cctor>b__4()
   at Microsoft.DotNet.Tools.Uninstall.Shared.Exceptions.ExceptionHandler.<>c__DisplayClass0_0.<HandleException>b__0()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.CommandLine.Invocation.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<UseParseErrorReporting>b__22_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c__DisplayClass8_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<UseSuggestDirective>b__7_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<UseParseDirective>b__6_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<UseHelp>b__20_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass4_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<RegisterWithDotnetSuggest>b__23_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c__DisplayClass5_0.<<UseExceptionHandler>b__0>d.MoveNext()

我的notnet --info信息如下

.NET Core SDK(反映任何 global.json):
 Version:   3.1.101
 Commit:    b377529961

运行时环境:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.101\

Host (useful for support):
  Version: 3.1.1
  Commit:  a1388f194c

.NET Core SDKs installed:
  3.1.101 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download

kairlec avatar Mar 16 '20 00:03 kairlec

Same issue.

$ dotnet-core-uninstall list

This tool can not uninstall versions of the runtime or SDK that are▒
    - SDKs installed using Visual Studio 2019 Update 3 or later.
    - SDKs and runtimes installed via zip/scripts.
    - Runtimes installed with SDKs (these should be removed by removing that SDK).
The versions that can be uninstalled with this tool are:

Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.IO.IOException: The handle is invalid.
   at System.ConsolePal.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
   at System.ConsolePal.GetBufferInfo()
   at System.ConsolePal.get_WindowWidth()
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.ListCommandExec.Execute(IEnumerable`1 bundles, IEnumerable`1 supportedBundleTypes)
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.ListCommandExec.Execute(IBundleCollector bundleCollector)
   at Microsoft.DotNet.Tools.Uninstall.Shared.Configs.CommandLineConfigs.<>c__DisplayClass40_0.<.cctor>b__4()
   at Microsoft.DotNet.Tools.Uninstall.Shared.Exceptions.ExceptionHandler.<>c__DisplayClass0_0.<HandleException>b__0()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.CommandLine.Invocation.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<UseParseErrorReporting>b__22_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c__DisplayClass8_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<UseSuggestDirective>b__7_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<UseParseDirective>b__6_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<UseHelp>b__20_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass4_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c.<<RegisterWithDotnetSuggest>b__23_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Invocation.InvocationExtensions.<>c__DisplayClass5_0.<<UseExceptionHandler>b__0>d.MoveNext()
dotnet-core-uninstall --version
1.0.115603+b1b2dd1c7f6ba246efb288a73693b66cfd480382

bitobrian avatar Mar 18 '20 16:03 bitobrian

I have the same exception with same stacktrace when running the command in MINGW64 (Git Bash) with and without admin rights. The command works in Powershell.

mattrobineau avatar Mar 24 '20 14:03 mattrobineau

Interesting, thanks for filing this issue. @kairlec @bitobrian, what shells are you using? Does this only occur in certain shells?

sfoslund avatar Mar 24 '20 16:03 sfoslund

Interesting, thanks for filing this issue. @kairlec @bitobrian, what shells are you using? Does this only occur in certain shells?

我在我电脑上所有的可用终端执行都会出现一样的错误 emi

kairlec avatar Mar 25 '20 00:03 kairlec

Based on the stack traces it seems like we have two separate issues here:

  1. On certain shells we can't get the window width to properly display output. I can repro this issue.
  2. A bundle does not contain all the information that we expect when we're searching the registry. I haven't been able to repro this. @kairlec is there any chance that the 3.1.101 SDK listed in dotnet --info was installed by Visual Studio? If it was, it will have "from Visual Studio" in the display name in Add or Remove Programs.

sfoslund avatar Mar 31 '20 19:03 sfoslund

Based on the stack traces it seems like we have two separate issues here:

  1. On certain shells we can't get the window width to properly display output. I can repro this issue.
  2. A bundle does not contain all the information that we expect when we're searching the registry. I haven't been able to repro this. @kairlec is there any chance that the 3.1.101 SDK listed in dotnet --info was installed by Visual Studio? If it was, it will have "from Visual Studio" in the display name in Add or Remove Programs.

I did install Dotnet Core from Visual Studio, and it also showed from Visual Studio in Add or Remove Programs. I thought this tool would work like this too, it seems that I think too much

kairlec avatar Apr 01 '20 01:04 kairlec

This tool can't be used to uninstall SDKs managed by Visual Studio (any SDKs installed with Visual Studio 16.3 or later) but it also shouldn't crash if that's the case. I'm reopening to track investigating this failure.

sfoslund avatar Apr 01 '20 15:04 sfoslund

@sfoslund

This tool can't be used to uninstall SDKs managed by Visual Studio (any SDKs installed with Visual Studio 16.3 or later) but it also shouldn't crash if that's the case. I'm reopening to track investigating this failure.

There is --force flag mentioned in MS docs: --force Forces removal of versions that might be used by Visual Studio.

in https://docs.microsoft.com/en-us/dotnet/core/additional-tools/uninstall-tool?tabs=windows

r-pankevicius avatar Apr 22 '20 14:04 r-pankevicius

@r-pankevicius yes, thanks for pointing that out, there's some subtle complexity here.

Prior to 16.3, Visual Studio would install SDKs and leave them behind, the idea being that uninstalling old SDKs could break people if they're using global.json. There's nothing special about these SDK installations, other than the method of installation, so this tool is able to uninstall them. We mark them and require users to use the --force option because some of those older versions of Visual Studio will break without them, so we want you to be sure that you want them gone.

In 16.3 and later, Visual Studio switched to managing SDKs itself instead of simply leaving them behind. This tool (and the user) can't uninstall these SDKs manually. If you have the latest Visual Studio you can see this in action if you go to Add or Remove Programs and look through the SDKs you have installed. You'll likely see a few that say "from Visual Studio" (see the screenshot below). If you try to uninstall one of these through Add or Remove Programs you'll get an error telling you to go to the Visual Studio Installer.

AddRemoveProgramsVSInstall

tldr: There are 2 ways Visual Studio versions manage SDKs. This tool can uninstall (with the --force switch) one type, but it can't uninstall the other at all.

sfoslund avatar Apr 22 '20 15:04 sfoslund

Same issue

PS C:\WINDOWS\system32> dotnet-core-uninstall list --sdk

This tool cannot uninstall versions of the runtime or SDK that are 
    - SDKs installed using Visual Studio 2019 Update 3 or later.
    - SDKs and runtimes installed via zip/scripts.
    - Runtimes installed with SDKs (these should be removed by removing that SDK).
The versions that can be uninstalled with this tool are:

.NET Core SDKs:
  5.0.203          x64    [Used by Visual Studio. Specify individually or use --force to remove]
  2.1.800          x64    [Used by Visual Studio 2019. Specify individually or use --force to remove]
  2.1.800-preview  x64
  2.1.602          x64
PS C:\WINDOWS\system32> dotnet-core-uninstall remove 2.1.602 --sdk
The following items will be removed:
  Microsoft .NET Core SDK 2.1.602 (x64)

To avoid breaking Visual Studio or other problems, read https://aka.ms/dotnet-core-uninstall-docs.

Do you want to continue? [y/n] y
Uninstalling: Microsoft .NET Core SDK 2.1.602 (x64).
Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ComponentModel.Win32Exception (2): The system cannot find the file specified.
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.UninstallCommandExec.DoIt(IEnumerable`1 bundles)
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.UninstallCommandExec.Execute(IBundleCollector bundleCollector)
   at Microsoft.DotNet.Tools.Uninstall.Shared.Configs.CommandLineConfigs.<>c__DisplayClass40_0.<.cctor>b__6()
   at Microsoft.DotNet.Tools.Uninstall.Shared.Exceptions.ExceptionHandler.<>c__DisplayClass0_0.<HandleException>b__0()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Delegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.CommandLine.Invocation.ModelBindingCommandHandler.InvokeAsync(InvocationContext context)
   at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseParseErrorReporting>b__21_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseHelp>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass25_0.<<UseVersionOption>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass23_0.<<UseTypoCorrections>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__22_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseParseDirective>b__20_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseDebugDirective>b__11_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__10_0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass14_0.<<UseExceptionHandler>b__0>d.MoveNext()

bevin0708 avatar May 17 '21 05:05 bevin0708

There are 3 different issues, the issue needs to be split.

For example original NullReferenceException issue that @kairlec has was solved on Jan 6, 2021 by commit: https://github.com/dotnet/cli-lab/commit/ff7ccfcb142100ed3b7ca338921922720087caa1#diff-bad95d57529fabfa7c211b95dca8e77cf94d58267bf9ddde1746e4130e4009df This commit added null check in method method private static bool IsNetCoreBundle(RegistryKey uninstallKey).

Different issue from @bitobrian, occuring at list stage:

 ---> System.IO.IOException: The handle is invalid.
   at System.ConsolePal.GetBufferInfo(Boolean throwOnNoConsole, Boolean& succeeded)
   at System.ConsolePal.GetBufferInfo()
   at System.ConsolePal.get_WindowWidth()

Latest issue from @bevin0708, occuring at unistall stage:

Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.ComponentModel.Win32Exception (2): The system cannot find the file specified.
   at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.UninstallCommandExec.DoIt(IEnumerable`1 bundles)
   at Microsoft.DotNet.Tools.Uninstall.Shared.Commands.UninstallCommandExec.Execute(IBundleCollector bundleCollector)

r-pankevicius avatar May 17 '21 14:05 r-pankevicius