RamblingCookieMonster / BuildHelpers

Helper functions for PowerShell CI/CD scenarios
MIT License
214 stars 47 forks source link

Error in Azure Pipeline when Get-ProjectName returns multiple items #134

Open Jaykul opened 1 year ago

Jaykul commented 1 year ago

We're getting an error "Cannot process argument transformation on parameter 'Value'. Cannot convert value to type System.String." when Set-BuildEnvironment calls Set-AzurePipelinesVariable with the value of ProjectName which is an array.

Quick fix: since Set-AzurePipelinesVariable requires a string, you should cast the value explicitly:

https://github.com/RamblingCookieMonster/BuildHelpers/blob/5b712c4777f258b987672658af71488caa5f9ccc/BuildHelpers/Public/Set-BuildEnvironment.ps1#L126

Needs to be:

Set-AzurePipelinesVariable -Name $prefixedVar -Value "$($BuildHelpersVariables[$VarName])"

or:

Set-AzurePipelinesVariable -Name $prefixedVar -Value ([string]$BuildHelpersVariables[$VarName])

Root cause: we've been using BuildHelpers for a long time with no problems -- even on non-module projects. Today we upgraded from 2.0.3 to 2.0.16 and this new discovery case in Get-ProjectName caused the problem.

        #PSD1 in root of project but name doesn't match
        #very ick or just an icky time in Azure Pipelines
        elseif ( $PSDs = Get-ChildItem -Path $Path "*.psd1" )
        {
            if ($PSDs.count -gt 1) {
                Write-Warning "Found more than one project manifest in the root folder"
            }
            $result = $PSDs.BaseName
        }

Until now, we've been falling through to the default $result = Split-Path $Path -Leaf value, which was fine, because we're in a multi-checkout project, so our folder is named with the project name.

Now, our ScriptAnalyzerSettings.psd1 and our RequiredModules.psd1 and a couple of other similar configuration files are being treated as the project name(s) -- resulting in multiple items being returned, all of which are wrong -- and this error when the call to Set-AzurePipelinesVariable assumes a string result.

My recommendation is to fix the call to Set-AzurePipelinesVariable as I suggested above, but also to consider that if you found multiple results from looking at psd1 files, they are definitely not the right value and you should keep looking (and fall through to the folder name).

 ErrorRecord       : 
        Exception             : 
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : Cannot process argument transformation on parameter 'Value'. Cannot convert value to type System.String.
            HResult : -2146233087
        CategoryInfo          : InvalidData: (:) [Set-AzurePipelinesVariable], ParentContainsErrorRecordException
        FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-AzurePipelinesVariable
        InvocationInfo        : 
            MyCommand        : Set-AzurePipelinesVariable
            ScriptLineNumber : 126
            OffsetInLine     : 70
            HistoryId        : 1
            ScriptName       : C:\Program Files\WindowsPowerShell\Modules\BuildHelpers\2.0.16\Public\Set-BuildEnvironment.ps1
            Line             :                 Set-AzurePipelinesVariable -Name $prefixedVar -Value $BuildHelpersVariables[$VarName]
            PositionMessage  : At C:\Program Files\WindowsPowerShell\Modules\BuildHelpers\2.0.16\Public\Set-BuildEnvironment.ps1:126 char:70
                               + … esVariable -Name $prefixedVar -Value $BuildHelpersVariables[$VarName]
                               +                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            PSScriptRoot     : C:\Program Files\WindowsPowerShell\Modules\BuildHelpers\2.0.16\Public
            PSCommandPath    : C:\Program Files\WindowsPowerShell\Modules\BuildHelpers\2.0.16\Public\Set-BuildEnvironment.ps1
            CommandOrigin    : Internal
        ScriptStackTrace      : at Set-BuildEnvironment, C:\Program Files\WindowsPowerShell\Modules\BuildHelpers\2.0.16\Public\Set-BuildEnvironment.ps1: line 126
                                at <ScriptBlock>, D:\Agents\Agent1\_work\17\s\InvokeBuildTasks\Initialize.ps1: line 4
                                at <ScriptBlock>, D:\Agents\Agent1\_work\17\s\SharedInfrastructureModules\Bicep.build.ps1: line 22
                                at <ScriptBlock><End>, C:\Program Files\WindowsPowerShell\Modules\InvokeBuild\5.8.0\Invoke-Build.ps1: line 655
                                at <ScriptBlock>, D:\Agents\Agent1\_work\17\s\SharedInfrastructureModules\init.ps1: line 24
                                at <ScriptBlock>, D:\Agents\Agent1\_work\_temp\59cf28f5-e8a3-41af-b6d3-6b2866b97e99.ps1: line 3
                                at <ScriptBlock>, <No file>: line 1