Skip to content
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

AssemblyName.ProcessorArchitecture no longer returns the correct result #77697

Closed
ericstj opened this issue Oct 31, 2022 · 8 comments · Fixed by #80581
Closed

AssemblyName.ProcessorArchitecture no longer returns the correct result #77697

ericstj opened this issue Oct 31, 2022 · 8 comments · Fixed by #80581

Comments

@ericstj
Copy link
Member

ericstj commented Oct 31, 2022

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

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ericstj
Copy link
Member Author

ericstj commented Oct 31, 2022

cc @VSadov

@rainersigwald
Copy link
Member

Related discussion in #74040

@ericstj ericstj added the untriaged New issue has not been triaged by the area owner label Oct 31, 2022
@jkotas
Copy link
Member

jkotas commented Oct 31, 2022

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.

@VSadov
Copy link
Member

VSadov commented Oct 31, 2022

I do not think that this was intentional behavior change.

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

@marek-safar marek-safar added this to the 7.0.x milestone Nov 1, 2022
@marek-safar marek-safar added regression-from-last-release and removed untriaged New issue has not been triaged by the area owner labels Nov 1, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 12, 2023
@jkotas jkotas reopened this Jan 13, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 18, 2023
@VSadov
Copy link
Member

VSadov commented Jan 18, 2023

Will keep this open to track backporting to 7.0

@VSadov VSadov reopened this Jan 18, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jan 24, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Feb 8, 2023
@karelz
Copy link
Member

karelz commented May 14, 2024

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

@VSadov
Copy link
Member

VSadov commented May 28, 2024

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

@VSadov VSadov closed this as not planned Won't fix, can't repro, duplicate, stale May 28, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants