diff --git a/resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1 b/resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1 index ac10bc33..1f935b0c 100644 --- a/resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1 +++ b/resources/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.psm1 @@ -146,7 +146,6 @@ function Invoke-VSCode { $stdErrTempFile = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath (New-Guid).Guid $stdOutTempFile = Join-Path -Path ([System.IO.Path]::GetTempPath()) -ChildPath (New-Guid).Guid - $invocationSuccess = $true $processParams = @{ FilePath = $VSCodeCLIPath @@ -160,14 +159,14 @@ function Invoke-VSCode { $invocation = Start-Process @processParams $invocationErrors = Get-Content $stdErrTempFile -Raw -ErrorAction SilentlyContinue - $invocationErrors = $invocationErrors -replace '\n', '\n ' $invocationOutput = Get-Content $stdOutTempFile -ErrorAction SilentlyContinue Remove-Item -Path $stdErrTempFile -ErrorAction Ignore Remove-Item -Path $stdOutTempFile -ErrorAction Ignore - if (![string]::IsNullOrWhiteSpace($invocationErrors)) { $invocationSuccess = $false } - if ($invocation.ExitCode) { $invocationSuccess = $false } - if (!$invocationSuccess) { throw [System.Configuration.ConfigurationException]::new("Executing '$VSCodeCLIPath $Command' failed. Command Output: '$invocationErrors'") } + if ($invocation.ExitCode -ne 0) { + $details = if ([string]::IsNullOrWhiteSpace($invocationErrors)) { $invocationOutput -join [System.Environment]::NewLine } else { $invocationErrors.Trim() } + throw [System.Configuration.ConfigurationException]::new("Executing '$VSCodeCLIPath $Command' failed with exit code $($invocation.ExitCode). Command Output: '$details'") + } return $invocationOutput } diff --git a/resources/NpmDsc/NpmDsc.psm1 b/resources/NpmDsc/NpmDsc.psm1 index 572e69c9..f6c0b9de 100644 --- a/resources/NpmDsc/NpmDsc.psm1 +++ b/resources/NpmDsc/NpmDsc.psm1 @@ -128,8 +128,11 @@ function GetNpmPath { } elseif (Test-Path $globalNpmCacheDir -ErrorAction SilentlyContinue) { return $globalNpmCacheDir } else { - $result = (Invoke-Npm -Command @('config', 'list', '--json') | ConvertFrom-Json -ErrorAction SilentlyContinue).cache - if (Test-Path $result -ErrorAction SilentlyContinue) { + # Call 'npm' directly rather than through Invoke-Npm: this is an error-reporting helper + # and Invoke-Npm's failure path calls back into GetNpmPath, which would recurse indefinitely. + $cacheRoot = (& npm config list --json --logs-max=0 2>$null | ConvertFrom-Json -ErrorAction SilentlyContinue).cache + $result = if ($cacheRoot) { Join-Path $cacheRoot '_logs' } else { $null } + if ($result -and (Test-Path $result -ErrorAction SilentlyContinue)) { return $result } else { return $null @@ -428,15 +431,19 @@ class NpmPackage { [string] WhatIf() { if ($this.Ensure -eq [Ensure]::Present) { - $whatIfState = Install-NpmPackage -PackageName $this.Name -Global $this.Global -Arguments @('--dry-run') - $out = @{ Name = $this.Name _metaData = @{ whatif = @() } } - $out._metaData.whatif = $LASTEXITCODE -ne 0 ? (GetNpmWhatIfResponse) : ($whatIfState | Where-Object { $_.Trim() -ne '' }) # Removes empty lines from response + + try { + $whatIfState = Install-NpmPackage -PackageName $this.Name -Global $this.Global -Arguments @('--dry-run') + $out._metaData.whatif = $whatIfState | Where-Object { $_.Trim() -ne '' } # Removes empty lines from response + } catch { + $out._metaData.whatif = GetNpmWhatIfResponse + } } else { # Uninstall does not have --dry-run param $out = @{}