RamblingCookieMonster / PSDepend

PowerShell Dependency Handler
MIT License
277 stars 75 forks source link

Ensure AddToPath is Processes When PSDepend is Rerun #48

Open doesitscript opened 6 years ago

doesitscript commented 6 years ago

The combination of AddToPath and $Existing being true causes issues when psdepend runs more than once. It looks like $Existing returns out of the script and if $PSDependAction does not contain 'Import', the module will not become available to the session For example, the first run adds the modules to the path as intended:

PSDependOptions = @{
        AddToPath = $True
        Target = 'BuildOutput\modules'
        Parameters = @{
    }
}

The following line will not run after the first run: https://github.com/RamblingCookieMonster/PSDepend/blob/264537471ca99e0f53a1145322295aa4da77474d/PSDepend/PSDependScripts/PSGalleryModule.ps1#L186

Currently I've moved the AddToPath code to occur after determining scope and things seem to work. Any thoughts on this?

# We use target as a proxy for Scope
if (-not $Dependency.Target) {
    $Scope = 'AllUsers'
}
else {
    $Scope = $Dependency.Target
}

if ($Dependency.AddToPath -and $Dependency.Target ) {
    Write-Verbose "Setting PSModulePath to`n$($($Dependency.Target), $env:PSModulePath -join ';' | Out-String)"
    Add-ToItemCollection -Reference Env:\PSModulePath -Item (Get-Item $Dependency.Target).FullName
}
RamblingCookieMonster commented 6 years ago

Hiyo!

I'd be up for adjusting this - ideally only if the action is install or import. Open to persuasion, but I'd expect the test action to be read-only and not change the environment, so maybe a logic gate on install or import?

Cheers!