Skip to content

Null Reference while accessing GlobalOptionService #61473

@prshri-msft

Description

@prshri-msft

Version Used:
Microsoft.CodeAnalysis.LanguageServer.Protocol.dll
[assembly: AssemblyVersion("17.3.14.0")]

Steps to Reproduce:
The XAML Designer is using the following code snippet to generate event handler code in the Code behind file.

void IHostCodeContext.AppendStatements(IHostCodeEvent codeEvent, string methodName, string statements, int relativePosition)
        {
            Action<object> deleteFunc;
            IHostSourceItem item = this.info.ImplementationItem;
            CodeFunction function = this.FindMethod(this.info.ImplementationItem, codeEvent, methodName, out deleteFunc);
            if (function == null)
            {
                function = this.FindMethod(this.info.DeclarationItem, codeEvent, methodName, out deleteFunc);
                item = this.info.DeclarationItem;
            }
            if (function == null)
            {
                throw new ArgumentException(
                    string.Format(CultureInfo.CurrentCulture,
                                        InternalResources.MethodNotFoundForAppend,
                                        methodName));
            }

            EditPoint point = function.GetEndPoint(vsCMPart.vsCMPartBody).CreateEditPoint();
            if (relativePosition < 0)
            {
                point.LineUp(Math.Abs(relativePosition));
                point.StartOfLine();
            }

            point.Insert(statements);
            EditPoint smartFormat = function.StartPoint.CreateEditPoint();
            smartFormat.SmartFormat(function.EndPoint);
            this.EnsureFileOpened(item);
        }

This call to smartFormat(function.EndPoint) results in a null ref in the following call stack

 	Microsoft.CodeAnalysis.LanguageServer.Protocol.dll!Microsoft.CodeAnalysis.Formatting.SyntaxFormattingOptionsStorage.GetCommonSyntaxFormattingOptions(Microsoft.CodeAnalysis.Options.IGlobalOptionService globalOptions, string language) Line 25	C#
 	Microsoft.CodeAnalysis.CSharp.EditorFeatures.dll!Microsoft.CodeAnalysis.CSharp.Formatting.CSharpSyntaxFormattingOptionsStorage.GetCSharpSyntaxFormattingOptions(Microsoft.CodeAnalysis.Options.IGlobalOptionService globalOptions) Line 33	C#
 	Microsoft.CodeAnalysis.LanguageServer.Protocol.dll!Microsoft.CodeAnalysis.Formatting.SyntaxFormattingOptionsStorage.GetSyntaxFormattingOptionsAsync(Microsoft.CodeAnalysis.Document document, Microsoft.CodeAnalysis.Options.IGlobalOptionService globalOptions, System.Threading.CancellationToken cancellationToken) Line 33	C#
 	Microsoft.VisualStudio.LanguageServices.dll!Microsoft.VisualStudio.LanguageServices.Implementation.LanguageService.AbstractLanguageService<Microsoft.VisualStudio.LanguageServices.CSharp.LanguageService.CSharpPackage, Microsoft.VisualStudio.LanguageServices.CSharp.LanguageService.CSharpLanguageService>.FormatWorker(Microsoft.VisualStudio.TextManager.Interop.IVsTextLayer textLayer, Microsoft.VisualStudio.TextManager.Interop.TextSpan[] selections, System.Threading.CancellationToken cancellationToken) Line 93	C#
 	Microsoft.VisualStudio.LanguageServices.dll!Microsoft.VisualStudio.LanguageServices.Implementation.LanguageService.AbstractLanguageService<Microsoft.VisualStudio.LanguageServices.CSharp.LanguageService.CSharpPackage, Microsoft.VisualStudio.LanguageServices.CSharp.LanguageService.CSharpLanguageService>.Format.AnonymousMethod__0(Microsoft.VisualStudio.Utilities.IUIThreadOperationContext c) Line 44	C#
 	Microsoft.VisualStudio.Editor.Implementation.dll!Microsoft.VisualStudio.Editor.Implementation.VSUIThreadOperationExecutor.Execute(Microsoft.VisualStudio.Utilities.UIThreadOperationExecutionOptions executionOptions, System.Action<Microsoft.VisualStudio.Utilities.IUIThreadOperationContext> action) Line 46	C#
 	Microsoft.VisualStudio.LanguageServices.dll!Microsoft.VisualStudio.LanguageServices.Implementation.LanguageService.AbstractLanguageService<Microsoft.VisualStudio.LanguageServices.CSharp.LanguageService.CSharpPackage, Microsoft.VisualStudio.LanguageServices.CSharp.LanguageService.CSharpLanguageService>.Format(Microsoft.VisualStudio.TextManager.Interop.IVsTextLayer textLayer, Microsoft.VisualStudio.TextManager.Interop.TextSpan[] selections) Line 46	C#
 	[Native to Managed Transition]	
 	[Managed to Native Transition]		Microsoft.VisualStudio.DesignTools.XamlDesignerHost.dll!Microsoft.VisualStudio.DesignTools.XamlDesignerHost.Code.DTECodeContext.Microsoft.VisualStudio.DesignTools.DesignerContract.IHostCodeContext.AppendStatements(Microsoft.VisualStudio.DesignTools.DesignerContract.IHostCodeEvent codeEvent, string methodName, string statements, int relativePosition) Line 1578	C#

Inner Exception:

  at Microsoft.CodeAnalysis.Formatting.LineFormattingOptionsStorage.GetLineFormattingOptions(IGlobalOptionService globalOptions, String language) in /_/src/Features/LanguageServer/Protocol/Features/Options/LineFormattingOptionsStorage.cs:line 21

Here access to this IGlobalOptionService globalOptions is null.

Expected Behavior:
Should not null ref and create right formatting.

Actual Behavior:
Fails with Null ref exception for IGlobalOptionService globalOptions

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-IDEuntriagedIssues and PRs which have not yet been triaged by a lead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions