From c8440a35dda433e028409539847e5da5d750a689 Mon Sep 17 00:00:00 2001 From: Gilbert Sanchez Date: Fri, 12 Jun 2026 17:33:45 -0700 Subject: [PATCH 1/2] fix: InputObject hashtable no longer mutated by Get-Dependency (#35) Parse-Dependency called $Dependencies.Remove('PSDependOptions') on the original hashtable because $Dependencies was assigned by reference. Clone each element before Parse-Dependency runs so the caller's object is left intact and a second Invoke-PSDepend / Get-Dependency call still honours PSDependOptions (e.g. Target). Co-Authored-By: Claude Sonnet 4.6 --- CHANGELOG.md | 6 ++++++ PSDepend/Public/Get-Dependency.ps1 | 2 +- Tests/PSDepend.Tests.ps1 | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb01fbc..aaa0e86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- `Get-Dependency -InputObject` no longer mutates the caller's hashtable: + `PSDependOptions` is now preserved after the call, so a second invocation + with the same object still honors global options such as `Target` (#35). + ## [0.4.1] - 2026-06-12 ### Added diff --git a/PSDepend/Public/Get-Dependency.ps1 b/PSDepend/Public/Get-Dependency.ps1 index 96f1544..b1935cc 100644 --- a/PSDepend/Public/Get-Dependency.ps1 +++ b/PSDepend/Public/Get-Dependency.ps1 @@ -437,7 +437,7 @@ elseif ($PSCmdlet.ParameterSetName -eq 'Hashtable') { $DependencyFile = 'Hashtable' $ParsedDependencies = foreach ($InputDependency in $InputObject) { - $Dependencies = $InputDependency + $Dependencies = $InputDependency.Clone() Parse-Dependency -ParamSet $PSCmdlet.ParameterSetName } diff --git a/Tests/PSDepend.Tests.ps1 b/Tests/PSDepend.Tests.ps1 index c7c094d..d30b593 100644 --- a/Tests/PSDepend.Tests.ps1 +++ b/Tests/PSDepend.Tests.ps1 @@ -119,6 +119,26 @@ Describe "Get-Dependency PS$PSVersion" -Tag 'Unit' { $Dependencies | Should -BeNullOrEmpty } + It 'Does not mutate the caller''s InputObject — PSDependOptions key survives after Get-Dependency' { + $inputObj = @{ + PSDependOptions = @{ Target = 'CurrentUser' } + Pester = 'latest' + } + $null = Get-Dependency -InputObject $inputObj + $inputObj.ContainsKey('PSDependOptions') | Should -Be $True + } + + It 'Does not mutate the caller''s InputObject — PSDependOptions honored on a second Get-Dependency call' { + $inputObj = @{ + PSDependOptions = @{ Target = 'CurrentUser' } + Pester = 'latest' + } + $null = Get-Dependency -InputObject $inputObj + $null = Get-Dependency -InputObject $inputObj + $inputObj.ContainsKey('PSDependOptions') | Should -Be $True + $inputObj.PSDependOptions.Target | Should -Be 'CurrentUser' + } + It 'Parses -InputObject hashtable as PSGalleryModule by default' { $Dependencies = Get-Dependency -InputObject @{ Pester = 'latest' } $Dependencies.DependencyName | Should -Be 'Pester' From cd1fcb42f1c47e51d41fcc5c2a36271222636271 Mon Sep 17 00:00:00 2001 From: Gilbert Sanchez Date: Fri, 12 Jun 2026 21:12:06 -0700 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Gilbert Sanchez --- Tests/PSDepend.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/PSDepend.Tests.ps1 b/Tests/PSDepend.Tests.ps1 index d30b593..4bf53cd 100644 --- a/Tests/PSDepend.Tests.ps1 +++ b/Tests/PSDepend.Tests.ps1 @@ -134,9 +134,10 @@ Describe "Get-Dependency PS$PSVersion" -Tag 'Unit' { Pester = 'latest' } $null = Get-Dependency -InputObject $inputObj - $null = Get-Dependency -InputObject $inputObj + $deps2 = Get-Dependency -InputObject $inputObj $inputObj.ContainsKey('PSDependOptions') | Should -Be $True $inputObj.PSDependOptions.Target | Should -Be 'CurrentUser' + ($deps2 | Where-Object DependencyName -eq 'Pester').Target | Should -Be 'CurrentUser' } It 'Parses -InputObject hashtable as PSGalleryModule by default' {