Skip to content

Commit b04cf29

Browse files
authored
force import modules to avoid errors when importing duplicate types (#8100)
1 parent 153fe7d commit b04cf29

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

Tasks/AzurePowerShell/task.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"version": {
1818
"Major": 2,
1919
"Minor": 0,
20-
"Patch": 11
20+
"Patch": 12
2121
},
2222
"demands": [
2323
"azureps"

Tasks/AzurePowerShell/task.loc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"version": {
1818
"Major": 2,
1919
"Minor": 0,
20-
"Patch": 11
20+
"Patch": 12
2121
},
2222
"demands": [
2323
"azureps"

Tasks/Common/VstsAzureHelpers_/ImportFunctions.ps1

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ function Import-FromModulePath {
9494
}
9595

9696
# Import the module.
97-
Write-Host "##[command]Import-Module -Name $($module.Path) -Global"
98-
$module = Import-Module -Name $module.Path -Global -PassThru
97+
Write-Host "##[command]Import-Module -Name $($module.Path) -Global -Force"
98+
$module = Import-Module -Name $module.Path -Global -PassThru -Force
9999
Write-Verbose "Imported module version: $($module.Version)"
100100

101101
if ($Classic) {
@@ -117,8 +117,8 @@ function Import-FromModulePath {
117117
throw (Get-VstsLocString -Key AZ_AzureRMProfileModuleNotFound)
118118
}
119119
# Import and then store the AzureRM.profile module.
120-
Write-Host "##[command]Import-Module -Name $($profileModule.Path) -Global"
121-
$script:azureRMProfileModule = Import-Module -Name $profileModule.Path -Global -PassThru
120+
Write-Host "##[command]Import-Module -Name $($profileModule.Path) -Global -Force"
121+
$script:azureRMProfileModule = Import-Module -Name $profileModule.Path -Global -PassThru -Force
122122
} else {
123123
$script:azureRMProfileModule = $profileModule
124124
}
@@ -156,8 +156,8 @@ function Import-FromSdkPath {
156156
continue
157157
}
158158
# Import the module.
159-
Write-Host "##[command]Import-Module -Name $path -Global"
160-
$module = Import-Module -Name $path -Global -PassThru
159+
Write-Host "##[command]Import-Module -Name $path -Global -Force"
160+
$module = Import-Module -Name $path -Global -PassThru -Force
161161
Write-Verbose "Imported module version: $($module.Version)"
162162
# Store the imported module.
163163
if ($Classic) {
@@ -197,8 +197,8 @@ function Import-AzureRmSubmodulesFromSdkPath {
197197
try {
198198
# Azure.Storage submodule needs to be imported first
199199
$azureStorageModulePath = [System.IO.Path]::Combine($programFiles, "Microsoft SDKs\Azure\PowerShell\Storage\Azure.Storage\Azure.Storage.psd1")
200-
Write-Host "##[command]Import-Module -Name $azureStorageModulePath -Global"
201-
$azureStorageModule = Import-Module -Name $azureStorageModulePath -Global -PassThru
200+
Write-Host "##[command]Import-Module -Name $azureStorageModulePath -Global -Force"
201+
$azureStorageModule = Import-Module -Name $azureStorageModulePath -Global -PassThru -Force
202202
Write-Verbose "Imported module version: $($azureStorageModule.Version)"
203203
}
204204
catch {
@@ -215,8 +215,8 @@ function Import-AzureRmSubmodulesFromSdkPath {
215215
}
216216
$azureRmNestedModulePath = [System.IO.Path]::Combine($azureRmNestedModule.FullName, $azureRmNestedModule.Name + ".psd1")
217217
try {
218-
Write-Verbose "##[command]Import-Module -Name $azureRmNestedModulePath -Global"
219-
$azureRmSubmodule = Import-Module -Name $azureRmNestedModulePath -Global -PassThru
218+
Write-Verbose "##[command]Import-Module -Name $azureRmNestedModulePath -Global -Force"
219+
$azureRmSubmodule = Import-Module -Name $azureRmNestedModulePath -Global -PassThru -Force
220220
Write-Verbose "Imported module version: $($azureRmSubmodule.Version)"
221221
}
222222
catch {

Tasks/Common/VstsAzureHelpers_/Tests/Import-FromModulePath.ImportsModules.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ foreach ($variableSet in $variableSets) {
4444
Unregister-Mock Get-Module
4545
Unregister-Mock Import-Module
4646
Register-Mock Get-Module { $variableSet.Modules[0] } -- -Name $variableSet.Modules[0].Name -ListAvailable
47-
Register-Mock Import-Module { $variableSet.Modules[0] } -- -Name $variableSet.Modules[0].Path -Global -PassThru
47+
Register-Mock Import-Module { $variableSet.Modules[0] } -- -Name $variableSet.Modules[0].Path -Global -PassThru -Force
4848
if ($variableSet.Modules.Length -eq 2) {
4949
Register-Mock Get-Module { $variableSet.Modules[0] } -- -Name $variableSet.Modules[0].Name
50-
Register-Mock Import-Module { $variableSet.Modules[1] } -- -Name $variableSet.Modules[1].Path -Global -PassThru
50+
Register-Mock Import-Module { $variableSet.Modules[1] } -- -Name $variableSet.Modules[1].Path -Global -PassThru -Force
5151
}
5252

5353
# Act.
5454
$result = & $module Import-FromModulePath -Classic:($variableSet.Classic) -azurePsVersion "4.1.0"
5555

5656
# Assert.
5757
Assert-AreEqual $true $result
58-
Assert-WasCalled Import-Module -- -Name $variableSet.Modules[0].Path -Global -PassThru
58+
Assert-WasCalled Import-Module -- -Name $variableSet.Modules[0].Path -Global -PassThru -Force
5959
if ($variableSet.Modules.Length -eq 2) {
60-
Assert-WasCalled Import-Module -- -Name $variableSet.Modules[1].Path -Global -PassThru
60+
Assert-WasCalled Import-Module -- -Name $variableSet.Modules[1].Path -Global -PassThru -Force
6161
}
6262

6363
if ($variableSet.Classic) {

Tasks/Common/VstsAzureHelpers_/Tests/Import-FromSdkPath.ImportsModule.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ foreach ($variableSet in $variableSets) {
8080
# Setup Import-Module.
8181
if ($variableSet.FoundInProgramFilesX86) {
8282
$expectedModule = @{ Version = [version]'1.2.3.4' }
83-
Register-Mock Import-Module { $expectedModule } -Name $wowPsd1 -Global -PassThru
83+
Register-Mock Import-Module { $expectedModule } -Name $wowPsd1 -Global -PassThru -Force
8484
}
8585

8686
if ($variableSet.FoundInProgramFiles) {
8787
$expectedModule = @{ Version = [version]'2.3.4.5' }
88-
Register-Mock Import-Module { $expectedModule } -Name $psd1 -Global -PassThru
88+
Register-Mock Import-Module { $expectedModule } -Name $psd1 -Global -PassThru -Force
8989
}
9090

9191
if($variableSet.Classic -eq $false) {
@@ -100,9 +100,9 @@ foreach ($variableSet in $variableSets) {
100100
# Assert.
101101
Assert-AreEqual $true $result
102102
if ($variableSet.FoundInProgramFilesX86) {
103-
Assert-WasCalled Import-Module -- -Name $wowPsd1 -Global -PassThru
103+
Assert-WasCalled Import-Module -- -Name $wowPsd1 -Global -PassThru -Force
104104
} else {
105-
Assert-WasCalled Import-Module -- -Name $psd1 -Global -PassThru
105+
Assert-WasCalled Import-Module -- -Name $psd1 -Global -PassThru -Force
106106
}
107107

108108
if ($variableSet.Classic) {

0 commit comments

Comments
 (0)