Skip to content

Fixed module path not found with pre-release #2889 #2890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ What's changed since pre-release v3.0.0-B0453:
[#2824](https://github.com/microsoft/PSRule/issues/2824)
- Bump vscode engine to v1.99.1.
[#2858](https://github.com/microsoft/PSRule/pull/2858)
- Bug fixes:
- Fixed module path not found with pre-release by @BernieWhite.
[#2889](https://github.com/microsoft/PSRule/issues/2889)

## v3.0.0-B0453 (pre-release)

Expand Down
28 changes: 22 additions & 6 deletions src/PSRule/Pipeline/SourcePipelineBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@

Source(new Source(info, files, dependency: false));

// Import dependencies
// TODO: Import dependencies
//for (var i = 0; module.RequiredModules != null && i < module.RequiredModules.Count; i++)
// Module(module.RequiredModules[i], dependency: true);
}
Expand All @@ -182,15 +182,17 @@
if (_CachePath == null)
return false;

Log($"[PSRule][S] -- Searching for module in: {_CachePath}");
if (!string.IsNullOrEmpty(version))
Log($"[PSRule][S] -- Searching for module in cache: {_CachePath}");
if (!string.IsNullOrEmpty(version) && version != null)
{
path = Environment.GetRootedBasePath(Path.Combine(_CachePath, "Modules", name, version));
path = Environment.GetRootedBasePath(Path.Combine(_CachePath, "Modules", name, GetVersionPath(version)));
Log($"[PSRule][S] -- Search for module by version in: {path}");
if (File.Exists(Path.Combine(path, GetManifestName(name))))
return true;
}

path = Environment.GetRootedBasePath(Path.Combine(_CachePath, "Modules", name));
Log($"[PSRule][S] -- Search for module as version-less in: {path}");
return File.Exists(Path.Combine(path, GetManifestName(name)));
}

Expand All @@ -211,9 +213,9 @@
var searchPath = Environment.GetRootedBasePath(Path.Combine(searchPaths[i], name));

// Try a specific version.
if (!string.IsNullOrEmpty(version))
if (!string.IsNullOrEmpty(version) && version != null)
{
var versionPath = Path.Combine(searchPath, version);
var versionPath = Path.Combine(searchPath, GetVersionPath(version));
var manifestPath = Path.Combine(versionPath, GetManifestName(name));
if (File.Exists(manifestPath))
{
Expand Down Expand Up @@ -247,6 +249,20 @@
return sorted.Length > 0;
}

/// <summary>
/// Get the path for version lookups which ignore pre-release version and build segments.
/// If version is pre-release, use major.minor.patch.
/// </summary>
/// <returns>Return a version string just containing major.minor.patch.</returns>
private static string GetVersionPath(string version)
{
if (version.Contains('-') || version.Contains('+'))
{
version = version.Split('-', '+')[0];
}
return version;
}

private static string[] SortModulePath(IEnumerable<string> values)
{
var results = values.ToArray();
Expand Down
19 changes: 19 additions & 0 deletions tests/PSRule.Tests/PSRule.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,23 @@
</None>
</ItemGroup>

<!-- Cop test modules for unit tests. -->
<ItemGroup>
<Module7FilesToCopy Include="$(ProjectDir)TestModule7\TestModule7.psd1" />
<Module7FilesToCopy Include="$(ProjectDir)TestModule7\Baseline.Rule.yaml" />
<Module8FilesToCopy Include="$(ProjectDir)TestModule8\TestModule8.psd1" />
<Module8FilesToCopy Include="$(ProjectDir)TestModule8\rules\Test.Rule.ps1" />
</ItemGroup>

<Target Name="CopyFile_ForTest_Issue2889" AfterTargets="Build">
<Copy
SourceFiles="@(Module7FilesToCopy)"
DestinationFolder="$(TargetDir)Modules/TestModule7/0.0.1/"
/>
<Copy
SourceFiles="@(Module8FilesToCopy)"
DestinationFolder="$(TargetDir)Modules/TestModule8/0.0.1/"
/>
</Target>

</Project>
21 changes: 18 additions & 3 deletions tests/PSRule.Tests/SourcePipelineBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Add_directory()
var sources = builder.Build();

Assert.Single(sources);
Assert.Equal(25, sources[0].File.Length);
Assert.Equal(27, sources[0].File.Length);
}

[Fact]
Expand Down Expand Up @@ -93,7 +93,7 @@ public void Add_directory_with_disable_powershell()
var sources = builder.Build();

Assert.Single(sources);
Assert.Equal(20, sources[0].File.Length);
Assert.Equal(21, sources[0].File.Length);
}

[Fact]
Expand All @@ -106,6 +106,21 @@ public void Add_directory_with_module_only()
var sources = builder.Build();

Assert.Single(sources);
Assert.Equal(20, sources[0].File.Length);
Assert.Equal(21, sources[0].File.Length);
}

[Theory]
[InlineData("TestModule7", "0.0.1")]
[InlineData("TestModule8", "0.0.1-Alpha")]
public void ModuleByName_WithNameAndVersion_ShouldAddModuleFiles(string name, string version)
{
var builder = new SourcePipelineBuilder(null, null, cachePath: GetSourcePath(""));
builder.ModuleByName(name: name, version: version);
var sources = builder.Build();

Assert.Single(sources);
Assert.Equal(name, sources[0].Module.Name);
Assert.Equal(version, sources[0].Module.FullVersion);
Assert.NotEmpty(sources[0].File);
}
}
8 changes: 4 additions & 4 deletions tests/PSRule.Tests/TestModule8/TestModule8.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ ModuleVersion = '0.0.1'
GUID = 'ff869216-0ca8-42e8-930f-5728719d4f5d'

# Author of this module
Author = 'Armaan Mcleod'
Author = 'Bernie White'

# Company or vendor of this module
CompanyName = 'Armaan Mcleod'
CompanyName = 'Bernie White'

# Copyright statement for this module
Copyright = '(c) Armaan Mcleod. All rights reserved.'
Copyright = '(c) Bernie White. All rights reserved.'

# Description of the functionality provided by this module
Description = 'A module for Pester testing for PSRule.'
Expand Down Expand Up @@ -109,7 +109,7 @@ PrivateData = @{
# ReleaseNotes = ''

# Prerelease string of this module
# Prerelease = ''
Prerelease = 'Alpha'

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
# RequireLicenseAcceptance = $false
Expand Down
Loading