Skip to content

Commit 60b96d9

Browse files
authored
Fix PSModuleInfo property deserialization in ValidateModuleManifest (#1909)
1 parent 6c2076c commit 60b96d9

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

src/code/Utils.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,7 +1375,7 @@ private static bool TryReadPSDataFile(
13751375
public static bool ValidateModuleManifest(string moduleManifestPath, out string errorMsg)
13761376
{
13771377
errorMsg = string.Empty;
1378-
using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create())
1378+
using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace))
13791379
{
13801380
// use PowerShell cmdlet Test-ModuleManifest
13811381
// TODO: Test-ModuleManifest will throw an error if RequiredModules specifies a module that does not exist
@@ -1400,32 +1400,32 @@ public static bool ValidateModuleManifest(string moduleManifestPath, out string
14001400
}
14011401
}
14021402

1403-
if (pwsh.HadErrors)
1403+
// Validate the result object directly
1404+
if (results.Any())
14041405
{
1405-
if (results.Any())
1406+
PSModuleInfo psModuleInfoObj = results[0].BaseObject as PSModuleInfo;
1407+
if (string.IsNullOrWhiteSpace(psModuleInfoObj.Author))
14061408
{
1407-
PSModuleInfo psModuleInfoObj = results[0].BaseObject as PSModuleInfo;
1408-
if (string.IsNullOrWhiteSpace(psModuleInfoObj.Author))
1409-
{
1410-
errorMsg = "No author was provided in the module manifest. The module manifest must specify a version, author and description. Run 'Test-ModuleManifest' to validate the file.";
1411-
}
1412-
else if (string.IsNullOrWhiteSpace(psModuleInfoObj.Description))
1413-
{
1414-
errorMsg = "No description was provided in the module manifest. The module manifest must specify a version, author and description. Run 'Test-ModuleManifest' to validate the file.";
1415-
}
1416-
else if (psModuleInfoObj.Version == null)
1417-
{
1418-
errorMsg = "No version or an incorrectly formatted version was provided in the module manifest. The module manifest must specify a version, author and description. Run 'Test-ModuleManifest' to validate the file.";
1419-
}
1409+
errorMsg = "No author was provided in the module manifest. The module manifest must specify a version, author and description. Run 'Test-ModuleManifest' to validate the file.";
1410+
return false;
14201411
}
1421-
1422-
if (string.IsNullOrEmpty(errorMsg))
1412+
else if (string.IsNullOrWhiteSpace(psModuleInfoObj.Description))
14231413
{
1424-
// Surface any inner error messages
1425-
var innerErrorMsg = (pwsh.Streams.Error.Count > 0) ? pwsh.Streams.Error[0].ToString() : string.Empty;
1426-
errorMsg = $"Module manifest file validation failed with error: {innerErrorMsg}. Run 'Test-ModuleManifest' to validate the module manifest.";
1414+
errorMsg = "No description was provided in the module manifest. The module manifest must specify a version, author and description. Run 'Test-ModuleManifest' to validate the file.";
1415+
return false;
14271416
}
1428-
1417+
else if (psModuleInfoObj.Version == null)
1418+
{
1419+
errorMsg = "No version or an incorrectly formatted version was provided in the module manifest. The module manifest must specify a version, author and description. Run 'Test-ModuleManifest' to validate the file.";
1420+
return false;
1421+
}
1422+
}
1423+
1424+
// Check for any errors from Test-ModuleManifest
1425+
if (pwsh.HadErrors)
1426+
{
1427+
var innerErrorMsg = (pwsh.Streams.Error.Count > 0) ? pwsh.Streams.Error[0].ToString() : string.Empty;
1428+
errorMsg = $"Module manifest file validation failed with error: {innerErrorMsg}. Run 'Test-ModuleManifest' to validate the module manifest.";
14291429
return false;
14301430
}
14311431
}

test/PublishPSResourceTests/PublishPSResource.Tests.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,21 @@ Describe "Test Publish-PSResource" -tags 'CI' {
131131
}
132132
}
133133

134+
It "Publish a module with valid Author field without -SkipModuleManifestValidate" {
135+
# This test verifies that the fix for runspace deserialization issue works correctly.
136+
# Previously, PSModuleInfo.Author would return empty string when called via PowerShell.Create(),
137+
# causing false positive "No author was provided" errors.
138+
$version = "1.0.0"
139+
$author = "TestAuthor"
140+
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -Author $author
141+
142+
# This should succeed without needing -SkipModuleManifestValidate
143+
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2
144+
145+
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg"
146+
(Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath
147+
}
148+
134149
It "Publish a module with -Path to the highest priority repo" {
135150
$version = "1.0.0"
136151
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"

0 commit comments

Comments
 (0)