diff --git a/src/code/Utils.cs b/src/code/Utils.cs index c9c970506..cc6f0fa14 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -1375,7 +1375,7 @@ private static bool TryReadPSDataFile( public static bool ValidateModuleManifest(string moduleManifestPath, out string errorMsg) { errorMsg = string.Empty; - using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create()) + using (System.Management.Automation.PowerShell pwsh = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace)) { // use PowerShell cmdlet Test-ModuleManifest // 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 } } - if (pwsh.HadErrors) + // Validate the result object directly + if (results.Any()) { - if (results.Any()) + PSModuleInfo psModuleInfoObj = results[0].BaseObject as PSModuleInfo; + if (string.IsNullOrWhiteSpace(psModuleInfoObj.Author)) { - PSModuleInfo psModuleInfoObj = results[0].BaseObject as PSModuleInfo; - if (string.IsNullOrWhiteSpace(psModuleInfoObj.Author)) - { - 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."; - } - else if (string.IsNullOrWhiteSpace(psModuleInfoObj.Description)) - { - 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."; - } - else if (psModuleInfoObj.Version == null) - { - 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."; - } + 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."; + return false; } - - if (string.IsNullOrEmpty(errorMsg)) + else if (string.IsNullOrWhiteSpace(psModuleInfoObj.Description)) { - // Surface any inner error messages - var innerErrorMsg = (pwsh.Streams.Error.Count > 0) ? pwsh.Streams.Error[0].ToString() : string.Empty; - errorMsg = $"Module manifest file validation failed with error: {innerErrorMsg}. Run 'Test-ModuleManifest' to validate the module manifest."; + 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."; + return false; } - + else if (psModuleInfoObj.Version == null) + { + 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."; + return false; + } + } + + // Check for any errors from Test-ModuleManifest + if (pwsh.HadErrors) + { + var innerErrorMsg = (pwsh.Streams.Error.Count > 0) ? pwsh.Streams.Error[0].ToString() : string.Empty; + errorMsg = $"Module manifest file validation failed with error: {innerErrorMsg}. Run 'Test-ModuleManifest' to validate the module manifest."; return false; } } diff --git a/test/PublishPSResourceTests/PublishPSResource.Tests.ps1 b/test/PublishPSResourceTests/PublishPSResource.Tests.ps1 index e97f64407..ed0adf1b8 100644 --- a/test/PublishPSResourceTests/PublishPSResource.Tests.ps1 +++ b/test/PublishPSResourceTests/PublishPSResource.Tests.ps1 @@ -131,6 +131,21 @@ Describe "Test Publish-PSResource" -tags 'CI' { } } + It "Publish a module with valid Author field without -SkipModuleManifestValidate" { + # This test verifies that the fix for runspace deserialization issue works correctly. + # Previously, PSModuleInfo.Author would return empty string when called via PowerShell.Create(), + # causing false positive "No author was provided" errors. + $version = "1.0.0" + $author = "TestAuthor" + New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -Author $author + + # This should succeed without needing -SkipModuleManifestValidate + Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2 + + $expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$script:PublishModuleName.$version.nupkg" + (Get-ChildItem $script:repositoryPath2).FullName | Should -Be $expectedPath + } + It "Publish a module with -Path to the highest priority repo" { $version = "1.0.0" New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module"