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 @@ -4379,7 +4379,7 @@ private bool GetListOfFilesFromData(
{
try
{
string fixedFileName = FixupFileName(moduleBase, s, extension, importingModule);
string fixedFileName = FixupFileName(moduleBase, s, extension, importingModule, skipLoading: true);
var dir = Path.GetDirectoryName(fixedFileName);

if (string.Equals(psHome, dir, StringComparison.OrdinalIgnoreCase) ||
Expand Down Expand Up @@ -4537,9 +4537,9 @@ internal bool GetScalarFromData<T>(
/// <summary>
/// A utility routine to fix up a file name so it's rooted and has an extension.
/// </summary>
internal string FixupFileName(string moduleBase, string name, string extension, bool isImportingModule)
internal string FixupFileName(string moduleBase, string name, string extension, bool isImportingModule, bool skipLoading = false)
{
return FixupFileName(moduleBase, name, extension, isImportingModule, pathIsResolved: out _);
return FixupFileName(moduleBase, name, extension, isImportingModule, pathIsResolved: out _, skipLoading);
}

/// <summary>
Expand All @@ -4554,10 +4554,11 @@ internal string FixupFileName(string moduleBase, string name, string extension,
/// <param name="extension">The extension to use in case the given name has no extension.</param>
/// <param name="isImportingModule">Indicate if we are loading a module.</param>
/// <param name="pathIsResolved">Indicate if the returned path is fully resolved.</param>
/// <param name="skipLoading">Indicate if the resolved module should be loaded.</param>
/// <returns>
/// The resolved file path. Or, the combined path of <paramref name="moduleBase"/> and <paramref name="name"/> when the file path cannot be resolved.
/// </returns>
internal string FixupFileName(string moduleBase, string name, string extension, bool isImportingModule, out bool pathIsResolved)
internal string FixupFileName(string moduleBase, string name, string extension, bool isImportingModule, out bool pathIsResolved, bool skipLoading = false)
{
pathIsResolved = false;
string originalName = name;
Expand Down Expand Up @@ -4585,7 +4586,7 @@ internal string FixupFileName(string moduleBase, string name, string extension,
// Return the path if successfully resolved.
if (resolvedPath != null)
{
if (isImportingModule && resolvedPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
if (isImportingModule && resolvedPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && !skipLoading)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we need skipLoading if we have isImportingModule?

Copy link
Member Author

Choose a reason for hiding this comment

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

Even though we are importing the module, we should not be loading the files in FileList here. FileList is meant to be just a list of all the files in the module. It should not have effect on the loading.

Copy link
Collaborator

Choose a reason for hiding this comment

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

The new parameter description and name could be more informative.
getFileListOnly?

{
// If we are fixing up an assembly file path and we are actually loading the module, then we load the resolved assembly file here.
// This is because we process type/format ps1xml files before 'RootModule' and 'NestedModules' entries during the module loading.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,36 @@ Describe "Import-Module -Force behaviour" -Tag "CI" {
{ Test-Two } | Should -Throw -ErrorId 'CommandNotFoundException'
}
}

Describe "Module with FileList" -Tag "CI" {
BeforeAll {
$src = @"
using System;
namespace ModuleCmdlets
{
public class FileListLoad
{
}
}
"@

$originalPSModulePath = $env:PSModulePath
New-Item -ItemType Directory -Path "$testdrive\Modules\FileListLoadTest\" -Force > $null

$asmPath = "$TESTDRIVE\Modules\FileListLoadTest\FileListLoadTest.dll"
Add-Type -TypeDefinition $src -OutputAssembly $asmPath

$env:PSModulePath += [System.IO.Path]::PathSeparator + "$testdrive\Modules"
New-ModuleManifest -Path "$testdrive\Modules\FileListLoadTest\FileListLoadTest.psd1" -FileList @("FileListLoadTest.dll")
}

AfterAll {
$env:PSModulePath = $originalPSModulePath
}

It "Assemblies in FileList are not loaded" {
Import-Module FileListLoadTest
$asms = [System.AppDomain]::CurrentDomain.GetAssemblies().Location
$asmPath | Should -Not -BeIn $asms
}
}