From 2e8b494dade6cbb9efeace4577da1615036975a8 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 12 Jul 2022 17:23:07 -0700 Subject: [PATCH 1/3] Fix Export-PSSession to not throw error when a rooted path is specified for -OutputModule --- .../resources/PathUtilsStrings.resx | 2 +- .../utils/PathUtils.cs | 25 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/resources/PathUtilsStrings.resx b/src/System.Management.Automation/resources/PathUtilsStrings.resx index b7675e53268..07a4677fa2f 100644 --- a/src/System.Management.Automation/resources/PathUtilsStrings.resx +++ b/src/System.Management.Automation/resources/PathUtilsStrings.resx @@ -139,7 +139,7 @@ The directory '{0}' already exists. Use the -Force parameter if you want to overwrite the directory and files within the directory. - The -OutputModule parameter does not resolve to a path, and a user module path cannot be found for the provided name. + The user module path does not exist, and hence a module folder cannot be created for the provided module name '{0}'. Cannot create the module {0} due to the following: {1}. Use a different argument for the -OutputModule parameter and retry. diff --git a/src/System.Management.Automation/utils/PathUtils.cs b/src/System.Management.Automation/utils/PathUtils.cs index 848fcbd6bfc..b0a4ee87a27 100644 --- a/src/System.Management.Automation/utils/PathUtils.cs +++ b/src/System.Management.Automation/utils/PathUtils.cs @@ -366,18 +366,25 @@ internal static DirectoryInfo CreateModuleDirectory(PSCmdlet cmdlet, string modu if (string.IsNullOrEmpty(rootedPath)) { - string personalModuleRoot = ModuleIntrinsics.GetPersonalModulePath(); - if (string.IsNullOrEmpty(personalModuleRoot)) + if (Path.IsPathRooted(moduleNameOrPath)) { - cmdlet.ThrowTerminatingError( - new ErrorRecord( - new ArgumentException(PathUtilsStrings.ExportPSSession_ErrorModuleNameOrPath), - "ExportPSSession_ErrorModuleNameOrPath", - ErrorCategory.InvalidArgument, - cmdlet)); + rootedPath = moduleNameOrPath; } + else + { + string personalModuleRoot = ModuleIntrinsics.GetPersonalModulePath(); + if (string.IsNullOrEmpty(personalModuleRoot)) + { + cmdlet.ThrowTerminatingError( + new ErrorRecord( + new ArgumentException(StringUtil.Format(PathUtilsStrings.ExportPSSession_ErrorModuleNameOrPath, moduleNameOrPath)), + "ExportPSSession_ErrorModuleNameOrPath", + ErrorCategory.InvalidArgument, + cmdlet)); + } - rootedPath = Path.Combine(personalModuleRoot, moduleNameOrPath); + rootedPath = Path.Combine(personalModuleRoot, moduleNameOrPath); + } } directoryInfo = new DirectoryInfo(rootedPath); From 23b841134ec33b3dd336aed9910e233c48a79f00 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 13 Jul 2022 10:16:56 -0700 Subject: [PATCH 2/3] Update comment --- src/System.Management.Automation/utils/PathUtils.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/utils/PathUtils.cs b/src/System.Management.Automation/utils/PathUtils.cs index b0a4ee87a27..f82061d2543 100644 --- a/src/System.Management.Automation/utils/PathUtils.cs +++ b/src/System.Management.Automation/utils/PathUtils.cs @@ -5,6 +5,7 @@ using System.Globalization; using System.IO; using System.Management.Automation.Internal; +using Microsoft.PowerShell.Commands; using System.Runtime.CompilerServices; using System.Text; @@ -357,7 +358,9 @@ internal static DirectoryInfo CreateModuleDirectory(PSCmdlet cmdlet, string modu DirectoryInfo directoryInfo = null; try { - string rootedPath = Microsoft.PowerShell.Commands.ModuleCmdletBase.ResolveRootedFilePath(moduleNameOrPath, cmdlet.Context); + // Even if 'moduleNameOrPath' is a rooted path, 'ResolveRootedFilePath' may return null when the path doesn't exist yet, + // or when it contains wildcards but cannot be resolved to a single path. + string rootedPath = ModuleCmdletBase.ResolveRootedFilePath(moduleNameOrPath, cmdlet.Context); if (string.IsNullOrEmpty(rootedPath) && moduleNameOrPath.StartsWith('.')) { PathInfo currentPath = cmdlet.CurrentProviderLocation(cmdlet.Context.ProviderNames.FileSystem); From eb24966b75e6299f7aba461a515883225bbbe668 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 13 Jul 2022 10:58:36 -0700 Subject: [PATCH 3/3] Update src/System.Management.Automation/utils/PathUtils.cs Co-authored-by: Aditya Patwardhan --- src/System.Management.Automation/utils/PathUtils.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/System.Management.Automation/utils/PathUtils.cs b/src/System.Management.Automation/utils/PathUtils.cs index f82061d2543..48d0acb5f49 100644 --- a/src/System.Management.Automation/utils/PathUtils.cs +++ b/src/System.Management.Automation/utils/PathUtils.cs @@ -5,10 +5,9 @@ using System.Globalization; using System.IO; using System.Management.Automation.Internal; -using Microsoft.PowerShell.Commands; using System.Runtime.CompilerServices; using System.Text; - +using Microsoft.PowerShell.Commands; using Dbg = System.Management.Automation.Diagnostics; namespace System.Management.Automation