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;

0 commit comments

Comments
 (0)