Skip to content

Avoid constructing StrongNameKeyPair.#1311

Merged
tom-englert merged 1 commit into
Fody:masterfrom
teo-tsirpanis:sn-fix
Mar 1, 2025
Merged

Avoid constructing StrongNameKeyPair.#1311
tom-englert merged 1 commit into
Fody:masterfrom
teo-tsirpanis:sn-fix

Conversation

@teo-tsirpanis

Copy link
Copy Markdown
Contributor

You should already be a Patron

(I tried becoming one but my card was declined)

Description

This PR uses an alternative way to pass to Cecil the strong-name key pair, without needing to create legacy StrongNameKeyPair objects. This will reduce Fody's reliance on its fork of Mono.Cecil, and unless there are other reasons to use it, I can replace it with the upstream NuGet package in this or a subsequent PR.

The solution

Instead of setting a StrongNameKeyPair to WriterParameters.StrongNameKeyPair, we pass the key file's content directly to the StrongNameKeyBlob property. Cecil will read it in a cross-platform way.

Fody has been using the StrongNameKeyPair.PublicKey property to set the assembly name's public key, and determine if the file actually points to a private key. This PR stops changing the assembly name because I discovered that Cecil already does that, and manually examines the key blob to determine if it is a private key.

Todos

  • Related issues
  • Tests
  • Documentation

@ltrzesniewski

Copy link
Copy Markdown
Member

Thanks!

I'll let the others review this since they implemented the current code in the first place 😅 - I'd just like to know if this PR only aims to improve the implementation, or is there anything the old code didn't handle?

I'll add a few links to related stuff here:

This will reduce Fody's reliance on its fork of Mono.Cecil, and unless there are other reasons to use it, I can replace it with the upstream NuGet package in this or a subsequent PR.

The crypto stuff is the main reason to have a fork. Besides this, Fody only adds [Obsolete("Use BaseModuleWeaver.TypeSystem")] to the TypeSystem class and changes Cecil's strong name to match Fody's.

@tom-englert

Copy link
Copy Markdown
Member

I already had a quick look, but was not sure what the real benefit of this PR is, and how to test.
I think it just simplifies the code, so I'm fine with this.

BTW, the main reason for the fork is to change the strong name to avoid conflicts when Mono.Cecil is already used by the target project.

@tom-englert

Copy link
Copy Markdown
Member

BTW shouldn't we then also remove Mono.Cecil.StrongNameKeyPair from the fork?

@teo-tsirpanis

Copy link
Copy Markdown
Contributor Author

but was not sure what the real benefit of this PR is, and how to test

The benefit of this PR is that we will stop using StrongNameKeyPair and remove the need for patching upstream Cecil (jbevain/cecil#803). Even in .NET Framework its use is a code smell, because Cecil relies on the class' private members.

You can test it by building with dotnet build a project that uses Fody and strong-naming. It seems we have CI coverage?

Fody only adds [Obsolete("Use BaseModuleWeaver.TypeSystem")] to the TypeSystem class

Where is that? I couldn't find it.

changes Cecil's strong name to match Fody's

Fody can keep building Cecil itself, but change the submodule to point to the upstream repository.

BTW shouldn't we then also remove Mono.Cecil.StrongNameKeyPair from the fork?

Yes.

@ltrzesniewski

Copy link
Copy Markdown
Member

Fody only adds [Obsolete("Use BaseModuleWeaver.TypeSystem")] to the TypeSystem class

Where is that? I couldn't find it.

https://github.com/Fody/cecil/blob/6ac9678a8618de548386f6c6e3dd04306cc9dfe7/Mono.Cecil/TypeSystem.cs#L16

@tom-englert
tom-englert merged commit 67bd03d into Fody:master Mar 1, 2025
@teo-tsirpanis
teo-tsirpanis deleted the sn-fix branch March 1, 2025 15:57
andrewlock added a commit to DataDog/dd-trace-dotnet that referenced this pull request Apr 29, 2026
Updated [Fody](https://github.com/Fody/Fody) from 6.8.1 to 6.9.3.

<details>
<summary>Release notes</summary>

_Sourced from [Fody's releases](https://github.com/Fody/Fody/releases)._

## 6.9.3

## What's Changed
* Refactor packaging and remove `Fody.nuspec` #​2. by @​tom-englert in
Fody/Fody#1313
* Clean-up unnecessary dependencies. by @​teo-tsirpanis in
Fody/Fody#1316
* Update Verify.Xunit and xunit.runner.visualstudio by @​GeertvanHorrik
in Fody/Fody#1320
* Prevent failure on updating xsd during build by @​GeertvanHorrik in
Fody/Fody#1319


**Full Changelog**: Fody/Fody@6.9.2...6.9.3

## 6.9.2

## What's Changed
* Avoid constructing `StrongNameKeyPair`. by @​teo-tsirpanis in
Fody/Fody#1311

## New Contributors
* @​teo-tsirpanis made their first contribution in
Fody/Fody#1311

**Full Changelog**: Fody/Fody@6.9.1...6.9.2

## 6.9.1

## What's Changed
* Embed a simplified readme in the nuget package by @​tom-englert in
Fody/Fody#1305


**Full Changelog**: Fody/Fody@6.9.0...6.9.1

## 6.9.0

## What's Changed
* Update cecil to v0.11.6 by @​ltrzesniewski in
Fody/Fody#1304


**Full Changelog**: Fody/Fody@6.8.2...6.9.0

## 6.8.2

## What's Changed
* Bump xunit from 2.8.1 to 2.9.0 by @​dependabot in
Fody/Fody#1281
* Bump xunit.runner.visualstudio from 2.8.1 to 2.8.2 by @​dependabot in
Fody/Fody#1282
* Bump Microsoft.NET.Test.Sdk from 17.9.0 to 17.10.0 by @​dependabot in
Fody/Fody#1272
* Add folder name to test weaver by @​GeertvanHorrik in
Fody/Fody#1298


**Full Changelog**: Fody/Fody@6.8.1...6.8.2

Commits viewable in [compare
view](Fody/Fody@6.8.1...6.9.3).
</details>

[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=Fody&package-manager=nuget&previous-version=6.8.1&new-version=6.9.3)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

You can trigger a rebase of this PR by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

> **Note**
> Automatic rebases have been disabled on this pull request as it has
been open for over 30 days.

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Andrew Lock <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants