PowerShell / PSResourceGet

PSResourceGet is the package manager for PowerShell
https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet
MIT License
484 stars 92 forks source link

`Get-PSResource` does not search in user context environment variable `PSModulePath` #889

Open o-l-a-v opened 1 year ago

o-l-a-v commented 1 year ago

Prerequisites

Steps to reproduce

PackageManagement, PowerShellGetv2 and PowerShellGet 3.0.18-beta (and prior versions) does not search in directorories specified in user context environmental variable PSModulePath when searching for modules. But at least v3 has a -Path parameter.

Reproduce:

Microsoft.PowerShell.Core\Import-Module and Get-Module works as expected.

Expected behavior

PowerShellGet\Get-PSResource should search in all PSModulePath paths, also those specified in user context environmental variable.

Actual behavior

It does not.

Error details

No response

Environment data

PowerShellGet v3.0.18
Windows PowerShell 5.1 x64 on Windows 10 22H2

Visuals

No response

o-l-a-v commented 5 months ago

Here's the responsible code as far as I can see:

https://github.com/PowerShell/PSResourceGet/blob/b28fba0fecd6736bad1a40640b9814b2bbf27cf5/src/code/Utils.cs#L993-L1017

Seems doable to add PSModulePath here?

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && scope is not null) {
    Environment.GetEnvironmentVariable(
        "PSModulePath",
        scope.Value is ScopeType.CurrentUser ? EnvironmentVariableTarget.User : EnvironmentVariableTarget.Machine
    );
}

I understand it's more complex than that. One must have tests etc.

Is this one up for grabs, or do you not want environment variable PSModulePath to have any say?


Edit: Here's a PowerShellers attempt. 😁

Click to view ```csharp if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && scope is not null) { string PSModulePath = Environment.GetEnvironmentVariable( "PSModulePath", scope.Value is ScopeType.CurrentUser ? EnvironmentVariableTarget.User : EnvironmentVariableTarget.Machine ); if (string.IsNullOrEmpty(PSModulePath)) { psCmdlet.WriteVerbose("Found no value for PSModulePath given the scope."); } else { foreach (string element in PSModulePath.Split(Path.PathSeparator)) { if (Directory.Exists(element)) { if ( string.Equals( element.Split(Path.DirectorySeparatorChar).Last(), "Modules", StringComparison.OrdinalIgnoreCase ) ) { psCmdlet.WriteVerbose( string.Format( "'{0}' exists, adding it to 'resourcePaths'.", element ) ); resourcePaths.Add(element); resourcePaths.Add(Path.Combine(Directory.GetParent(element).FullName, "Scripts")); } else { psCmdlet.WriteVerbose( string.Format( "'{0}' exists but does not match expected pattern, not adding it to 'resourcePaths'.", element ) ); } } else { psCmdlet.WriteVerbose( string.Format( "'{0}' does not exist, not adding it to 'resourcePaths'.", element ) ); } } } } ```