Skip to content

Commit 4145dd4

Browse files
authored
Fix Az.Accounts cannot be imported due to assembly not found (#20637)
* fix accounts cannot be imported * Az.Accounts v2.11.1 * fixed type error in CheckAssemblies.ps1
1 parent 33ad3cb commit 4145dd4

File tree

19 files changed

+313
-237
lines changed

19 files changed

+313
-237
lines changed

src/Accounts/Accounts/Az.Accounts.psd1

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#
44
# Generated by: Microsoft Corporation
55
#
6-
# Generated on: 1/6/2023
6+
# Generated on: 1/12/2023
77
#
88

99
@{
@@ -12,7 +12,7 @@
1212
# RootModule = ''
1313

1414
# Version number of this module.
15-
ModuleVersion = '2.11.0'
15+
ModuleVersion = '2.11.1'
1616

1717
# Supported PSEditions
1818
CompatiblePSEditions = 'Core', 'Desktop'
@@ -147,11 +147,7 @@ PrivateData = @{
147147
# IconUri = ''
148148

149149
# ReleaseNotes of this module
150-
ReleaseNotes = '* Supported Web Account Manager (WAM) as an opt-in interactive login experience. Enable it by ''Update-AzConfig -EnableLoginByWam True''.
151-
* Optimized the mechanism for assembly loading.
152-
* Enabled AzKeyStore with keyring in Linux.
153-
* Fixed a typo in GetAzureRmContextAutosaveSetting.cs changing the cmdlet class name to GetAzureRmContextAutosaveSetting
154-
* Removed survey on error message in ''Resolve-AzError''. [#20398]'
150+
ReleaseNotes = '* Fixed an issue where Az.Accounts cannot be imported correctly. [#20615]'
155151

156152
# Prerelease string of this module
157153
# Prerelease = ''

src/Accounts/Accounts/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
## Upcoming Release
2222

23+
## Version 2.11.1
24+
* Fixed an issue where Az.Accounts cannot be imported correctly. [#20615]
25+
2326
## Version 2.11.0
2427
* Supported Web Account Manager (WAM) as an opt-in interactive login experience. Enable it by `Update-AzConfig -EnableLoginByWam $true`.
2528
* Optimized the mechanism for assembly loading.

src/Accounts/Accounts/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
// You can specify all the values or you can default the Build and Revision Numbers
4444
// by using the '*' as shown below:
4545

46-
[assembly: AssemblyVersion("2.11.0")]
47-
[assembly: AssemblyFileVersion("2.11.0")]
46+
[assembly: AssemblyVersion("2.11.1")]
47+
[assembly: AssemblyFileVersion("2.11.1")]
4848
#if !SIGN
4949
[assembly: InternalsVisibleTo("Microsoft.Azure.PowerShell.Cmdlets.Accounts.Test")]
5050
#endif

src/Accounts/Accounts/StartupScripts/InitializeAssemblyResolver.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
$assemblyRootPath = [System.IO.Path]::Combine($PSScriptRoot, "..", "lib")
2-
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($Host.Version)
2+
Write-Debug "Initializing ConditionalAssemblyContext. PSEdition is [$($PSVersionTable.PSEdition)]. PSVersion is [$($PSVersionTable.PSVersion)]."
3+
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($PSVersionTable.PSEdition, $PSVersionTable.PSVersion)
4+
Write-Debug "Initializing ConditionalAssemblyProvider. AssemblyRootPath is [$assemblyRootPath]."
35
[Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::Initialize($assemblyRootPath, $conditionalAssemblyContext)
46

57
if ($PSEdition -eq 'Desktop') {

src/Accounts/AssemblyLoading.Test/Mocks/MockConditionalAssemblyContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace Microsoft.Azure.PowerShell.AssemblyLoading.Test.Mocks
1919
{
2020
internal class MockConditionalAssemblyContext : IConditionalAssemblyContext
2121
{
22+
public string PSEdition { get; set; }
2223
public Version PSVersion { get; set; }
2324
public Architecture OSArchitecture { get; set; }
2425
public OSPlatform OS { get; set; }

src/Accounts/AssemblyLoading.Test/UnitTests/ConditionalAssemblyExtensionsTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public void CanWorkWithPSVersion()
2828
{
2929
var windowsPSContext = new MockConditionalAssemblyContext()
3030
{
31+
PSEdition = Constants.PSEditionDesktop,
3132
PSVersion = Version.Parse("5.1.22621.608")
3233
};
3334
var windowsPSAssembly = new MockConditionalAssembly(windowsPSContext)
@@ -40,6 +41,7 @@ public void CanWorkWithPSVersion()
4041

4142
var ps7Context = new MockConditionalAssemblyContext()
4243
{
44+
PSEdition = Constants.PSEditionCore,
4345
PSVersion = Version.Parse("7.3.0")
4446
};
4547
windowsPSAssembly = new MockConditionalAssembly(
@@ -52,6 +54,23 @@ public void CanWorkWithPSVersion()
5254
Assert.False(windowsPSAssembly.ShouldLoad);
5355
}
5456

57+
[Fact]
58+
[Trait(Category.AcceptanceType, Category.CheckIn)]
59+
public void CanWorkWithEmptyPSEdition()
60+
{
61+
var windowsPSContext = new MockConditionalAssemblyContext()
62+
{
63+
PSVersion = Version.Parse("1.0.0.0")
64+
};
65+
var windowsPSAssembly = new MockConditionalAssembly(windowsPSContext)
66+
.WithWindowsPowerShell();
67+
var psCoreAssembly = new MockConditionalAssembly(
68+
windowsPSContext)
69+
.WithPowerShellCore();
70+
Assert.True(windowsPSAssembly.ShouldLoad);
71+
Assert.False(psCoreAssembly.ShouldLoad);
72+
}
73+
5574
[Fact]
5675
[Trait(Category.AcceptanceType, Category.CheckIn)]
5776
public void CanWorkWithOS()

src/Accounts/AssemblyLoading.Test/UnitTests/ConditionalAssemblyProviderTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void CanGetAssembliesOnWindowsPowerShell()
3535
var context = new MockConditionalAssemblyContext()
3636
{
3737
OS = OSPlatform.Windows,
38+
PSEdition = Constants.PSEditionDesktop,
3839
PSVersion = Version.Parse("5.1.22621.608"),
3940
OSArchitecture = Architecture.X64
4041
};
@@ -57,6 +58,7 @@ public void CanGetAssembliesOnPowerShellCorePlus()
5758
var context = new MockConditionalAssemblyContext()
5859
{
5960
OS = OSPlatform.Windows,
61+
PSEdition = Constants.PSEditionCore,
6062
PSVersion = Version.Parse("7.3.0"),
6163
OSArchitecture = Architecture.X64
6264
};

src/Accounts/AssemblyLoading.Test/UnitTests/ConditionalAssemblyTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ public void ShouldLoadAssemblyAccordingToPSVersion()
3737
{
3838
// windows powershell
3939
var context = new MockConditionalAssemblyContext()
40-
{ PSVersion = Version.Parse("5.1.22621.608") };
40+
{
41+
PSEdition = Constants.PSEditionDesktop,
42+
PSVersion = Version.Parse("5.1.22621.608")
43+
};
4144
var windowsPSAssembly = NewDummyAssembly(context).WithWindowsPowerShell();
4245
var psCoreAssembly = NewDummyAssembly(context).WithPowerShellCore();
4346
var neturalAssembly = NewDummyAssembly(context);
@@ -46,6 +49,7 @@ public void ShouldLoadAssemblyAccordingToPSVersion()
4649
Assert.True(neturalAssembly.ShouldLoad);
4750

4851
// powershell core and 7+
52+
context.PSEdition = Constants.PSEditionCore;
4953
context.PSVersion = Version.Parse("7.3.0");
5054
windowsPSAssembly = NewDummyAssembly(context).WithWindowsPowerShell();
5155
psCoreAssembly = NewDummyAssembly(context).WithPowerShellCore();

src/Accounts/AssemblyLoading/ConditionalAssemblyContext.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ namespace Microsoft.Azure.PowerShell.AssemblyLoading
2020
/// <inheritdoc/>
2121
public class ConditionalAssemblyContext : IConditionalAssemblyContext
2222
{
23-
public ConditionalAssemblyContext(Version psVersion)
23+
public ConditionalAssemblyContext(string psEdition, Version psVersion)
2424
{
25+
PSEdition = psEdition;
2526
PSVersion = psVersion;
2627
}
2728

29+
/// <inheritdoc/>
30+
public string PSEdition { get; private set; }
31+
2832
/// <inheritdoc/>
2933
public Version PSVersion { get; private set; }
3034

src/Accounts/AssemblyLoading/ConditionalAssemblyExtensions.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,24 @@ public static class ConditionalAssemblyExtensions
2727
/// </summary>
2828
public static IConditionalAssembly WithWindowsPowerShell(this IConditionalAssembly assembly)
2929
{
30-
return assembly.WithPowerShellVersion(new Version("5.0.0"), new Version("6.0.0"));
30+
// In PowerShell 4 and below, this variable does not exist.
31+
// $PSEdition being null should be treated as the same as having the value Desktop.
32+
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_editions?view=powershell-7.3
33+
var psEdition = assembly.Context.PSEdition ?? Constants.PSEditionDesktop;
34+
bool shouldLoad = psEdition.Equals(Constants.PSEditionDesktop, StringComparison.OrdinalIgnoreCase);
35+
assembly.UpdateShouldLoad(shouldLoad);
36+
return assembly;
3137
}
3238

3339
/// <summary>
3440
/// The given assembly should be loaded in PowerShell Core (6+).
3541
/// </summary>
3642
public static IConditionalAssembly WithPowerShellCore(this IConditionalAssembly assembly)
3743
{
38-
return assembly.WithPowerShellVersion(new Version("6.0.0"));
44+
var psEdition = assembly.Context.PSEdition ?? Constants.PSEditionDesktop;
45+
bool shouldLoad = psEdition.Equals(Constants.PSEditionCore, StringComparison.OrdinalIgnoreCase);
46+
assembly.UpdateShouldLoad(shouldLoad);
47+
return assembly;
3948
}
4049

4150
/// <summary>
@@ -47,10 +56,19 @@ public static IConditionalAssembly WithPowerShellCore(this IConditionalAssembly
4756
/// <param name="upper">Upper limit of PowerShell version, exclusive.</param>
4857
public static IConditionalAssembly WithPowerShellVersion(this IConditionalAssembly assembly, Version lower, Version upper = null)
4958
{
50-
bool shouldLoad = lower <= assembly.Context.PSVersion;
51-
if (upper != null)
59+
bool shouldLoad;
60+
var psVersion = assembly.Context.PSVersion;
61+
if (psVersion == null)
62+
{
63+
shouldLoad = false;
64+
}
65+
else
5266
{
53-
shouldLoad = shouldLoad && assembly.Context.PSVersion < upper;
67+
shouldLoad = lower <= assembly.Context.PSVersion;
68+
if (upper != null)
69+
{
70+
shouldLoad = shouldLoad && assembly.Context.PSVersion < upper;
71+
}
5472
}
5573
assembly.UpdateShouldLoad(shouldLoad);
5674
return assembly;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.PowerShell.AssemblyLoading
16+
{
17+
public class Constants
18+
{
19+
public const string PSEditionDesktop = "Desktop";
20+
public const string PSEditionCore = "Core";
21+
}
22+
}

src/Accounts/AssemblyLoading/IConditionalAssemblyContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace Microsoft.Azure.PowerShell.AssemblyLoading
2323
/// </summary>
2424
public interface IConditionalAssemblyContext
2525
{
26+
/// <summary>
27+
/// Edition of PowerShell, "Desktop" or "Core".
28+
/// </summary>
29+
string PSEdition { get; }
30+
2631
/// <summary>
2732
/// Version of PowerShell. For example "5.1.22621.608".
2833
/// </summary>

src/Accounts/Authentication/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
// You can specify all the values or you can default the Build and Revision Numbers
4444
// by using the '*' as shown below:
4545
// [assembly: AssemblyVersion("1.0.*")]
46-
[assembly: AssemblyVersion("2.11.0")]
47-
[assembly: AssemblyFileVersion("2.11.0")]
46+
[assembly: AssemblyVersion("2.11.1")]
47+
[assembly: AssemblyFileVersion("2.11.1")]
4848
#if !SIGN
4949
[assembly: InternalsVisibleTo("Microsoft.Azure.PowerShell.Authentication.Test")]
5050
#endif

src/Accounts/Authenticators/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@
4848
// You can specify all the values or you can default the Build and Revision Numbers
4949
// by using the '*' as shown below:
5050
// [assembly: AssemblyVersion("1.0.*")]
51-
[assembly: AssemblyVersion("2.11.0")]
52-
[assembly: AssemblyFileVersion("2.11.0")]
51+
[assembly: AssemblyVersion("2.11.1")]
52+
[assembly: AssemblyFileVersion("2.11.1")]

tools/Az/Az.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ DotNetFrameworkVersion = '4.7.2'
5252
# ProcessorArchitecture = ''
5353

5454
# Modules that must be imported into the global environment prior to importing this module
55-
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.11.0'; },
55+
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.11.1'; },
5656
@{ModuleName = 'Az.Advisor'; RequiredVersion = '2.0.0'; },
5757
@{ModuleName = 'Az.Aks'; RequiredVersion = '5.2.0'; },
5858
@{ModuleName = 'Az.AnalysisServices'; RequiredVersion = '1.1.4'; },

tools/AzPreview/AzPreview.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ DotNetFrameworkVersion = '4.7.2'
5252
# ProcessorArchitecture = ''
5353

5454
# Modules that must be imported into the global environment prior to importing this module
55-
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.11.0'; },
55+
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.11.1'; },
5656
@{ModuleName = 'Az.ADDomainServices'; RequiredVersion = '0.2.0'; },
5757
@{ModuleName = 'Az.Advisor'; RequiredVersion = '2.0.0'; },
5858
@{ModuleName = 'Az.Aks'; RequiredVersion = '5.2.0'; },

tools/CheckAssemblies.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function Get-PreloadAssemblies{
2727
Write-Host "Getting preload assemblies in $BuildFolder for $ModuleFolder"
2828
Add-Type -Path ([System.IO.Path]::Combine($BuildFolder, "Az.Accounts", "Microsoft.Azure.PowerShell.AssemblyLoading.dll"))
2929
$assemblyRootPath = [System.IO.Path]::Combine($BuildFolder, "Az.Accounts", "lib")
30-
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($Host.Version)
30+
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($PSVersionTable.PSEdition, $PSVersionTable.PSVersion)
3131
[Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::Initialize($assemblyRootPath, $conditionalAssemblyContext)
3232
$assemblyDict = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::GetAssemblies()
3333
return $assemblyDict.Keys

0 commit comments

Comments
 (0)