Skip to content

Commit 45d9990

Browse files
Merged PR 31780: Resolve paths correctly when importing files or files referenced in the module manifest
1 parent b780a11 commit 45d9990

6 files changed

Lines changed: 235 additions & 22 deletions

File tree

src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4439,31 +4439,14 @@ private bool GetListOfFilesFromData(
44394439

44404440
if (listOfStrings != null)
44414441
{
4442-
var psHome = Utils.DefaultPowerShellAppBase;
4443-
string alternateDirToCheck = null;
4444-
if (moduleBase.StartsWith(psHome, StringComparison.OrdinalIgnoreCase))
4445-
{
4446-
// alternateDirToCheck is an ugly hack for how Microsoft.PowerShell.Diagnostics and
4447-
// Microsoft.WSMan.Management refer to the ps1xml that was in $PSHOME but removed.
4448-
alternateDirToCheck = moduleBase + "\\..\\..";
4449-
}
4450-
44514442
list = new List<string>();
44524443
foreach (string s in listOfStrings)
44534444
{
44544445
try
44554446
{
44564447
string fixedFileName = FixFileNameWithoutLoadingAssembly(moduleBase, s, extension);
4457-
var dir = Path.GetDirectoryName(fixedFileName);
44584448

4459-
if (string.Equals(psHome, dir, StringComparison.OrdinalIgnoreCase) ||
4460-
(alternateDirToCheck != null && string.Equals(alternateDirToCheck, dir, StringComparison.OrdinalIgnoreCase)))
4461-
{
4462-
// The ps1xml file no longer exists in $PSHOME. Downstream, we expect a resolved path,
4463-
// which we can't really do b/c the file doesn't exist.
4464-
fixedFileName = psHome + "\\" + Path.GetFileName(s);
4465-
}
4466-
else if (verifyFilesExist && !File.Exists(fixedFileName))
4449+
if (verifyFilesExist && !File.Exists(fixedFileName))
44674450
{
44684451
string message = StringUtil.Format(SessionStateStrings.PathNotFound, fixedFileName);
44694452
throw new FileNotFoundException(message, fixedFileName);
@@ -4663,7 +4646,8 @@ private string FixFileName(string moduleName, string moduleBase, string fileName
46634646
}
46644647

46654648
// Try to get the resolved fully qualified path to the file.
4666-
// Note that, the 'IsRooted' method also returns true for relative paths, in which case we need to check for 'combinedPath' as well.
4649+
// We only use the combinedPath to resolve the file path, as that combines the file specified with the moduleBase path
4650+
// and ensures that the file is found relative only to the moduleBase not current working directory path.
46674651
// * For example, the 'Microsoft.WSMan.Management.psd1' in Windows PowerShell defines 'FormatsToProcess="..\..\WSMan.format.ps1xml"'.
46684652
// * For such a module, we will have the following input when reaching this method:
46694653
// - moduleBase = 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\Microsoft.WSMan.Management'
@@ -4672,9 +4656,7 @@ private string FixFileName(string moduleName, string moduleBase, string fileName
46724656
// The 'Microsoft.WSMan.Management' module in PowerShell was updated to not use the relative path for 'FormatsToProcess' entry,
46734657
// but it's safer to keep the original behavior to avoid unexpected breaking changes.
46744658
string combinedPath = Path.Combine(moduleBase, fileName);
4675-
string resolvedPath = IsRooted(fileName)
4676-
? ResolveRootedFilePath(fileName, Context) ?? ResolveRootedFilePath(combinedPath, Context)
4677-
: ResolveRootedFilePath(combinedPath, Context);
4659+
string resolvedPath = ResolveRootedFilePath(combinedPath, Context);
46784660

46794661
// Return the path if successfully resolved.
46804662
if (resolvedPath is not null)

test/powershell/Modules/Microsoft.PowerShell.Core/ModuleConstraint.Tests.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,48 @@ Describe "Module loading with version constraints" -Tags "Feature" {
210210
Get-Module $moduleName | Remove-Module
211211
}
212212

213+
It "Loads the module files (ie .ps1xml) relative to module base only" {
214+
# Create module base file structure
215+
$testModule2Name = "TestModule2"
216+
$testModule2Base = Join-Path $TestDrive -ChildPath "$($testModule2Name)Base" # $TestDrive/TestModule2Base
217+
New-Item -Path $testModule2Base -ItemType Directory
218+
$testModule2Path = Join-Path $testModule2Base -ChildPath $testModule2Name # $TestDrive/TestModule2Base/TestModule2
219+
New-Item -Path $testModule2Path -ItemType Directory
220+
221+
$assetsFolderPath = Join-Path $PSScriptRoot -ChildPath "assets"
222+
223+
Copy-Item -Path (Join-Path -Path $assetsFolderPath -ChildPath "$($testModule2Name).psd1") -Destination $testModule2Path # $TestDrive/TestModule2Base/TestModule2/TestModule2.psd1
224+
Copy-Item -Path (Join-Path -Path $assetsFolderPath -ChildPath "$($testModule2Name).psm1") -Destination $testModule2Path # $TestDrive/TestModule2Base/TestModule2/TestModule2.psm1
225+
Copy-Item -Path (Join-Path -Path $assetsFolderPath -ChildPath "$($testModule2Name).Format.ps1xml") -Destination $testModule2Base # $TestDrive/TestModule2Base/TestModule2.Format.ps1xml
226+
Copy-Item -Path (Join-Path -Path $assetsFolderPath -ChildPath "$($testModule2Name).Types.ps1xml") -Destination $testModule2Base # $TestDrive/TestModule2Base/TestModule2.Types.ps1xml
227+
228+
# Also create subfolder file structure within module base
229+
$subFolder1 = Join-Path -Path $testModule2Base -ChildPath "subFolder1" # $TestDrive/TestModule2Base/subFolder1
230+
New-Item -Path $subFolder1 -ItemType Directory
231+
$subFolder2 = Join-Path -Path $subFolder1 -ChildPath "subFolder2" # $TestDrive/TestModule2Base/subFolder1/subFolder2
232+
New-Item -Path $subFolder2 -ItemType Directory
233+
$subFolder3 = Join-Path -Path $subFolder2 -ChildPath "subFolder3" # $TestDrive/TestModule2Base/subFolder1/subFolder2/subFolder3
234+
New-Item -Path $subFolder3 -ItemType Directory
235+
236+
Copy-Item -Path (Join-Path -Path $assetsFolderPath -ChildPath "$($testModule2Name).Format.ps1xml") -Destination $subFolder2 # $TestDrive/subFolder2/TestModule2.Format.ps1xml
237+
Copy-Item -Path (Join-Path -Path $assetsFolderPath -ChildPath "$($testModule2Name).Types.ps1xml") -Destination $subFolder2 # $TestDrive/subFolder2/TestModule2.Types.ps1xml
238+
239+
try {
240+
Push-Location -Path $folder3 # $TestDrive/TestModule2Base/subFolder1/subFolder2/subFolder3
241+
$psd1Path = Join-Path -Path $testModule2Path -ChildPath "$($testModule2Name).psd1"
242+
Import-Module $psd1Path
243+
$filesLoaded = Get-MyFormatsAndTypesFilePath
244+
$filesLoaded[0] | Should -BeLike "*${testModule2Path}*"
245+
$filesLoaded[0] | Should -Not -BeLike "*${subFolder2}*"
246+
$filesLoaded[1] | Should -BeLike "*${testModule2Path}*"
247+
$filesLoaded[1] | Should -Not -BeLike "*${subFolder2}*"
248+
}
249+
finally {
250+
Pop-Location
251+
Remove-Module $testModule2Name
252+
}
253+
}
254+
213255
It "Loads the module by FullyQualifiedName from absolute path when ModuleVersion=<ModuleVersion>, MaximumVersion=<MaximumVersion>, RequiredVersion=<RequiredVersion>, Guid=<Guid>" -TestCases $guidSuccessCases {
214256
param($ModuleVersion, $MaximumVersion, $RequiredVersion, $Guid)
215257

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Configuration>
2+
<ViewDefinitions>
3+
<View>
4+
<Name>TestModule2TypeTable</Name>
5+
<ViewSelectedBy>
6+
<TypeName>TestModule2Type</TypeName>
7+
</ViewSelectedBy>
8+
<TableControl>
9+
<TableHeaders>
10+
<TableColumnHeader><Label>prop1</Label></TableColumnHeader>
11+
<TableColumnHeader><Label>prop2</Label></TableColumnHeader>
12+
</TableHeaders>
13+
<TableRowEntries>
14+
<TableRowEntry>
15+
<TableColumnItems>
16+
<TableColumnItem><PropertyName>prop1</PropertyName></TableColumnItem>
17+
<TableColumnItem><PropertyName>prop2</PropertyName></TableColumnItem>
18+
</TableColumnItems>
19+
</TableRowEntry>
20+
</TableRowEntries>
21+
</TableControl>
22+
</View>
23+
</ViewDefinitions>
24+
</Configuration>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Types>
2+
<Type>
3+
<Name>TestModule2Type</Name>
4+
<Members>
5+
<ScriptMethod>
6+
<Name>ToString</Name>
7+
<Script>
8+
return (":: {0} : {1} ::" -f $this.prop1, $this.prop2)
9+
</Script>
10+
</ScriptMethod>
11+
</Members>
12+
</Type>
13+
</Types>
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
@{
5+
6+
# Script module or binary module file associated with this manifest.
7+
RootModule = 'TestModule2.psm1'
8+
9+
# Version number of this module.
10+
ModuleVersion = '0.0.1'
11+
12+
# Supported PSEditions
13+
# CompatiblePSEditions = @()
14+
15+
# ID used to uniquely identify this module
16+
GUID = '95943533-3149-4f26-99ab-ea3df84d28b2'
17+
18+
# Author of this module
19+
Author = 'PowerShellTesting'
20+
21+
# Company or vendor of this module
22+
CompanyName = 'Unknown'
23+
24+
# Copyright statement for this module
25+
Copyright = '(c) PowerShellTesting. All rights reserved.'
26+
27+
# Description of the functionality provided by this module
28+
# Description = ''
29+
30+
# Minimum version of the PowerShell engine required by this module
31+
# PowerShellVersion = ''
32+
33+
# Name of the PowerShell host required by this module
34+
# PowerShellHostName = ''
35+
36+
# Minimum version of the PowerShell host required by this module
37+
# PowerShellHostVersion = ''
38+
39+
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
40+
# DotNetFrameworkVersion = ''
41+
42+
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
43+
# ClrVersion = ''
44+
45+
# Processor architecture (None, X86, Amd64) required by this module
46+
# ProcessorArchitecture = ''
47+
48+
# Modules that must be imported into the global environment prior to importing this module
49+
# RequiredModules = @()
50+
51+
# Assemblies that must be loaded prior to importing this module
52+
# RequiredAssemblies = @()
53+
54+
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
55+
# ScriptsToProcess = @()
56+
57+
# Type files (.ps1xml) to be loaded when importing this module
58+
TypesToProcess = @('../TestModule2.Types.ps1xml')
59+
60+
# Format files (.ps1xml) to be loaded when importing this module
61+
FormatsToProcess = '../TestModule2.Format.ps1xml'
62+
63+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
64+
# NestedModules = @()
65+
66+
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
67+
FunctionsToExport = '*'
68+
69+
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
70+
CmdletsToExport = '*'
71+
72+
# Variables to export from this module
73+
VariablesToExport = '*'
74+
75+
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
76+
AliasesToExport = '*'
77+
78+
# DSC resources to export from this module
79+
# DscResourcesToExport = @()
80+
81+
# List of all modules packaged with this module
82+
# ModuleList = @()
83+
84+
# List of all files packaged with this module
85+
# FileList = @()
86+
87+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
88+
PrivateData = @{
89+
90+
PSData = @{
91+
92+
# Tags applied to this module. These help with module discovery in online galleries.
93+
# Tags = @()
94+
95+
# A URL to the license for this module.
96+
# LicenseUri = ''
97+
98+
# A URL to the main website for this project.
99+
# ProjectUri = ''
100+
101+
# A URL to an icon representing this module.
102+
# IconUri = ''
103+
104+
# ReleaseNotes of this module
105+
# ReleaseNotes = ''
106+
107+
# Prerelease string of this module
108+
# Prerelease = ''
109+
110+
# Flag to indicate whether the module requires explicit user acceptance for install/update/save
111+
# RequireLicenseAcceptance = $false
112+
113+
# External dependent modules of this module
114+
# ExternalModuleDependencies = @()
115+
116+
} # End of PSData hashtable
117+
118+
} # End of PrivateData hashtable
119+
120+
# HelpInfo URI of this module
121+
# HelpInfoURI = ''
122+
123+
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
124+
# DefaultCommandPrefix = ''
125+
126+
}
127+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
class TestModule2Type {
4+
[string]$prop1
5+
[string]$prop2
6+
[string]$prop3
7+
[string]$prop4
8+
[string]$prop5
9+
[string]$prop6
10+
}
11+
12+
function Get-MyFormatsAndTypesFilePath {
13+
$Host.runspace.InitialSessionState.Types.FileName | ?{$_ -match "TestModule2"}
14+
$Host.runspace.InitialSessionState.Formats.FileName | ?{$_ -match "TestModule2"}
15+
}
16+
17+
function get-mytype {
18+
$module = [TestModule2Type]::new()
19+
$module.prop1 = "p1"
20+
$module.prop2 = "p2"
21+
$module.prop3 = "p3"
22+
$module.prop4 = "p4"
23+
$module.prop5 = "p5"
24+
$module
25+
}

0 commit comments

Comments
 (0)