{{ message }}
Fix NullReferenceException in CompleteFilename when called with empty string - #26286
Closed
Ilya (iSazonov) with Copilot wants to merge 4 commits into
Closed
Fix NullReferenceException in CompleteFilename when called with empty string#26286Ilya (iSazonov) with Copilot wants to merge 4 commits into
Ilya (iSazonov) with Copilot wants to merge 4 commits into
Conversation
Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix NullReferenceException in CompleteFilename method
Fix NullReferenceException in CompleteFilename when called with empty string
Oct 23, 2025
12 tasks
Collaborator
…ts.ps1 Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com>
Contributor
Author
Collaborator
|
Copilot Please remove your comments from the new test. |
Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com>
Contributor
Author
Ilya (iSazonov)
approved these changes
Oct 23, 2025
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a NullReferenceException thrown when calling CompleteFilename("") with an empty string by adding a null check for context.RelatedAsts before accessing it.
Key Changes:
- Added null/empty check for
context.RelatedAstsbefore array indexing - Added fallback to use current provider path when
RelatedAstsis null/empty - Added Pester test to verify the fix works correctly
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs | Added null check for context.RelatedAsts and fallback logic to use current provider path |
| test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 | Added test case to verify CompleteFilename("") doesn't throw exception |
Comments suppressed due to low confidence (1)
src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs:1
- The else block at line 4598 is paired with the if statement at line 4583, but based on the overall control flow structure, this creates an incorrect nesting. The else block (lines 4599-4602) should be associated with the if statement at line 4583, but there's already a closing brace at line 4598 that closes that if block. This means the else at line 4598 cannot be paired with the if at line 4583. The correct structure should have the else block (4599-4602) immediately follow the closing brace at 4598 without the 'else' keyword on line 4598, or the braces need to be restructured to properly nest the conditional logic.
// Copyright (c) Microsoft Corporation.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
5 tasks
Ilya (iSazonov)
marked this pull request as draft
October 23, 2025 09:10
Member
|
Suppressed by #26291 |
Contributor
Ilya (iSazonov)
deleted the
copilot/fix-nullreferenceexception-filename
branch
October 24, 2025 03:07
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Fixed
NullReferenceExceptionwhen calling[System.Management.Automation.CompletionCompleters]::CompleteFilename("").Root Cause
The public API
CompleteFilename(string fileName)creates aCompletionContextwithRelatedAstsset to null. The internal method tried to accesscontext.RelatedAsts[^1]without checking for null, causing the exception.Fix
Added null/empty check for
context.RelatedAstsbefore accessing it. When null or empty, falls back to usingcontext.ExecutionContext.SessionState.Internal.CurrentLocation.ProviderPath.Test
Added Pester test in
TabCompletion.Tests.ps1that verifies:CompleteFilename("")doesn't throw exceptionOriginal prompt
This section details on the original issue you should resolve
<issue_title>
[System.Management.Automation.CompletionCompleters]::CompleteFilename("")throws NullReferenceException</issue_title><issue_description>### Prerequisites
Steps to reproduce
on 7.6.0-preview.5
[System.Management.Automation.CompletionCompleters]::CompleteFilename("")throwsNullReferenceExceptionExpected behavior
should returns CompletionResultActual behavior
Error details