From ee4e3f22f7d693079c0a971940225580f21b3883 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 9 Jul 2020 18:16:28 -0700 Subject: [PATCH 1/6] Fix constructing PSModulePath if a sub-path has trailing separator --- .../engine/Modules/ModuleIntrinsics.cs | 24 +++++++++---------- .../engine/Module/ModulePath.Tests.ps1 | 8 +++++++ 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs index e8b76de4837..cab7ed6c083 100644 --- a/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs +++ b/src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs @@ -1139,7 +1139,7 @@ private static string AddToPath(string basePath, string pathToAdd, int insertPos int position = PathContainsSubstring(result.ToString(), subPathToAdd); // searching in effective 'result' value ensures that possible duplicates in pathsToAdd are handled correctly if (position == -1) // subPathToAdd not found - add it { - if (insertPosition == -1) // append subPathToAdd to the end + if (insertPosition == -1 || insertPosition > basePath.Length) // append subPathToAdd to the end { bool endsWithPathSeparator = false; if (result.Length > 0) @@ -1223,23 +1223,23 @@ public static string GetModulePath(string currentProcessModulePath, string hklmM // personalModulePath // sharedModulePath // systemModulePath - int insertIndex = 0; - if (!string.IsNullOrEmpty(personalModulePathToUse)) + currentProcessModulePath = AddToPath(currentProcessModulePath, personalModulePathToUse, 0); + int insertIndex = currentProcessModulePath.IndexOf(Path.PathSeparator, PathContainsSubstring(currentProcessModulePath, personalModulePathToUse)); + if (insertIndex != -1) { - currentProcessModulePath = AddToPath(currentProcessModulePath, personalModulePathToUse, insertIndex); - insertIndex = PathContainsSubstring(currentProcessModulePath, personalModulePathToUse) + personalModulePathToUse.Length + 1; + // advance past the path separator + insertIndex++; } - if (!string.IsNullOrEmpty(sharedModulePath)) + currentProcessModulePath = AddToPath(currentProcessModulePath, sharedModulePath, insertIndex); + insertIndex = currentProcessModulePath.IndexOf(Path.PathSeparator, PathContainsSubstring(currentProcessModulePath, sharedModulePath)); + if (insertIndex != -1) { - currentProcessModulePath = AddToPath(currentProcessModulePath, sharedModulePath, insertIndex); - insertIndex = PathContainsSubstring(currentProcessModulePath, sharedModulePath) + sharedModulePath.Length + 1; + // advance past the path separator + insertIndex++; } - if (!string.IsNullOrEmpty(systemModulePathToUse)) - { - currentProcessModulePath = AddToPath(currentProcessModulePath, systemModulePathToUse, insertIndex); - } + currentProcessModulePath = AddToPath(currentProcessModulePath, systemModulePathToUse, insertIndex); } return currentProcessModulePath; diff --git a/test/powershell/engine/Module/ModulePath.Tests.ps1 b/test/powershell/engine/Module/ModulePath.Tests.ps1 index b4dd5add437..31efd6d7e7a 100644 --- a/test/powershell/engine/Module/ModulePath.Tests.ps1 +++ b/test/powershell/engine/Module/ModulePath.Tests.ps1 @@ -187,4 +187,12 @@ Describe "SxS Module Path Basic Tests" -tags "CI" { Remove-Item -Path $userConfigPath -Force } } + + It 'Path with trailing separator' { + $env:PSModulePath = $env:PSModulePath.Replace($expectedUserPath, $expectedUserPath + [System.IO.Path]::PathSeparator) + $out = pwsh -noprofile -command '$env:PSModulePath' + $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries).ForEach{ + $_ | Should -Exist + } + } } From c5e48415a32c2c1c2a68122db682dd4843b7a36e Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 9 Jul 2020 18:49:18 -0700 Subject: [PATCH 2/6] skip test on Linux --- test/powershell/engine/Module/ModulePath.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/powershell/engine/Module/ModulePath.Tests.ps1 b/test/powershell/engine/Module/ModulePath.Tests.ps1 index 31efd6d7e7a..d9acee216f7 100644 --- a/test/powershell/engine/Module/ModulePath.Tests.ps1 +++ b/test/powershell/engine/Module/ModulePath.Tests.ps1 @@ -188,7 +188,8 @@ Describe "SxS Module Path Basic Tests" -tags "CI" { } } - It 'Path with trailing separator' { + # Some paths are not pre-created on Linux + It 'Path with trailing separator' -Skip:$IsLinux { $env:PSModulePath = $env:PSModulePath.Replace($expectedUserPath, $expectedUserPath + [System.IO.Path]::PathSeparator) $out = pwsh -noprofile -command '$env:PSModulePath' $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries).ForEach{ From 6af8439e7cb525f37c87706391ffa4be58af959e Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 9 Jul 2020 22:17:06 -0700 Subject: [PATCH 3/6] Removed test as the system env var needs to be modified to repro --- test/powershell/engine/Module/ModulePath.Tests.ps1 | 9 --------- 1 file changed, 9 deletions(-) diff --git a/test/powershell/engine/Module/ModulePath.Tests.ps1 b/test/powershell/engine/Module/ModulePath.Tests.ps1 index d9acee216f7..b4dd5add437 100644 --- a/test/powershell/engine/Module/ModulePath.Tests.ps1 +++ b/test/powershell/engine/Module/ModulePath.Tests.ps1 @@ -187,13 +187,4 @@ Describe "SxS Module Path Basic Tests" -tags "CI" { Remove-Item -Path $userConfigPath -Force } } - - # Some paths are not pre-created on Linux - It 'Path with trailing separator' -Skip:$IsLinux { - $env:PSModulePath = $env:PSModulePath.Replace($expectedUserPath, $expectedUserPath + [System.IO.Path]::PathSeparator) - $out = pwsh -noprofile -command '$env:PSModulePath' - $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries).ForEach{ - $_ | Should -Exist - } - } } From 00e14105da4954252e7b4937a36973adea7442f0 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Wed, 22 Jul 2020 21:33:12 -0700 Subject: [PATCH 4/6] Add test --- test/powershell/engine/Module/ModulePath.Tests.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/powershell/engine/Module/ModulePath.Tests.ps1 b/test/powershell/engine/Module/ModulePath.Tests.ps1 index b4dd5add437..6befea50edc 100644 --- a/test/powershell/engine/Module/ModulePath.Tests.ps1 +++ b/test/powershell/engine/Module/ModulePath.Tests.ps1 @@ -187,4 +187,11 @@ Describe "SxS Module Path Basic Tests" -tags "CI" { Remove-Item -Path $userConfigPath -Force } } + + It 'User PSModulePath has trailing separator' -Skip:(!$IsWindows) { + $newUserPath = Join-Path $expectedUserPath ([System.IO.Path]::DirectorySeparatorChar) + $env:PSModulePath = $env:PSModulePath.Replace($expectedUserPath, $newUserPath).Replace($expectedSharedPath,"") + $out = & $powershell -noprofile -command '$env:PSModulePath' + $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | Should -Exist + } } From c3cc9de4dacdeb89774b496033433f665eebef63 Mon Sep 17 00:00:00 2001 From: "Steve Lee (POWERSHELL)" Date: Thu, 23 Jul 2020 05:39:11 -0700 Subject: [PATCH 5/6] Fix test by comparing against incorrectly created path --- test/powershell/engine/Module/ModulePath.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/powershell/engine/Module/ModulePath.Tests.ps1 b/test/powershell/engine/Module/ModulePath.Tests.ps1 index 6befea50edc..99d310b3ab3 100644 --- a/test/powershell/engine/Module/ModulePath.Tests.ps1 +++ b/test/powershell/engine/Module/ModulePath.Tests.ps1 @@ -192,6 +192,6 @@ Describe "SxS Module Path Basic Tests" -tags "CI" { $newUserPath = Join-Path $expectedUserPath ([System.IO.Path]::DirectorySeparatorChar) $env:PSModulePath = $env:PSModulePath.Replace($expectedUserPath, $newUserPath).Replace($expectedSharedPath,"") $out = & $powershell -noprofile -command '$env:PSModulePath' - $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | Should -Exist + $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | Should -Not -BeLike "*\$env:SystemDrive\*" } } From 183114a97d137b7146b8cc54d6cd6b9a175ef507 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 23 Jul 2020 15:33:25 -0700 Subject: [PATCH 6/6] Update test to be cross platform --- test/powershell/engine/Module/ModulePath.Tests.ps1 | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/powershell/engine/Module/ModulePath.Tests.ps1 b/test/powershell/engine/Module/ModulePath.Tests.ps1 index 99d310b3ab3..a18b4fc3ffa 100644 --- a/test/powershell/engine/Module/ModulePath.Tests.ps1 +++ b/test/powershell/engine/Module/ModulePath.Tests.ps1 @@ -188,10 +188,17 @@ Describe "SxS Module Path Basic Tests" -tags "CI" { } } - It 'User PSModulePath has trailing separator' -Skip:(!$IsWindows) { + It 'User PSModulePath has trailing separator' { + if ($IsWindows) { + $validation = "*\$env:SystemDrive\*" + } + else { + $validation = "*//*" + } + $newUserPath = Join-Path $expectedUserPath ([System.IO.Path]::DirectorySeparatorChar) $env:PSModulePath = $env:PSModulePath.Replace($expectedUserPath, $newUserPath).Replace($expectedSharedPath,"") $out = & $powershell -noprofile -command '$env:PSModulePath' - $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | Should -Not -BeLike "*\$env:SystemDrive\*" + $out.Split([System.IO.Path]::PathSeparator, [System.StringSplitOptions]::RemoveEmptyEntries) | Should -Not -BeLike $validation } }