Gotcha’s when developing VSTS Build Extension
I recently posted on my development process for VSTS Extensions, it has been specifically PowerShell based build ones I have been working on. During this development I have come across a few more gotcha’s that I think are worth mentioning
32/64 bit
The VSTS build agent launches PowerShell 64bit (as does the PowerShell command line on dev PC), but VSCode launches it 32bit. Whilst working my StyleCop extension this caused me a problem as StyleCop it seems can only load dictionaries for spell checking based rules when in a 32bit shell. So my Pester tests for the extension worked in VSCode but failed at the command line and within a VSTS build
After many hours my eventual solution was to put some guard code in my scripts to force a reload in 32bit mode
1param
2(
3 \[string\]$treatStyleCopViolationsErrorsAsWarnings,
4 \[string\]$maximumViolationCount,
5 … other params
6)
7
8if ($env:Processor\_Architecture -ne "x86")
9{
10 # Get the command parameters
11 $args = $myinvocation.BoundParameters.GetEnumerator() | ForEach-Object {$($\_.Value)}
12 write-warning 'Launching x86 PowerShell'
13 &"$env:windirsyswow64windowspowershellv1.0powershell.exe" -noprofile -executionpolicy bypass -file $myinvocation.Mycommand.path $args
14 exit
15}
16write-verbose "Running in $($env:Processor\_Architecture) PowerShell"
17
18... rest of my code
The downside of this trick is that you can’t pass return values back as you swapped execution process. For the type of things I am doing with VSTS tasks this not an issue as the important data has usually be dropped to a file which is accessible by everything, such as test results.
For a worked sample of production code and Pester tests see by GitHub repo.
Using Modules
In the last post I mentioned the problem when trying to run Pester tests against scripts, the script content is executed. I stupidly did not mention the obvious solution of moving all the code into functions in a PowerShell modules. This makes it easier to write tests for all bar the outer wrapper .PS1 script that is called by the VSTS agent.
Again see by GitHub repo so a good sample. Note how I have split out the files so that I have
- A module that contains the functions I can test via Pester
- A .PS1 script called by VSTS (this will run 64bit) where I deal with interaction with VSTS/TFS
- An inner PS1 string that we force into 32bit mode as needed (see above)
Hacking around on your code
You always get to the point I find when developing things like VSTS build tasks that you want to make some quick change to try something without the full development/build/release cycle. This is in effect the local development stage, it is just build task development makes with awkward. It is hard to fully test a task locally, it need to be deployed within a build
I have found a way to help here is to use a local build agent, you can then get at the deployed task and edit the .PS1 code. The important bit to node is that the task will not be redeployed so you local ‘hack’ can be tested within a real TFS build without having to increment the task’s version and redeploy.
Hacky but handy to know.
You of course do need to make sure you hacked code is eventually put through your formal release process.
And maybe something or nothings…
I may have seen these issues, but have not got to the bottom of them, so they may not be real issues
- The order parameters are declared in a task.json file seems to need to match the order they are declared in the .PS1 file call. I had thought they we associated by name not order, but in one task they all got transposed until I fixed the order.
- The F5 dev debug cycle is still a little awkward with VSCode, sometime it seems to leave stuff running and you get high CPU utilisation – just restart the VSCode - the old fix!
- If using the 32 bit relaunch discussed above write-verbose messages don’t awlays seem to show up in the VSTS log, I assume a –verbose parameter is being lost somewhere, or it is the spawning of another PowerShell instance that cause the problem.
SO again I hope these tips help with your VSTS extension development