Scripts unable to find $PSScriptRoot after upgrade to 2020.6.4722

We have a deployment that is bootstrapped together with Cake scripts and Powershell. Since upgrading to Octopus 2020.6.4722 last Friday, we are unable to deploy that project. I’ve been troubleshooting the scripts and found the $PSScriptRoot variable as at least one of the culprits. I added a line to concatenate the $PSScriptRoot to the -ChildPath of the folder using Join-Path. Now I’m finding more missing folders in the deployment. Is this a known bug in that release of Octopus? If so, is there a workaround or fix?

This used to work
[Parameter()][string]$sourcePackagePath = “$($PSScriptRoot)…\UpdatePackages”

Now I had to add this statement in the script
$sourcePackagePath = Join-Path $PSScriptRoot -ChildPath $sourcePackagePath

Hi @cmuesing,

Thanks for reaching out.

Which folders are you finding that are missing in your deployment?

Would you be able to give me more details about the step that’s running into this issue as well as what your end goal with the step is?

It sounds like you are running a script inside of a package and trying to reference the script location within the script to set a location, but I want to be sure.

Do you know what version you were on previous to this where it worked?

If possible, can you send me screenshots of your step as well as attach the script itself as a ps1/txt so I can attempt to reproduce the behavior on my end? Please feel free to DM me these for privacy reasons if necessary.

Please let me know if you have any questions.

Best,
Jeremy

Jeremy, we were on version 2019.9.12. And yes this is a package deploy trying to run a script within the package and attempting to get to the script location and various folders relative to it. This is the first error we saw and it was trying to get to the UpdatePackages folder.
The structure is running a ps1 under the Scripts folder, trying to get back to UpdatePackages a the same level as the Scripts folder.

This is just 1 of our scripts using that variable and after I fixed this one with the Join-Path command, it proceeded to several others that are now broken also.
I wonder if the temp folder name under the Octopus\work is what is missing now.

image

Hi @cmuesing,

Thanks for all of the information.

I’ve set this up on 2021.1.6460 and it appears to work for me.

Please take a look at my setup to see if I’ve possibly missed something in my reproduction below.

The package is called psscriptroot with a script inside of it called script.ps1.

Script contents:

[Parameter()][string]$sourcePackagePath = "$($PSScriptRoot)\..\UpdatePackages\"

Write-Host $sourcePackagePath

$sourcePackagePath = Join-Path $PSScriptRoot -ChildPath $sourcePackagePath

Write-Host $sourcePackagePath

Run a Script Step Setup:

Output of Script:

19:41:53   Info     |       C:\Octopus\CloudPollingJeremyMSpace\Work\20210415194147-67764-54\..\UpdatePackages\
19:41:53   Info     |       C:\Octopus\CloudPollingJeremyMSpace\Work\20210415194147-67764-54\C:\Octopus\CloudPollingJeremyMSpace\Work\20210415194147-67764-54\..\UpdatePackages\

As you can, see the first line correctly points to the subfolder within the working directory as indicated, and the second results in a duplicate path.

Please let me know if I missed something in my reproduction. If not, I can try to spin up your identical version and repeat the steps above.

Best,
Jeremy

This looks close and can you add a write-Host of the $PSScriptRoot? I used the same variable to concatenate the PSScriptRoot to, so that the rest of the script would have the correct value in it. Your example is working with the full path, so when you concatenate it to itself, it does double it.

Hi @cmuesing,

I altered the script to be:

[Parameter()][string]$sourcePackagePath = "$($PSScriptRoot)\..\UpdatePackages\"

Write-Host $sourcePackagePath

Write-Host "================"

$sourcePackagePath = Join-Path $PSScriptRoot -ChildPath $sourcePackagePath

Write-Host $sourcePackagePath

Write-Host "================"

Write-Host $PSScriptRoot

The output is:

20:18:00   Info     |       C:\Octopus\CloudPollingJeremyMSpace\Work\20210415201755-67775-60\..\UpdatePackages\
20:18:00   Info     |       ================
20:18:00   Info     |       C:\Octopus\CloudPollingJeremyMSpace\Work\20210415201755-67775-60\C:\Octopus\CloudPollingJeremyMSpace\Work\20210415201755-67775-60\..\UpdatePackages\
20:18:00   Info     |       ================
20:18:00   Info     |       C:\Octopus\CloudPollingJeremyMSpace\Work\20210415201755-67775-60

Am I missing anything from my repro that I should add in?

Please let me know.

Best,
Jeremy

Is that your result in 2020.6.4722?

Hi @cmuesing,

No, sorry, this is still 2021.1.6460. I wanted to make sure I wasn’t missing anything in my reproduction by confirming my steps with you. Everything is identical on your side? Which version of PowerShell is the worker/tentacle running?

Best,
Jeremy

Tentacle is running Powershell 5.1.14393.4350

Hi @cmuesing,

Thanks for confirming your PowerShell version.

I’m going to spin up the exact copy of Octopus you are running and try the process over again. I will let you know my results as soon as I have them.

Please feel free to reach out in the meantime with any other questions or concerns.

Best,
Jeremy

Hey @cmuesing,

I spun up the same package and step on your version and here are the results which appear to be working:

Script:

[Parameter()][string]$sourcePackagePath = "$($PSScriptRoot)\..\UpdatePackages\"

Write-Host $sourcePackagePath

Write-Host "================"

$sourcePackagePath = Join-Path $PSScriptRoot -ChildPath $sourcePackagePath

Write-Host $sourcePackagePath

Write-Host "================"

Write-Host $PSScriptRoot

Please let me know if you think I’ve missed any element in my repro that might be causing this for you as it appears to be working for me.

Best,
Jeremy

Thank you for attempting to recreate it. I have modified all our scripts to reset the variables using the Join-Path of $PSScriptRoot and the -ChildPath that was originally intended by the concatenation. I removed the Join-Path that used the -ChildPath of the whole original variable. That prevents the duplication, but still doesn’t explain why this stopped working with this Octopus upgrade. Since you have proven the Octopus release itself is not the culprit, I won’t immediately upgrade to the latest LTS. Thank you for your help Jeremy. I wish I had a warm fuzzy about the resolution, but at least it is resolved for now.

Hi @cmuesing,

This feels like it may be a PowerShell issue on the local box.

What happens if you copy paste my script above and save it as a ps1 on the local box and run it? Do you get the correct output or do you also get unexpected paths there?

If I don’t hear from you, I hope you have a great weekend.

Best,
Jeremy

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.