Skip to content

[Bug] Add-Path does not add multiple values if just one is in TargetEnvVar #6710

Description

@xworld21

Bug Report

There's a logic bug in the following lines:

Scoop/lib/system.ps1

Lines 106 to 120 in b588a06

$inPath, $strippedPath = Split-PathLikeEnvVar $Path (Get-EnvVar -Name $TargetEnvVar -Global:$Global)
if (!$inPath -or $Force) {
if (!$Quiet) {
$Path | ForEach-Object {
Write-Host "Adding $(friendly_path $_) to $(if ($Global) {'global'} else {'your'}) path."
}
}
Set-EnvVar -Name $TargetEnvVar -Value ((@($Path) + $strippedPath) -join ';') -Global:$Global
}
# current session
$inPath, $strippedPath = Split-PathLikeEnvVar $Path $env:PATH
if (!$inPath -or $Force) {
$env:PATH = (@($Path) + $strippedPath) -join ';'
}
}

The return value $inPath of the function Split-PathLikeEnvVar is the string containing the components of $Path already in TargetEnvVar, so when given multiple strings, the if (!$Path test will be false when any of the components were already present, rather than when all the components were present.

Current Behavior

Calling Add-Path with multiple paths, if only one of them is in the variable, the other is not added.

Expected Behavior

When calling Add-Path with multiple paths, if any of them is in not in the variable, they are all added back.

Additional context/output

Noticed while updating #6580.

Possible Solution

Split-PathLikeEnvVar should return two lists rather than two strings, and Add-Path should then prepend the items that are not $inPath, which incidentally is also a better solution to the problem raised in #6580 (comment).

System details

Windows version: 11

OS architecture: 64bit

PowerShell version: 7.6.4

Scoop Configuration

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions