-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Validate binding redirects #7153
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
Forgind
merged 28 commits into
dotnet:main
from
Forgind:validate-packages-match-binding-redirects
Jan 7, 2022
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
1eb3176
Hacky, untested binding redirect validation
Forgind ef86539
Revert "Hacky, untested binding redirect validation"
Forgind 11d1b98
Use task to validate packages
Forgind ac03457
Add Source="Foo.cs"
Forgind 288ecc9
Move code into separate task
Forgind 08bd7b5
Make into full class
Forgind 39731bc
Fix up task
Forgind 29e7068
Fix app.config 🙂
Forgind 45d54d4
Opt System.ValueTuple out
Forgind d0b8b5b
Revert "Opt System.ValueTuple out"
Forgind a389c1a
Specifically opt out S.ValueTuple
Forgind 747aa87
Missed a quote
Forgind 354316d
Use String.Equals?
Forgind 5f77395
Move file existence check earlier
Forgind f2c30e5
Somewhat cleaner version
Forgind 60274a2
Add explicit check for System.ValueTuple
Forgind c5583f7
Add copyright header
Forgind 1e4a18e
Learn to use namespaces in XML
Forgind b75a202
Update src/MSBuild/MSBuild.csproj
Forgind cbb140d
Update src/MSBuild/MSBuild.csproj
Forgind 70555c7
Update src/MSBuild/ValidateMSBuildPackageDependencyVersions.cs
Forgind 42a582f
Add comment for ignored assemblies
Forgind 1ecec1c
Merge branch 'validate-packages-match-binding-redirects' of https://g…
Forgind abf550f
Revert "Update src/MSBuild/MSBuild.csproj"
Forgind 9099712
Revert "Update src/MSBuild/MSBuild.csproj"
Forgind 0be049f
Stop batching
Forgind fe5159a
Update comment
Forgind a4f3b4c
Fix comment
Forgind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Reflection; | ||
| using System.Xml; | ||
| namespace MSBuild | ||
| { | ||
| public class ValidateMSBuildPackageDependencyVersions : Task | ||
| { | ||
| [Required] | ||
| public string AppConfig { get; set; } | ||
| [Required] | ||
| public string AssemblyPath { get; set; } | ||
|
|
||
| // Microsoft.Build.Conversion.Core and Microsoft.Build.Engine are deprecated, but they're still used in VS for now. This project doesn't directly reference them, so they don't appear in its output directory. | ||
| // Microsoft.NET.StringTools uses API not available in net35, but since we need it to work for TaskHosts as well, there are simpler versions implemented for that. Ensure it's the right version. | ||
| // Microsoft.Activities.Build and XamlBuildTask are loaded within an AppDomain in the XamlBuildTask after having been loaded from the GAC elsewhere. See https://github.com/dotnet/msbuild/pull/856 | ||
| private string[] assembliesToIgnore = { "Microsoft.Build.Conversion.Core", "Microsoft.NET.StringTools.net35", "Microsoft.Build.Engine", "Microsoft.Activities.Build", "XamlBuildTask" }; | ||
rainersigwald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public override bool Execute() | ||
| { | ||
| XmlDocument doc = new XmlDocument(); | ||
| doc.Load(AppConfig); | ||
| XmlNamespaceManager namespaceManager = new(doc.NameTable); | ||
| namespaceManager.AddNamespace("asm", "urn:schemas-microsoft-com:asm.v1"); | ||
| bool foundSystemValueTuple = false; | ||
| foreach (XmlElement dependentAssemblyElement in doc.DocumentElement.SelectNodes("/configuration/runtime/asm:assemblyBinding/asm:dependentAssembly[asm:assemblyIdentity][asm:bindingRedirect]", namespaceManager)) | ||
| { | ||
| string name = (dependentAssemblyElement.SelectSingleNode("asm:assemblyIdentity", namespaceManager) as XmlElement).GetAttribute("name"); | ||
| string version = (dependentAssemblyElement.SelectSingleNode("asm:bindingRedirect", namespaceManager) as XmlElement).GetAttribute("newVersion"); | ||
| if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(version) && !assembliesToIgnore.Contains(name, StringComparer.OrdinalIgnoreCase)) | ||
| { | ||
| string path = Path.Combine(AssemblyPath, name + ".dll"); | ||
| string assemblyVersion = AssemblyName.GetAssemblyName(path).Version.ToString(); | ||
| if (!version.Equals(assemblyVersion)) | ||
| { | ||
| // Ensure that the binding redirect is to the GAC version, but | ||
| // we still ship the version we explicitly reference to let | ||
| // API consumers bind to it at runtime. | ||
| // See https://github.com/dotnet/msbuild/issues/6976. | ||
| if (String.Equals(name, "System.ValueTuple", StringComparison.OrdinalIgnoreCase) && String.Equals(version, "4.0.0.0") && String.Equals(assemblyVersion, "4.0.3.0")) | ||
| { | ||
| foundSystemValueTuple = true; | ||
| } | ||
| else | ||
| { | ||
| Log.LogError($"Binding redirect for '{name}' redirects to a different version ({version}) than MSBuild ships ({assemblyVersion})."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (!foundSystemValueTuple) | ||
| { | ||
| Log.LogError("Binding redirect for 'System.ValueTuple' missing."); | ||
| } | ||
| return !Log.HasLoggedErrors; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.