Skip to content

Commit b614f4a

Browse files
committed
Improve robustness of Get-NugetPackageDllPath to file access issues
`Test-Path` will write to the error stream if there is a file access issue, but still return back `$false`. Within this method, we simply want to test if the file exists -- not having access to the file is an equivalence case for us and should be silent. This updates `Test-Path` to use `-ErrorAction Ignore` to silence any potential errors that we don't care about. Additionally this updates `Test-AssemblyIsDesiredVersion` to also be robust to file access issues. If it has issues getting the file information, even after getting passed the `Test-Path` in the parameter validation, we want to gracefully return `$false` without undo error logging (we will write a Warning log entry though for completeness).
1 parent 9a74d73 commit b614f4a

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

NugetTools.ps1

+12-5
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,14 @@ function Test-AssemblyIsDesiredVersion
198198

199199
$splitTargetVer = $DesiredVersion.Split('.')
200200

201-
$versionInfo = (Get-Item -Path $AssemblyPath).VersionInfo
201+
$file = Get-Item -Path $AssemblyPath -ErrorVariable ev
202+
if (($null -ne $ev) -and ($ev.Count -gt 0))
203+
{
204+
Write-Log "Problem accessing [$Path]: $($ev[0].Exception.Message)" -Level Warning
205+
return $false
206+
}
207+
208+
$versionInfo = $file.VersionInfo
202209
$splitSourceVer = @(
203210
$versionInfo.ProductMajorPart,
204211
$versionInfo.ProductMinorPart,
@@ -300,7 +307,7 @@ function Get-NugetPackageDllPath
300307

301308
# First we'll check to see if the user has cached the assembly into the module's script directory
302309
$moduleAssembly = Join-Path -Path $PSScriptRoot -ChildPath $AssemblyName
303-
if (Test-Path -Path $moduleAssembly -PathType Leaf)
310+
if (Test-Path -Path $moduleAssembly -PathType Leaf -ErrorAction Ignore)
304311
{
305312
if (Test-AssemblyIsDesiredVersion -AssemblyPath $moduleAssembly -DesiredVersion $NugetPackageVersion)
306313
{
@@ -318,7 +325,7 @@ function Get-NugetPackageDllPath
318325
if (-not [System.String]::IsNullOrEmpty($alternateAssemblyPath))
319326
{
320327
$assemblyPath = Join-Path -Path $alternateAssemblyPath -ChildPath $AssemblyName
321-
if (Test-Path -Path $assemblyPath -PathType Leaf)
328+
if (Test-Path -Path $assemblyPath -PathType Leaf -ErrorAction Ignore)
322329
{
323330
if (Test-AssemblyIsDesiredVersion -AssemblyPath $assemblyPath -DesiredVersion $NugetPackageVersion)
324331
{
@@ -340,7 +347,7 @@ function Get-NugetPackageDllPath
340347
else
341348
{
342349
$cachedAssemblyPath = Join-Path -Path $(Join-Path $script:tempAssemblyCacheDir $AssemblyPackageTailDirectory) $AssemblyName
343-
if (Test-Path -Path $cachedAssemblyPath -PathType Leaf)
350+
if (Test-Path -Path $cachedAssemblyPath -PathType Leaf -ErrorAction Ignore)
344351
{
345352
if (Test-AssemblyIsDesiredVersion -AssemblyPath $cachedAssemblyPath -DesiredVersion $NugetPackageVersion)
346353
{
@@ -359,7 +366,7 @@ function Get-NugetPackageDllPath
359366
Get-NugetPackage -PackageName $NugetPackageName -Version $NugetPackageVersion -TargetPath $script:tempAssemblyCacheDir -NoStatus:$NoStatus
360367

361368
$cachedAssemblyPath = Join-Path -Path $(Join-Path -Path $script:tempAssemblyCacheDir -ChildPath $AssemblyPackageTailDirectory) -ChildPath $AssemblyName
362-
if (Test-Path -Path $cachedAssemblyPath -PathType Leaf)
369+
if (Test-Path -Path $cachedAssemblyPath -PathType Leaf -ErrorAction Ignore)
363370
{
364371
Write-Log -Message @(
365372
"To avoid this download delay in the future, copy the following file:",

0 commit comments

Comments
 (0)