This one has me thoroughly confused. In some scenarios, running a script that dot-sources another script that defines a class does not reflect changes to the class. Here's the repro.
Script common.ps1:
class A
{
A()
{
Write-Host 'hi'
}
}
Script myscript.ps1:
. .\common.ps1
[A]::new() | out-null
In a powershell terminal, I then run
. .\common.ps1
[A]::new() | out-null
.\myscript.ps1
outputs
hi
hi
I then change common.ps1 by replacing 'hi' with 'bye', and again run
. .\common.ps1
[A]::new() | out-null
.\myscript.ps1
To get this output (my comments added):
bye # expected
hi # what?!?!
The fact that the change is not-reflected when myscript.ps1 does the dot sourcing but is when the dot sourcing is done locally blows my mind. If I don't dot-source locally, running myscript.ps1 always reflects any changes to common.ps1. And it is only when my change is to some part of the class that it does not have an effect (I can add a Write-Host outside of the class and it will show up).
What's going on? Somehow myscript.ps1 can't load the updated class even though the local shell can? I imagine this is related to this question but the inconsistency between local shell and myscript.ps1 has me scratching my head.