Skip to content

AssemblyName.ProcessorArchitecture no longer returns the correct result #77697

Closed as not planned

Description

@ericstj

Description

Starting in .NET 7.0 AssemblyName.ProcessorArchitecture is not correct.

Build tools like ResolveAssemblyReferences in MSBuild used this to determine if an application was referencing an assembly for a different architecture than what it targeted. https://github.com/dotnet/msbuild/blob/3777dcaf7edb3e86a070037ba53e742dd1872873/src/Tasks/AssemblyDependency/ResolveAssemblyReference.cs#L2578-L2599 cc @rainersigwald

I noticed that starting with .NET 7.0 MSBuild stopped warning in this case. I was able to trace it down to #63915 which moved the AssemblyName.GetAssemblyName implementation to use the one in System.Reflection.Metadata

internal AssemblyName GetAssemblyName(StringHandle nameHandle, Version version, StringHandle cultureHandle, BlobHandle publicKeyOrTokenHandle, AssemblyHashAlgorithm assemblyHashAlgorithm, AssemblyFlags flags)
{
string name = GetString(nameHandle);
// compat: normalize Nil culture name to "" to match original behavior of AssemblyName.GetAssemblyName()
string cultureName = (!cultureHandle.IsNil) ? GetString(cultureHandle) : "";
var hashAlgorithm = (Configuration.Assemblies.AssemblyHashAlgorithm)assemblyHashAlgorithm;
// compat: original native implementation used to guarantee that publicKeyOrToken is never null in this scenario.
byte[]? publicKeyOrToken = !publicKeyOrTokenHandle.IsNil ? GetBlobBytes(publicKeyOrTokenHandle) : Array.Empty<byte>();
var assemblyName = new AssemblyName()
{
Name = name,
Version = version,
CultureName = cultureName,
#pragma warning disable SYSLIB0037 // AssemblyName.HashAlgorithm is obsolete
HashAlgorithm = hashAlgorithm,
#pragma warning restore
Flags = GetAssemblyNameFlags(flags),
ContentType = GetContentTypeFromAssemblyFlags(flags)
};
bool hasPublicKey = (flags & AssemblyFlags.PublicKey) != 0;
if (hasPublicKey)
{
assemblyName.SetPublicKey(publicKeyOrToken);
}
else
{
assemblyName.SetPublicKeyToken(publicKeyOrToken);
}
return assemblyName;

That version never set ProcessorArchitecture, for whatever reason, but it could. So that might be the fix for this if we want to fix it.

@jkotas you obsoleted this member in #59061. Given what you said about this, should we also proceed with obsoleting the PlatformTarget support in the compiler and references as well? If not, shouldn't we bring back this part of AssemblyName since our tools support building assemblies with it, and using it?

Reproduction Steps

Run attached project.
archConsole.zip

Expected behavior

It emits Amd64 for both the net6.0 and net7.0 builds.

Actual behavior

It emits Amd64 for the net6.0 build and None for the net7.0 build.

Regression?

Yes, from .NET 6.0

Known Workarounds

Calculate the ProcessorArchitecture of the assembly yourself? This would imply we find all the places in the dotnet tooling (like MSBuild or the compiler) that might use this property and fix them to avoid this regression.

Configuration

.NET 7.0 RC2, x64

Other information

No response

Activity

  1. ericstj commented on Oct 31, 2022

    @ericstj
    MemberAuthor
  2. rainersigwald commented on Oct 31, 2022

    @rainersigwald
    Member

    Related discussion in #74040

  3. added
    untriaged
    on Oct 31, 2022
  4. jkotas commented on Oct 31, 2022

    @jkotas
    Member

    I do not think that this was intentional behavior change. We should be able to make it return what it returned before if it is important. @VSadov ?

    Given what you said about this, should we also proceed with obsoleting the PlatformTarget support in the compiler and references as well?

    The target support has to stay in compiler for as long the compiler targets .NET Framework.

    Compiler targeting is superfluous in .NET Core. It was replaced by RID targeting. Use of compiler targeting in .NET Core only leads to problems. For example, see warning that we have added in dotnet/sdk#27776 .

    Calculate the ProcessorArchitecture of the assembly yourself? This would imply we find all the places in the dotnet tooling (like MSBuild or the compiler) that might use this property and fix them to avoid this regression.

    Note that the code fragment that you have linked won't work well for .NET Framework Arm64 targeting. The only way to make it work well for .NET Framework Arm64 targeting is to switch to reading PE header Machine architecture directly.

  5. VSadov commented on Oct 31, 2022

    @VSadov
    Member

    I do not think that this was intentional behavior change.

    no, I think this should be fixed for compat reasons.

  6. added this to the 7.0.x milestone on Nov 1, 2022
  7. added and removed
    untriaged
    on Nov 1, 2022
  8. ghost added
    in-pr
    on Jan 12, 2023
  9. reopened this on Jan 13, 2023
  10. ghost removed
    in-pr
    on Jan 18, 2023
  11. VSadov commented on Jan 18, 2023

    @VSadov
    Member

    Will keep this open to track backporting to 7.0

  12. reopened this on Jan 18, 2023
  13. ghost added
    in-pr
    on Jan 24, 2023
  14. ghost removed
    in-pr
    on Feb 8, 2023
  15. karelz commented on May 14, 2024

    @karelz
    Member

    @VSadov looks like the backporting to 7.0 didn't happen. Can we close it as 8.0 now?

  16. VSadov commented on May 28, 2024

    @VSadov
    Member

    Yes, since 7.0 is out of support, I think this is no longer actionable.

  17. locked and limited conversation to collaborators on Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions