Skip to content

Fix Az.Accounts cannot be imported due to assembly not found #20637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2023
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
10 changes: 3 additions & 7 deletions src/Accounts/Accounts/Az.Accounts.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Generated by: Microsoft Corporation
#
# Generated on: 1/6/2023
# Generated on: 1/12/2023
#

@{
Expand All @@ -12,7 +12,7 @@
# RootModule = ''

# Version number of this module.
ModuleVersion = '2.11.0'
ModuleVersion = '2.11.1'

# Supported PSEditions
CompatiblePSEditions = 'Core', 'Desktop'
Expand Down Expand Up @@ -147,11 +147,7 @@ PrivateData = @{
# IconUri = ''

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

# Prerelease string of this module
# Prerelease = ''
Expand Down
3 changes: 3 additions & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

## Upcoming Release

## Version 2.11.1
* Fixed an issue where Az.Accounts cannot be imported correctly. [#20615]

## Version 2.11.0
* Supported Web Account Manager (WAM) as an opt-in interactive login experience. Enable it by `Update-AzConfig -EnableLoginByWam $true`.
* Optimized the mechanism for assembly loading.
Expand Down
4 changes: 2 additions & 2 deletions src/Accounts/Accounts/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:

[assembly: AssemblyVersion("2.11.0")]
[assembly: AssemblyFileVersion("2.11.0")]
[assembly: AssemblyVersion("2.11.1")]
[assembly: AssemblyFileVersion("2.11.1")]
#if !SIGN
[assembly: InternalsVisibleTo("Microsoft.Azure.PowerShell.Cmdlets.Accounts.Test")]
#endif
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
$assemblyRootPath = [System.IO.Path]::Combine($PSScriptRoot, "..", "lib")
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($Host.Version)
Write-Debug "Initializing ConditionalAssemblyContext. PSEdition is [$($PSVersionTable.PSEdition)]. PSVersion is [$($PSVersionTable.PSVersion)]."
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($PSVersionTable.PSEdition, $PSVersionTable.PSVersion)
Write-Debug "Initializing ConditionalAssemblyProvider. AssemblyRootPath is [$assemblyRootPath]."
[Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::Initialize($assemblyRootPath, $conditionalAssemblyContext)

if ($PSEdition -eq 'Desktop') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Microsoft.Azure.PowerShell.AssemblyLoading.Test.Mocks
{
internal class MockConditionalAssemblyContext : IConditionalAssemblyContext
{
public string PSEdition { get; set; }
public Version PSVersion { get; set; }
public Architecture OSArchitecture { get; set; }
public OSPlatform OS { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void CanWorkWithPSVersion()
{
var windowsPSContext = new MockConditionalAssemblyContext()
{
PSEdition = Constants.PSEditionDesktop,
PSVersion = Version.Parse("5.1.22621.608")
};
var windowsPSAssembly = new MockConditionalAssembly(windowsPSContext)
Expand All @@ -40,6 +41,7 @@ public void CanWorkWithPSVersion()

var ps7Context = new MockConditionalAssemblyContext()
{
PSEdition = Constants.PSEditionCore,
PSVersion = Version.Parse("7.3.0")
};
windowsPSAssembly = new MockConditionalAssembly(
Expand All @@ -52,6 +54,23 @@ public void CanWorkWithPSVersion()
Assert.False(windowsPSAssembly.ShouldLoad);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void CanWorkWithEmptyPSEdition()
{
var windowsPSContext = new MockConditionalAssemblyContext()
{
PSVersion = Version.Parse("1.0.0.0")
};
var windowsPSAssembly = new MockConditionalAssembly(windowsPSContext)
.WithWindowsPowerShell();
var psCoreAssembly = new MockConditionalAssembly(
windowsPSContext)
.WithPowerShellCore();
Assert.True(windowsPSAssembly.ShouldLoad);
Assert.False(psCoreAssembly.ShouldLoad);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void CanWorkWithOS()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void CanGetAssembliesOnWindowsPowerShell()
var context = new MockConditionalAssemblyContext()
{
OS = OSPlatform.Windows,
PSEdition = Constants.PSEditionDesktop,
PSVersion = Version.Parse("5.1.22621.608"),
OSArchitecture = Architecture.X64
};
Expand All @@ -57,6 +58,7 @@ public void CanGetAssembliesOnPowerShellCorePlus()
var context = new MockConditionalAssemblyContext()
{
OS = OSPlatform.Windows,
PSEdition = Constants.PSEditionCore,
PSVersion = Version.Parse("7.3.0"),
OSArchitecture = Architecture.X64
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public void ShouldLoadAssemblyAccordingToPSVersion()
{
// windows powershell
var context = new MockConditionalAssemblyContext()
{ PSVersion = Version.Parse("5.1.22621.608") };
{
PSEdition = Constants.PSEditionDesktop,
PSVersion = Version.Parse("5.1.22621.608")
};
var windowsPSAssembly = NewDummyAssembly(context).WithWindowsPowerShell();
var psCoreAssembly = NewDummyAssembly(context).WithPowerShellCore();
var neturalAssembly = NewDummyAssembly(context);
Expand All @@ -46,6 +49,7 @@ public void ShouldLoadAssemblyAccordingToPSVersion()
Assert.True(neturalAssembly.ShouldLoad);

// powershell core and 7+
context.PSEdition = Constants.PSEditionCore;
context.PSVersion = Version.Parse("7.3.0");
windowsPSAssembly = NewDummyAssembly(context).WithWindowsPowerShell();
psCoreAssembly = NewDummyAssembly(context).WithPowerShellCore();
Expand Down
6 changes: 5 additions & 1 deletion src/Accounts/AssemblyLoading/ConditionalAssemblyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,15 @@ namespace Microsoft.Azure.PowerShell.AssemblyLoading
/// <inheritdoc/>
public class ConditionalAssemblyContext : IConditionalAssemblyContext
{
public ConditionalAssemblyContext(Version psVersion)
public ConditionalAssemblyContext(string psEdition, Version psVersion)
{
PSEdition = psEdition;
PSVersion = psVersion;
}

/// <inheritdoc/>
public string PSEdition { get; private set; }

/// <inheritdoc/>
public Version PSVersion { get; private set; }

Expand Down
28 changes: 23 additions & 5 deletions src/Accounts/AssemblyLoading/ConditionalAssemblyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@ public static class ConditionalAssemblyExtensions
/// </summary>
public static IConditionalAssembly WithWindowsPowerShell(this IConditionalAssembly assembly)
{
return assembly.WithPowerShellVersion(new Version("5.0.0"), new Version("6.0.0"));
// In PowerShell 4 and below, this variable does not exist.
// $PSEdition being null should be treated as the same as having the value Desktop.
// https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_editions?view=powershell-7.3
var psEdition = assembly.Context.PSEdition ?? Constants.PSEditionDesktop;
bool shouldLoad = psEdition.Equals(Constants.PSEditionDesktop, StringComparison.OrdinalIgnoreCase);
assembly.UpdateShouldLoad(shouldLoad);
return assembly;
}

/// <summary>
/// The given assembly should be loaded in PowerShell Core (6+).
/// </summary>
public static IConditionalAssembly WithPowerShellCore(this IConditionalAssembly assembly)
{
return assembly.WithPowerShellVersion(new Version("6.0.0"));
var psEdition = assembly.Context.PSEdition ?? Constants.PSEditionDesktop;
bool shouldLoad = psEdition.Equals(Constants.PSEditionCore, StringComparison.OrdinalIgnoreCase);
assembly.UpdateShouldLoad(shouldLoad);
return assembly;
}

/// <summary>
Expand All @@ -47,10 +56,19 @@ public static IConditionalAssembly WithPowerShellCore(this IConditionalAssembly
/// <param name="upper">Upper limit of PowerShell version, exclusive.</param>
public static IConditionalAssembly WithPowerShellVersion(this IConditionalAssembly assembly, Version lower, Version upper = null)
{
bool shouldLoad = lower <= assembly.Context.PSVersion;
if (upper != null)
bool shouldLoad;
var psVersion = assembly.Context.PSVersion;
if (psVersion == null)
{
shouldLoad = false;
}
else
{
shouldLoad = shouldLoad && assembly.Context.PSVersion < upper;
shouldLoad = lower <= assembly.Context.PSVersion;
if (upper != null)
{
shouldLoad = shouldLoad && assembly.Context.PSVersion < upper;
}
}
assembly.UpdateShouldLoad(shouldLoad);
return assembly;
Expand Down
22 changes: 22 additions & 0 deletions src/Accounts/AssemblyLoading/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.PowerShell.AssemblyLoading
{
public class Constants
{
public const string PSEditionDesktop = "Desktop";
public const string PSEditionCore = "Core";
}
}
5 changes: 5 additions & 0 deletions src/Accounts/AssemblyLoading/IConditionalAssemblyContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ namespace Microsoft.Azure.PowerShell.AssemblyLoading
/// </summary>
public interface IConditionalAssemblyContext
{
/// <summary>
/// Edition of PowerShell, "Desktop" or "Core".
/// </summary>
string PSEdition { get; }

/// <summary>
/// Version of PowerShell. For example "5.1.22621.608".
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Accounts/Authentication/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.11.0")]
[assembly: AssemblyFileVersion("2.11.0")]
[assembly: AssemblyVersion("2.11.1")]
[assembly: AssemblyFileVersion("2.11.1")]
#if !SIGN
[assembly: InternalsVisibleTo("Microsoft.Azure.PowerShell.Authentication.Test")]
#endif
4 changes: 2 additions & 2 deletions src/Accounts/Authenticators/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.11.0")]
[assembly: AssemblyFileVersion("2.11.0")]
[assembly: AssemblyVersion("2.11.1")]
[assembly: AssemblyFileVersion("2.11.1")]
2 changes: 1 addition & 1 deletion tools/Az/Az.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ DotNetFrameworkVersion = '4.7.2'
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.11.0'; },
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.11.1'; },
@{ModuleName = 'Az.Advisor'; RequiredVersion = '2.0.0'; },
@{ModuleName = 'Az.Aks'; RequiredVersion = '5.2.0'; },
@{ModuleName = 'Az.AnalysisServices'; RequiredVersion = '1.1.4'; },
Expand Down
2 changes: 1 addition & 1 deletion tools/AzPreview/AzPreview.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ DotNetFrameworkVersion = '4.7.2'
# ProcessorArchitecture = ''

# Modules that must be imported into the global environment prior to importing this module
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.11.0'; },
RequiredModules = @(@{ModuleName = 'Az.Accounts'; ModuleVersion = '2.11.1'; },
@{ModuleName = 'Az.ADDomainServices'; RequiredVersion = '0.2.0'; },
@{ModuleName = 'Az.Advisor'; RequiredVersion = '2.0.0'; },
@{ModuleName = 'Az.Aks'; RequiredVersion = '5.2.0'; },
Expand Down
2 changes: 1 addition & 1 deletion tools/CheckAssemblies.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function Get-PreloadAssemblies{
Write-Host "Getting preload assemblies in $BuildFolder for $ModuleFolder"
Add-Type -Path ([System.IO.Path]::Combine($BuildFolder, "Az.Accounts", "Microsoft.Azure.PowerShell.AssemblyLoading.dll"))
$assemblyRootPath = [System.IO.Path]::Combine($BuildFolder, "Az.Accounts", "lib")
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($Host.Version)
$conditionalAssemblyContext = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyContext]::new($PSVersionTable.PSEdition, $PSVersionTable.PSVersion)
[Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::Initialize($assemblyRootPath, $conditionalAssemblyContext)
$assemblyDict = [Microsoft.Azure.PowerShell.AssemblyLoading.ConditionalAssemblyProvider]::GetAssemblies()
return $assemblyDict.Keys
Expand Down
Loading