Skip to content

Export-Image.ps1 compatibility with PowerShell 5.1#31

Open
NeuroThom wants to merge 1 commit into
bonsai-rx:mainfrom
NeuroThom:issue30
Open

Export-Image.ps1 compatibility with PowerShell 5.1#31
NeuroThom wants to merge 1 commit into
bonsai-rx:mainfrom
NeuroThom:issue30

Conversation

@NeuroThom

Copy link
Copy Markdown

Fix Export-Image.ps1 compatibility with Windows PowerShell 5.1

The script previously required users to install PS7 separately, discussed in #30. It's possible that documenting the powershell 7+ requirement is preferable, but I felt I had to fix this for a ucl-open project, so thought to update it so it work out of the box with the PowerShell 5.1 that ships with Windows and VS Code.

Changes:

  • Replaced Set-StrictMode -Version 3.0 with 2.0
  • Replaced [IO.Path]::GetRelativePath() (.NET 5+ only)
  • Removed $PSNativeCommandUseErrorActionPreference: added an explicit $LASTEXITCODE check after the Bonsai.exe call to interupt if error occurs.

Fixes #30

@CLAassistant

CLAassistant commented Mar 9, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@glopesdev glopesdev left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NeuroThom I understand the frustration having hit this myself early on. However, installing PS 7 is simple using winget and otherwise only GitHub actions need to worry with this, and they all have PS7 pre-installed.

Still happy to consider this, but would be important to make sure all edge cases @PathogenDavid put in place are covered, so we are not inadvertently regressing other issues. I tried to review as best I could below.

Comment thread modules/Export-Image.ps1
$bootstrapperArgs += "$workflowFile"

if (!$IsWindows) {
$isWindowsPlatform = if ($null -eq (Get-Variable 'IsWindows' -ErrorAction SilentlyContinue)) { $true } else { $IsWindows }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NeuroThom why was it necessary to redefine this part of the implementation? It wasn't clear from the PR description.

Comment thread modules/Export-Image.ps1
$ErrorActionPreference = 'Stop'
$PSNativeCommandUseErrorActionPreference = $true

function Get-RelativePath([string]$basePath, [string]$targetPath) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolve-Path has a built-in replacement for this which does work on PS 5.0+ so we likely don't need to define a new function.

Resolve-Path -Relative abspath

Comment thread modules/Export-Image.ps1

Write-Verbose "$($bootstrapperPath) $($bootstrapperArgs)"
&$bootstrapperPath $bootstrapperArgs
if ($LASTEXITCODE -ne 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference in error output with PSNativeCommandUseErrorActionPreference versus this error check? Do you still get the error stack trace printed by the Bonsai bootstrapper?

A good way to check would be to get a workflow which fails to export and check the output of one version versus the other.

Comment thread modules/Export-Image.ps1
foreach ($workflowFile in Get-ChildItem -File -Recurse (Join-Path $workflowPath "*.bonsai")) {
$svgPath = Join-Path $workflowFile.DirectoryName "$($workflowFile.BaseName).svg"
$svgPathRelative = [IO.Path]::GetRelativePath($documentationRoot, $svgPath)
$svgPathRelative = Get-RelativePath $documentationRoot $svgPath

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like PS 5+ does not have -RelativeBasePath and always works relative to the current directory so we would need to push and pop location:

Suggested change
$svgPathRelative = Get-RelativePath $documentationRoot $svgPath
Push-Location $documentationRoot
$svgPathRelative = Resolve-Path $svgPath -Relative
Pop-Location

This could probably even be done outside the loop since we are probably fine running the script from the context of $documentationRoot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Method invocation failed because [System.IO.Path] does not contain a method named 'GetRelativePath'.

3 participants