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 @@ -1131,7 +1131,7 @@ private static CommandInfo TryModuleAutoDiscovery(string commandName,
// Get the available module files, preferring modules from $PSHOME so that user modules don't
// override system modules during auto-loading
if (etwEnabled) CommandDiscoveryEventSource.Log.SearchingForModuleFilesStart();
var defaultAvailableModuleFiles = ModuleUtils.GetDefaultAvailableModuleFiles(true, true, context);
var defaultAvailableModuleFiles = ModuleUtils.GetDefaultAvailableModuleFiles(isForAutoDiscovery: true, context);
if (etwEnabled) CommandDiscoveryEventSource.Log.SearchingForModuleFilesStop();

foreach (string modulePath in defaultAvailableModuleFiles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,7 @@ private string[] GetModulesForUnResolvedCommands(IEnumerable<string> unresolvedC
foreach (var unresolvedCommand in commandsToResolve)
{
// Use the analysis cache to find the first module containing the unresolved command.
foreach (string modulePath in ModuleUtils.GetDefaultAvailableModuleFiles(true, true, context))
foreach (string modulePath in ModuleUtils.GetDefaultAvailableModuleFiles(isForAutoDiscovery: true, context))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hooray for this and the one above!

{
string expandedModulePath = IO.Path.GetFullPath(modulePath);
var exportedCommands = AnalysisCache.GetExportedCommands(expandedModulePath, false, context);
Expand Down
160 changes: 55 additions & 105 deletions src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -869,37 +869,19 @@ internal List<PSModuleInfo> GetModule(string[] names, bool all, bool refresh)
moduleNames.Add(n);
}
}
modulesToReturn.AddRange(GetModuleForRootedPaths(modulePaths.ToArray(), all, refresh));
modulesToReturn.AddRange(GetModuleForRootedPaths(modulePaths, all, refresh));
}

// If no names were passed to this function, then this API will return list of all available modules
if (names == null || moduleNames.Count > 0)
{
modulesToReturn.AddRange(GetModuleForNonRootedPaths(moduleNames.ToArray(), all, refresh));
modulesToReturn.AddRange(GetModuleForNames(moduleNames, all, refresh));
}

return modulesToReturn;
}

private IEnumerable<PSModuleInfo> GetModuleForNonRootedPaths(string[] names, bool all, bool refresh)
{
const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
IEnumerable<WildcardPattern> patternList = SessionStateUtilities.CreateWildcardsFromStrings(names, wildcardOptions);

Dictionary<String, List<PSModuleInfo>> availableModules = GetAvailableLocallyModulesCore(names, all, refresh);
foreach (var entry in availableModules)
{
foreach (PSModuleInfo module in entry.Value)
{
if (SessionStateUtilities.MatchesAnyWildcardPattern(module.Name, patternList, true))
{
yield return module;
}
}
}
}

private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(string[] modulePaths, bool all, bool refresh)
private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(List<string> modulePaths, bool all, bool refresh)
{
// This is to filter out duplicate modules
var modules = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
Expand Down Expand Up @@ -950,7 +932,7 @@ private IEnumerable<PSModuleInfo> GetModuleForRootedPaths(string[] modulePaths,

var availableModuleFiles = all
? ModuleUtils.GetAllAvailableModuleFiles(resolvedModulePath)
: ModuleUtils.GetModuleVersionsFromAbsolutePath(resolvedModulePath);
: ModuleUtils.GetModuleFilesFromAbsolutePath(resolvedModulePath);

bool foundModule = false;
foreach (string file in availableModuleFiles)
Expand Down Expand Up @@ -998,38 +980,31 @@ private ErrorRecord CreateModuleNotFoundError(string modulePath)
return er;
}

private Dictionary<String, List<PSModuleInfo>> GetAvailableLocallyModulesCore(string[] names, bool all, bool refresh)
private IEnumerable<PSModuleInfo> GetModuleForNames(List<string> names, bool all, bool refresh)
{
var modules = new Dictionary<string, List<PSModuleInfo>>(StringComparer.OrdinalIgnoreCase);
var modulePaths = ModuleIntrinsics.GetModulePath(false, Context);

IEnumerable<PSModuleInfo> allModules = null;
HashSet<string> modulePathSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
bool cleanupModuleAnalysisAppDomain = Context.TakeResponsibilityForModuleAnalysisAppDomain();

try
{
foreach (string path in ModuleIntrinsics.GetModulePath(false, Context))
{
try
{
var availableModules = all
? GetAllAvailableModules(path, refresh)
: GetDefaultAvailableModules(names, path, refresh);
string uniquePath = path.TrimEnd(Utils.Separators.Directory);

// Add the path in $env:PSModulePath as the keys of the dictionary
// If the paths are repeated, ignore the repetitions
string uniquePath = path.TrimEnd(Utils.Separators.Directory);
if (!modules.ContainsKey(uniquePath))
{
modules.Add(uniquePath, availableModules.OrderBy(m => m.Name).ToList());
}
}
catch (IOException)
// Ignore repeated module path.
if (!modulePathSet.Add(uniquePath)) { continue; }

try
{
continue; // ignore directories that can't be accessed
IEnumerable<PSModuleInfo> modulesFound = GetModulesFromOneModulePath(
names, uniquePath, all, refresh).OrderBy(m => m.Name);
allModules = allModules == null ? modulesFound : allModules.Concat(modulesFound);
}
catch (UnauthorizedAccessException)
catch (Exception e) when (e is IOException || e is UnauthorizedAccessException)
{
continue; // ignore directories that can't be accessed
// ignore directories that can't be accessed
continue;
}
}
}
Expand All @@ -1041,64 +1016,43 @@ private Dictionary<String, List<PSModuleInfo>> GetAvailableLocallyModulesCore(st
}
}

return modules;
// Make sure we always return a non-null collection.
return allModules ?? Utils.EmptyArray<PSModuleInfo>();
}

/// <summary>
/// Get a list of all modules
/// which can be imported just by specifying a non rooted file name of the module
/// (Import-Module foo\bar.psm1; but not Import-Module .\foo\bar.psm1)
/// Get modules based on the given names and module files.
/// </summary>
private IEnumerable<PSModuleInfo> GetAllAvailableModules(string directory, bool refresh)
private IEnumerable<PSModuleInfo> GetModulesFromOneModulePath(List<string> names, string modulePath, bool all, bool refresh)
{
var availableModuleFiles = ModuleUtils.GetAllAvailableModuleFiles(directory);

foreach (string file in availableModuleFiles)
const WildcardOptions options = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
IEnumerable<WildcardPattern> namePatterns = null;
if (names != null && names.Count > 0)
{
PSModuleInfo module = CreateModuleInfoForGetModule(file, refresh);
if (module != null)
{
yield return module;
}
namePatterns = SessionStateUtilities.CreateWildcardsFromStrings(names, options);
}
}

/// <summary>
/// Get a list of the available modules
/// which can be imported just by specifying a non rooted directory name of the module
/// (Import-Module foo\bar; but not Import-Module .\foo\bar or Import-Module .\foo\bar.psm1)
/// </summary>
private List<PSModuleInfo> GetDefaultAvailableModules(string[] name, string directory, bool refresh)
{
List<PSModuleInfo> availableModules = new List<PSModuleInfo>();

const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
IEnumerable<WildcardPattern> patternList = SessionStateUtilities.CreateWildcardsFromStrings(name, wildcardOptions);
IEnumerable<string> moduleFiles = all
? ModuleUtils.GetAllAvailableModuleFiles(modulePath)
: ModuleUtils.GetDefaultAvailableModuleFiles(modulePath);

var availableModuleFiles = ModuleUtils.GetDefaultAvailableModuleFiles(directory);

foreach (string file in availableModuleFiles)
foreach (string file in moduleFiles)
{
string actualModuleName = System.IO.Path.GetFileNameWithoutExtension(file);
if (SessionStateUtilities.MatchesAnyWildcardPattern(actualModuleName, patternList, true))
if (namePatterns == null ||
SessionStateUtilities.MatchesAnyWildcardPattern(
Path.GetFileNameWithoutExtension(file), namePatterns, defaultValue: true))
{
PSModuleInfo module = CreateModuleInfoForGetModule(file, refresh);
if (module == null) { continue; }

if (module != null)
if (all || !ModuleUtils.IsModuleInVersionSubdirectory(file, out Version directoryVersion) || directoryVersion == module.Version)
{
Version directoryVersion;
if (!ModuleUtils.IsModuleInVersionSubdirectory(file, out directoryVersion)
|| directoryVersion == module.Version)
{
availableModules.Add(module);
}
yield return module;
}
}
}

ClearAnalysisCaches();

return availableModules;
}

/// <summary>
Expand Down Expand Up @@ -1191,24 +1145,24 @@ private PSModuleInfo CreateModuleInfoForGetModule(string file, bool refresh)
moduleInfo = LoadModuleManifest(
scriptInfo,
flags /* - don't write errors, don't load elements */,
null,
null,
null,
null);
minimumVersion: null,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah much nicer!

maximumVersion: null,
requiredVersion: null,
requiredModuleGuid: null);
}
else
{
// It's not a module manifest, process the individual module
ImportModuleOptions options = new ImportModuleOptions();
bool found = false;

moduleInfo = LoadModule(file, null, String.Empty, null, ref options, flags, out found);
moduleInfo = LoadModule(file, moduleBase: null, prefix: String.Empty, ss: null, ref options, flags, out found);
}

// return fake PSModuleInfo if can't read the file for any reason
if (moduleInfo == null)
{
moduleInfo = new PSModuleInfo(file, null, null);
moduleInfo = new PSModuleInfo(file, context: null, sessionState: null);
moduleInfo.HadErrorsLoading = true; // Prevent analysis cache from caching a bad module.
}

Expand Down Expand Up @@ -2677,9 +2631,13 @@ internal PSModuleInfo LoadModuleManifest(
}
}

var needToAnalyzeScriptModules = usedWildcard;
// We have to further analyze the module if any wildcard characters are used.
bool needToAnalyzeScriptModules = usedWildcard;

if (!needToAnalyzeScriptModules)
// We can skip further analysis if 'FunctionsToExport', 'CmdletsToExport' and 'AliasesToExport'
// are all declared and no wildcard character is used for them. But if any of 'FunctionsToExport',
// 'CmdletsToExport' or 'AliasesToExport' were not given, we must check to see if more analysis is needed.
if (!needToAnalyzeScriptModules && (!sawExportedCmdlets || !sawExportedFunctions || !sawExportedAliases))
{
foreach (var nestedModule in nestedModules)
{
Expand Down Expand Up @@ -3173,9 +3131,9 @@ internal PSModuleInfo LoadModuleManifest(
newManifestInfo.DeclaredCmdletExports = manifestInfo.DeclaredCmdletExports;
}

if (manifestInfo._detectedCmdletExports != null)
if (manifestInfo.DetectedCmdletExports != null)
{
foreach (string detectedExport in manifestInfo._detectedCmdletExports)
foreach (string detectedExport in manifestInfo.DetectedCmdletExports)
{
newManifestInfo.AddDetectedCmdletExport(detectedExport);
}
Expand All @@ -3187,9 +3145,9 @@ internal PSModuleInfo LoadModuleManifest(
newManifestInfo.DeclaredFunctionExports = manifestInfo.DeclaredFunctionExports;
}

if (manifestInfo._detectedFunctionExports != null)
if (manifestInfo.DetectedFunctionExports != null)
{
foreach (string detectedExport in manifestInfo._detectedFunctionExports)
foreach (string detectedExport in manifestInfo.DetectedFunctionExports)
{
newManifestInfo.AddDetectedFunctionExport(detectedExport);
}
Expand All @@ -3200,9 +3158,9 @@ internal PSModuleInfo LoadModuleManifest(
newManifestInfo.DeclaredAliasExports = manifestInfo.DeclaredAliasExports;
}

if (manifestInfo._detectedAliasExports != null)
if (manifestInfo.DetectedAliasExports != null)
{
foreach (var pair in manifestInfo._detectedAliasExports)
foreach (var pair in manifestInfo.DetectedAliasExports)
{
newManifestInfo.AddDetectedAliasExport(pair.Key, pair.Value);
}
Expand Down Expand Up @@ -3269,15 +3227,7 @@ internal PSModuleInfo LoadModuleManifest(
}
else
{
Collection<string> v = new Collection<string>();
if (manifestInfo.DeclaredFunctionExports != null)
{
foreach (var f in manifestInfo.DeclaredFunctionExports)
{
v.Add(f);
}
}
UpdateCommandCollection(v, exportedFunctions);
UpdateCommandCollection(manifestInfo.DeclaredFunctionExports, exportedFunctions);
}
}

Expand Down
Loading