dotnet / dotnet/roslyn

'GenerateConstructorCodeFixProvider' encountered an error and has been disabled

Open
#82,157 0 comments 0 reactions 0 assignees View on GitHub
Area-IDE
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

**Version Used**:
Visual Studio 18.2.0
**Steps to Reproduce**:

1. I was typing parameters on a line of code that creates a new instance of a class, and the error happened after typing a comma for the next constructor parameter. Constructor is not unusual and has 3 parameters that are also classes.

**Diagnostic Id**:

If this is a report about a bug in an analyzer, please include the diagnostic ID and message if possible (e.g. `"IDE0030: Use coalesce expression"`).

**Expected Behavior**:
No bar at the top of Visual Studio saying this provider had an error.

**Actual Behavior**:
A bar at the top of Visual Studio saying this provider had an error.
```
System.ArgumentException : SyntaxTree is not part of the compilation
Parameter name: syntaxTree
at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.GetSemanticModel(SyntaxTree syntaxTree,SemanticModelOptions options)
at Microsoft.CodeAnalysis.InitializeParameter.AbstractInitializerParameterService`1.IsThrowNotImplementedProperty(Compilation compilation,IPropertySymbol property,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor.GenerateConstructorHelpers.IsViableFieldOrProperty(SemanticDocument document,ITypeSymbol parameterType,ISymbol symbol,CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor.GenerateConstructorHelpers.<>c__DisplayClass3_0`1.g__FindExistingOrCreateNewMember|0(ParameterName parameterName,ITypeSymbol parameterType,Argument`1 argument)
at async Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor.GenerateConstructorHelpers.GetParametersAsync[TExpressionSyntax]()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor.AbstractGenerateConstructorService`2.State.InitializeNonDelegatedConstructorAsync[TService,TExpressionSyntax]()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor.AbstractGenerateConstructorService`2.State.TryInitializeAsync[TService,TExpressionSyntax]()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor.AbstractGenerateConstructorService`2.State.GenerateAsync[TService,TExpressionSyntax]()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.GenerateMember.GenerateConstructor.AbstractGenerateConstructorService`2.GenerateConstructorAsync[TService,TExpressionSyntax]()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeFixes.GenerateMember.AbstractGenerateMemberCodeFixProvider.RegisterCodeFixesAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.CodeFixes.CodeFixService.GetCodeFixesAsync()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at async Microsoft.CodeAnalysis.Extensions.IExtensionManagerExtensions.PerformFunctionAsync[T]()
```

Contributor guide

Open the contributing guide

Research direction

Start with the GenerateConstructorCodeFixProvider stack trace, especially GenerateConstructorHelpers.GetParametersAsync and IsViableFieldOrProperty, to understand how the syntax tree reaches CSharpCompilation.GetSemanticModel. Reproduce the constructor-parameter editing scenario in Visual Studio 18.2.0 and determine the conditions that trigger the exception. Done means the provider no longer throws or becomes disabled in that scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.