From c74e3f250ccc1746eebf6b6daf3b271abfbb9436 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 23 Oct 2025 05:49:45 +0000 Subject: [PATCH 1/4] Initial plan From c7a22707d82b95391ee1b1c95c82fc2bc6ca80c7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 23 Oct 2025 06:01:05 +0000 Subject: [PATCH 2/4] Fix NullReferenceException in CompleteFilename and add test Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com> --- .../CommandCompletion/CompletionCompleters.cs | 19 +++++++++++++------ .../Host/TabCompletion/BugFix.Tests.ps1 | 10 ++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs index 80ba4545ff4..464d8e202b1 100644 --- a/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs +++ b/src/System.Management.Automation/engine/CommandCompletion/CompletionCompleters.cs @@ -4580,13 +4580,20 @@ internal static IEnumerable CompleteFilename(CompletionContext return CommandCompletion.EmptyCompletionResult; } - var lastAst = context.RelatedAsts[^1]; - if (lastAst.Parent is UsingStatementAst usingStatement - && usingStatement.UsingStatementKind is UsingStatementKind.Module or UsingStatementKind.Assembly - && lastAst.Extent.File is not null) + if (context.RelatedAsts != null && context.RelatedAsts.Count > 0) { - relativeBasePath = Directory.GetParent(lastAst.Extent.File).FullName; - _ = currentPS.AddParameter("RelativeBasePath", relativeBasePath); + var lastAst = context.RelatedAsts[^1]; + if (lastAst.Parent is UsingStatementAst usingStatement + && usingStatement.UsingStatementKind is UsingStatementKind.Module or UsingStatementKind.Assembly + && lastAst.Extent.File is not null) + { + relativeBasePath = Directory.GetParent(lastAst.Extent.File).FullName; + _ = currentPS.AddParameter("RelativeBasePath", relativeBasePath); + } + else + { + relativeBasePath = context.ExecutionContext.SessionState.Internal.CurrentLocation.ProviderPath; + } } else { diff --git a/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 b/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 index 32fd73f8c09..11190af725b 100644 --- a/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 @@ -126,4 +126,14 @@ Describe "Tab completion bug fix" -Tags "CI" { $Runspace.Dispose() } } + + It "[CompletionCompleters]::CompleteFilename should not throw NullReferenceException with empty string" { + # This test verifies the fix for a bug where CompleteFilename("") would throw NullReferenceException + # because context.RelatedAsts was null when called via the public API + { [System.Management.Automation.CompletionCompleters]::CompleteFilename("") } | Should -Not -Throw + + # Verify it returns results (may be empty or contain current directory items) + $result = [System.Management.Automation.CompletionCompleters]::CompleteFilename("") + $result | Should -Not -BeNullOrEmpty + } } From 459c0f326db39ac5ea8eb885a02d74722dc9d491 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 23 Oct 2025 06:20:04 +0000 Subject: [PATCH 3/4] Move CompleteFilename test from BugFix.Tests.ps1 to TabCompletion.Tests.ps1 Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com> --- test/powershell/Host/TabCompletion/BugFix.Tests.ps1 | 10 ---------- .../Host/TabCompletion/TabCompletion.Tests.ps1 | 10 ++++++++++ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 b/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 index 11190af725b..32fd73f8c09 100644 --- a/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/BugFix.Tests.ps1 @@ -126,14 +126,4 @@ Describe "Tab completion bug fix" -Tags "CI" { $Runspace.Dispose() } } - - It "[CompletionCompleters]::CompleteFilename should not throw NullReferenceException with empty string" { - # This test verifies the fix for a bug where CompleteFilename("") would throw NullReferenceException - # because context.RelatedAsts was null when called via the public API - { [System.Management.Automation.CompletionCompleters]::CompleteFilename("") } | Should -Not -Throw - - # Verify it returns results (may be empty or contain current directory items) - $result = [System.Management.Automation.CompletionCompleters]::CompleteFilename("") - $result | Should -Not -BeNullOrEmpty - } } diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index 046d00b442f..d41ece5008d 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -4012,4 +4012,14 @@ Describe "WSMan Config Provider tab complete tests" -Tags Feature,RequireAdminOn # https://github.com/PowerShell/PowerShell/issues/4744 # TODO: move to test cases above once working } + + It "[CompletionCompleters]::CompleteFilename should not throw NullReferenceException with empty string" { + # This test verifies the fix for a bug where CompleteFilename("") would throw NullReferenceException + # because context.RelatedAsts was null when called via the public API + { [System.Management.Automation.CompletionCompleters]::CompleteFilename("") } | Should -Not -Throw + + # Verify it returns results (may be empty or contain current directory items) + $result = [System.Management.Automation.CompletionCompleters]::CompleteFilename("") + $result | Should -Not -BeNullOrEmpty + } } From 534098ccbe3898f9b070bdc3bd1a89d50f4385a1 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 23 Oct 2025 06:49:40 +0000 Subject: [PATCH 4/4] Remove comments from CompleteFilename test Co-authored-by: iSazonov <22290914+iSazonov@users.noreply.github.com> --- test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index d41ece5008d..9582ad72074 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -4014,11 +4014,7 @@ Describe "WSMan Config Provider tab complete tests" -Tags Feature,RequireAdminOn } It "[CompletionCompleters]::CompleteFilename should not throw NullReferenceException with empty string" { - # This test verifies the fix for a bug where CompleteFilename("") would throw NullReferenceException - # because context.RelatedAsts was null when called via the public API { [System.Management.Automation.CompletionCompleters]::CompleteFilename("") } | Should -Not -Throw - - # Verify it returns results (may be empty or contain current directory items) $result = [System.Management.Automation.CompletionCompleters]::CompleteFilename("") $result | Should -Not -BeNullOrEmpty }