Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 17, 2025

Fix NullReferenceException when calling SyntaxFactory.ParseArgumentList without parentheses

Issue

When calling SyntaxFactory.ParseArgumentList("42") without parentheses, the method fails with either a Debug.Assert failure or NullReferenceException. The method works correctly when parentheses are included: SyntaxFactory.ParseArgumentList("(42)").

Root Cause

The ParseParenthesizedArguments method in ParseExpression.vb assumes the current token is an OpenParenToken. When it's not, the TryGetTokenAndEatNewLine call with createIfMissing:=False leaves openParen as Nothing (null), which later causes issues.

Solution

Modified ParseParenthesizedArguments to gracefully handle missing opening parentheses by:

  1. Checking if the OpenParenToken is found
  2. Creating a missing token with appropriate error diagnostic (BC30199: '(' expected) if not found
  3. Ensuring the method always returns a valid ArgumentListSyntax

Changes Made

  • ParseExpression.vb (9 lines changed):

    • Removed Debug.Assert statements that caused assertion failures
    • Added If statement to check if TryGetTokenAndEatNewLine succeeds
    • Create missing OpenParenToken with proper error diagnostic when not found
  • SyntaxFactoryTests.vb (25 lines added):

    • Added comprehensive test TestParseArgumentListWithoutParentheses
    • Verifies no exception is thrown when parentheses are missing
    • Verifies proper diagnostic errors are reported
    • Verifies missing tokens are marked correctly
    • Verifies valid input still works correctly

Test Results

  • ✅ New test TestParseArgumentListWithoutParentheses passes
  • ✅ All 7 SyntaxFactoryTests pass
  • ✅ All 9 SyntaxListTests pass (which use ParseArgumentList)
  • ✅ 54 ParseExpression tests pass
  • ✅ 1239 total VB Syntax tests pass
  • ✅ No regressions detected

Security Summary

No security vulnerabilities introduced. CodeQL analysis did not identify any issues.

Fixes #58668

Original prompt

This section details on the original issue you should resolve

<issue_title>NullReferenceException when calling SyntaxFactory.ParseArgumentList</issue_title>
<issue_description>Version Used:
Nuget Microsoft.CodeAnalysis.VisualBasic Versions=4.0.1, 3.11.0

What are we trying to do?
Parse an argument list (and recieving an unexpected exception (presumably because the input is invalid).)

  1. Reference Microsoft.CodeAnalysis.VisualBasic package in a project
  2. Call SyntaxFactory.ParseArgumentList("42")

Expected Behavior:
Correct parsing of the argument. At least a more understandable exception like ArgumentException or similar, but not a direct bubbling of an internal exception.

Actual Behavior:
NullReferenceExceptionwhen calling a static method.


The current implementation is quite misleading. As soon as one changes the passed string to contain parenthesis (turn "42" into "(42)") the parsing works.
Which could be acceptable behavior. But simply passing on the internal exception is not. It should be something else.

</issue_description>

<agent_instructions>@copilot can you make a test to see if this still repros? Note that this is testing a VB syntaxfactory method.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Fixes #58668


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits October 17, 2025 18:51
…missing

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Fix NullReferenceException in SyntaxFactory.ParseArgumentList Fix NullReferenceException in VB SyntaxFactory.ParseArgumentList when parentheses are missing Oct 17, 2025
Copilot finished work on behalf of CyrusNajmabadi October 17, 2025 18:58
@Copilot Copilot AI requested a review from CyrusNajmabadi October 17, 2025 18:58
' Should have an error for missing opening parenthesis
Dim errors = result.GetDiagnostics().ToArray()
Assert.True(errors.Length > 0, "Expected diagnostic errors for missing parentheses")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot fix these blank lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in c17a74d (removed extra blank lines between statements)

Co-authored-by: CyrusNajmabadi <4564579+CyrusNajmabadi@users.noreply.github.com>
@Copilot Copilot AI requested a review from CyrusNajmabadi October 20, 2025 09:00
Copilot finished work on behalf of CyrusNajmabadi October 20, 2025 09:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullReferenceException when calling SyntaxFactory.ParseArgumentList

2 participants