My project is structured like this:
MyScript.ps1
classes\
Car.ps1
Tesla.ps1
Car.ps1 is the base class of Tesla.ps1. I attempt to define Tesla like this in Tesla.ps1:
. "$PSScriptRoot\Car.ps1"
class Tesla : Car
{
}
MyScript.ps1 needs to use the Tesla class, but shouldn't need to know that it inherits from Car.
. "$PSScriptRoot\classes\Tesla.ps1"
$tesla = [Tesla]::new()
Dot sourcing to classes\Tesla.ps1
works fine, but this error is thrown from the Tesla file:
Unable to find type [Car]
If I import all the files in the correct order in MyScript.ps1, it works fine. Example:
. "$PSScriptRoot\classes\Car.ps1"
. "$PSScriptRoot\classes\Tesla.ps1"
$tesla = [Tesla]::new()
This is cumbersome, especially as the complexity grows. Am I dot sourcing incorrectly? Is there a better way to import a custom PowerShell class using a relative path that isn't in the PSModulePath?