Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
<value>The directory '{0}' already exists. Use the -Force parameter if you want to overwrite the directory and files within the directory.</value>
</data>
<data name="ExportPSSession_ErrorModuleNameOrPath" xml:space="preserve">
<value>The -OutputModule parameter does not resolve to a path, and a user module path cannot be found for the provided name.</value>
<value>The user module path does not exist, and hence a module folder cannot be created for the provided module name '{0}'.</value>
</data>
<data name="ExportPSSession_CannotCreateOutputDirectory" xml:space="preserve">
<value>Cannot create the module {0} due to the following: {1}. Use a different argument for the -OutputModule parameter and retry.</value>
Expand Down
31 changes: 20 additions & 11 deletions src/System.Management.Automation/utils/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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
Expand Down Expand Up @@ -357,7 +357,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);
Expand All @@ -366,18 +368,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);
Expand Down