fix(Import-PSDependModule): Sanitize version string used for Import-Module#140
Conversation
|
One could argue that this change should be hoisted up to |
|
The first error that is reported is |
|
@johlju, Yes, you are correct, but that's because PowerShell has no idea about pre-release versions. The If you install a prerelease version, let's say MyModule, version 2.0.0-preview1, here's the directory structure you will find of the installed module: Notice the folder for the version--there is no pre-release tag there. |
|
Yes, correct. The preview release string is in the module manifest. |
|
Exactly. So, because PSDepend was passing the full SemVer version string as the Looking at PowerShell Core, I don't see that that has changed. One thing I want to research once more, is it possible to specify a pre-release version string in a full "module declaration" when importing? But IIRC, I don't believe there is. |
|
Per your other comment regarding |
02e3d86 to
6998b75
Compare
…odule
This commit fixes one particular issue out of two identified issues.
The first identified issue is that when initially "getting" a
dependency, it does not seem to be imported. (I may be mistaken on this
point, but read on.) The second issue, and the one this commit
addresses, is that upon a subsequent run of PSDepend, the now
"installed" module is attempted to be imported, and this fails in the
case of modules whose PSDepend specification's version string includes a
pre-release version tag.
For example, the following spec:
```powershell
@{
'PSResourceGet' = @{
Name = 'Micrsooft.PowerShell.PSResourceGet'
Version = 'latest'
Parameters = @{
AllowPreRelease = $true
}
}
}
```
The first time you run PSDepend, everything "works" fine. The reason I
say that an "install" is preformed but not an import is because there
are no errors when running this thi ferist time. But using this same
configuration, running PSDepend a subsequent time results in the
following error:
```text
Checking for and installing the latest versions of the build system dependencies...
WARNING: Access to the path 'D:\src\git\MyProject\build\deps\Microsoft.PowerShell.PSResourceGet\0.5.24' is denied.
Save-Package: Unable to save the module 'Microsoft.PowerShell.PSResourceGet'.
Import-Module: C:\Users\me\Documents\PowerShell\Modules\PSDepend\0.3.8\Private\Import-PSDependModule.ps1:21
Line |
21 | Import-Module @importParams
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The specified module 'D:\src\git\MyProject\build\deps\Microsoft.PowerShell.PSResourceGet' was not loaded because no valid module
| file was found in any module directory.
```
The issue here is that now that the module is "installed", the version
string, as specified in the PSDepend spec, is passed directly to
Import-Module via Import-PSDependModule. It is necessary to sanitize the
version string to remove any pre-release tags since Import-Module
doesn't know anything about pre-release versions. The RquiredVersion
parameter is a `System.Version` from the .NET BCL and is a
Microsoft-proprietary version number format, not a SemVer string.
This commit fixes this subsequent "import issue".
6998b75 to
4ab25b0
Compare
There was a problem hiding this comment.
Pull request overview
Updates Import-PSDependModule to handle prerelease SemVer strings when importing dependencies, addressing issue #132 where Import-Module -RequiredVersion cannot accept prerelease tags (e.g. 1.2.3-alpha).
Changes:
- Strip prerelease suffixes from the configured dependency version before passing it to
Import-Module -RequiredVersion.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if ($Version -and $Version -ne 'latest') { | ||
| $importParams.add('RequiredVersion',$Version) | ||
| # Sanitize version string. The RequiredVersion parameter is a System.Version and | ||
| # doesn't know anything about pre-release tags. | ||
| $BaseVersion = ($Version -split '-')[0] | ||
| $importParams.add('RequiredVersion',$BaseVersion) |
| $importParams.add('RequiredVersion',$Version) | ||
| # Sanitize version string. The RequiredVersion parameter is a System.Version and | ||
| # doesn't know anything about pre-release tags. | ||
| $BaseVersion = ($Version -split '-')[0] |
There was a problem hiding this comment.
You're still not loading the pre-release in this instance.
There was a problem hiding this comment.
Actually, I'm wrong. There is no -AllowPrerelease for Import. This is basically the best we can do.
This commit fixes one particular issue out of two identified issues.
The first identified issue is that when initially "getting" a dependency, it does not seem to be imported. (I may be mistaken on this point, but read on.) The second issue, and the one this commit addresses, is that upon a subsequent run of PSDepend, the now "installed" module is attempted to be imported, and this fails in the case of modules whose PSDepend specification's version string includes a pre-release version tag.
For example, the following spec:
The first time you run PSDepend, everything "works" fine. The reason I say that an "install" is performed but not an import is because there are no errors when running this the first time. But using this same configuration, running PSDepend a subsequent time results in the following error:
The issue here is that now that the module is "installed", the version string, as specified in the PSDepend spec, is passed directly to Import-Module via Import-PSDependModule. It is necessary to sanitize the version string to remove any pre-release tags since Import-Module doesn't know anything about pre-release versions. The RquiredVersion parameter is a
System.Versionfrom the .NET BCL and is a Microsoft-proprietary version number format, not a SemVer string.This commit fixes this subsequent "import issue".
Fixes #132