From f9d424dd860cc036cd35c901c3e51fd8fdad9d08 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Wed, 13 Jul 2022 13:31:57 -0700 Subject: [PATCH] Fix `Export-PSSession` to not throw error when a rooted path is specified for `-OutputModule` (#17671) --- .../resources/PathUtilsStrings.resx | 2 +- .../utils/PathUtils.cs | 31 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/System.Management.Automation/resources/PathUtilsStrings.resx b/src/System.Management.Automation/resources/PathUtilsStrings.resx index b7675e5326..07a4677fa2 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 848fcbd6bf..48d0acb5f4 100644 --- a/src/System.Management.Automation/utils/PathUtils.cs +++ b/src/System.Management.Automation/utils/PathUtils.cs @@ -7,7 +7,7 @@ using System.IO; using System.Management.Automation.Internal; using System.Runtime.CompilerServices; using System.Text; - +using Microsoft.PowerShell.Commands; using Dbg = System.Management.Automation.Diagnostics; namespace System.Management.Automation @@ -357,7 +357,9 @@ namespace System.Management.Automation 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); @@ -366,18 +368,25 @@ namespace System.Management.Automation 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);