From a97ee9e2534ae46c6b3e9cbcd2ebdc84f272078e Mon Sep 17 00:00:00 2001 From: tmat Date: Fri, 7 Apr 2023 12:18:13 -0700 Subject: [PATCH 1/9] Bundle Source Link packages --- eng/Version.Details.xml | 30 ++- eng/Versions.props | 9 + .../SourceLinkTestApp/Directory.Build.props | 2 + .../SourceLinkTestApp/Directory.Build.targets | 2 + .../TestProjects/SourceLinkTestApp/Program.cs | 6 + .../SourceLinkTestApp.csproj | 8 + src/Layout/redist/targets/BundledSdks.targets | 7 + .../redist/targets/RestoreDependency.proj | 21 +- .../targets/Microsoft.NET.Sdk.Common.targets | 2 + .../Microsoft.NET.Sdk.SourceLink.props | 30 +++ .../Microsoft.NET.Sdk.SourceLink.targets | 26 ++ .../targets/Microsoft.NET.Sdk.props | 2 + .../Microsoft.NET.Build.Tests.csproj | 1 + .../SourceLinkTests.cs | 238 ++++++++++++++++++ .../TestAssetsManager.cs | 4 +- 15 files changed, 373 insertions(+), 15 deletions(-) create mode 100644 src/Assets/TestProjects/SourceLinkTestApp/Directory.Build.props create mode 100644 src/Assets/TestProjects/SourceLinkTestApp/Directory.Build.targets create mode 100644 src/Assets/TestProjects/SourceLinkTestApp/Program.cs create mode 100644 src/Assets/TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj create mode 100644 src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props create mode 100644 src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.targets create mode 100644 src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs diff --git a/eng/Version.Details.xml b/eng/Version.Details.xml index 73d35afcc7b8..40e71f029b54 100644 --- a/eng/Version.Details.xml +++ b/eng/Version.Details.xml @@ -274,6 +274,31 @@ https://github.com/dotnet/deployment-tools b60c95e1ce736630d17e16626c59e3dd85ebae2b + + https://github.com/dotnet/sourcelink + 759f344923a0859f3fae83431d0ba1cc62108118 + + + + https://github.com/dotnet/sourcelink + 759f344923a0859f3fae83431d0ba1cc62108118 + + + https://github.com/dotnet/sourcelink + 759f344923a0859f3fae83431d0ba1cc62108118 + + + https://github.com/dotnet/sourcelink + 759f344923a0859f3fae83431d0ba1cc62108118 + + + https://github.com/dotnet/sourcelink + 759f344923a0859f3fae83431d0ba1cc62108118 + + + https://github.com/dotnet/sourcelink + 759f344923a0859f3fae83431d0ba1cc62108118 + @@ -288,11 +313,6 @@ 17d9eee32f20a6af0ebb620254a22f601d159578 - - https://github.com/dotnet/sourcelink - 759f344923a0859f3fae83431d0ba1cc62108118 - - https://github.com/dotnet/arcade 17d9eee32f20a6af0ebb620254a22f601d159578 diff --git a/eng/Versions.props b/eng/Versions.props index 7ed0fea50a17..17e54786e71f 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -174,6 +174,15 @@ 1.0.0-beta.23206.1 + + + 1.2.0-beta-23207-01 + 1.2.0-beta-23207-01 + 1.2.0-beta-23207-01 + 1.2.0-beta-23207-01 + 1.2.0-beta-23207-01 + 1.2.0-beta-23207-01 + true diff --git a/src/Assets/TestProjects/SourceLinkTestApp/Directory.Build.props b/src/Assets/TestProjects/SourceLinkTestApp/Directory.Build.props new file mode 100644 index 000000000000..c1df2220ddc6 --- /dev/null +++ b/src/Assets/TestProjects/SourceLinkTestApp/Directory.Build.props @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/src/Assets/TestProjects/SourceLinkTestApp/Directory.Build.targets b/src/Assets/TestProjects/SourceLinkTestApp/Directory.Build.targets new file mode 100644 index 000000000000..c1df2220ddc6 --- /dev/null +++ b/src/Assets/TestProjects/SourceLinkTestApp/Directory.Build.targets @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/src/Assets/TestProjects/SourceLinkTestApp/Program.cs b/src/Assets/TestProjects/SourceLinkTestApp/Program.cs new file mode 100644 index 000000000000..ebc51ad50005 --- /dev/null +++ b/src/Assets/TestProjects/SourceLinkTestApp/Program.cs @@ -0,0 +1,6 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; + +Console.WriteLine("Hello World!"); \ No newline at end of file diff --git a/src/Assets/TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj b/src/Assets/TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj new file mode 100644 index 000000000000..e3a5924a1691 --- /dev/null +++ b/src/Assets/TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj @@ -0,0 +1,8 @@ + + + $(MSBuildThisFileDirectory)..\..\..\artifacts\.nuget\packages + true + $(CurrentTargetFramework) + Exe + + diff --git a/src/Layout/redist/targets/BundledSdks.targets b/src/Layout/redist/targets/BundledSdks.targets index 4ce963aa7eaa..2888625dad7f 100644 --- a/src/Layout/redist/targets/BundledSdks.targets +++ b/src/Layout/redist/targets/BundledSdks.targets @@ -4,5 +4,12 @@ + + + + + + + diff --git a/src/Layout/redist/targets/RestoreDependency.proj b/src/Layout/redist/targets/RestoreDependency.proj index 671413e577f6..e3308ce93a93 100644 --- a/src/Layout/redist/targets/RestoreDependency.proj +++ b/src/Layout/redist/targets/RestoreDependency.proj @@ -1,4 +1,4 @@ - + + + $(MSBuildThisFileDirectory)..\tools\ net8.0 diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props new file mode 100644 index 000000000000..0a612ad1cda2 --- /dev/null +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props @@ -0,0 +1,30 @@ + + + + + + <_SourceLinkSdkSubDir>build + <_SourceLinkSdkSubDir Condition="'$(IsCrossTargetingBuild)' == 'true'">buildMultiTargeting + + + true + + + + + + + + + + diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.targets new file mode 100644 index 000000000000..033a86b990ab --- /dev/null +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.targets @@ -0,0 +1,26 @@ + + + + + + true + + + + + + + + + + diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.props b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.props index 2bb849977aba..5f4c342792c9 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.props +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.props @@ -157,6 +157,8 @@ Copyright (c) .NET Foundation. All rights reserved. + + diff --git a/src/Tests/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj b/src/Tests/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj index b536cb66226c..9ea2a9b2e710 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj +++ b/src/Tests/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj @@ -4,6 +4,7 @@ false Tests\$(MSBuildProjectName) + true diff --git a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs new file mode 100644 index 000000000000..3b8d93ec0ce6 --- /dev/null +++ b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs @@ -0,0 +1,238 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System; +using System.IO; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Xml.Linq; +using FluentAssertions; +using Microsoft.NET.TestFramework; +using Microsoft.NET.TestFramework.Assertions; +using Microsoft.NET.TestFramework.Commands; +using Xunit; +using Xunit.Abstractions; + +namespace Microsoft.NET.Build.Tests +{ + public class SourceLinkTests : SdkTest + { + private static readonly Guid s_embeddedSourceKindGuid = new("0E8A571B-6926-466E-B4AD-8AB04611F5FE"); + + public SourceLinkTests(ITestOutputHelper log) + : base(log) + { + } + + private void CreateGitFiles(string repoDir, string originUrl, string commitSha = "1200000000000000000000000000000000000000") + { + var gitDir = Path.Combine(repoDir, ".git"); + var headsDir = Path.Combine(gitDir, "refs", "heads"); + + Directory.CreateDirectory(gitDir); + File.WriteAllText(Path.Combine(gitDir, "HEAD"), "ref: refs/heads/master"); + Directory.CreateDirectory(headsDir); + + if (commitSha != null) + { + File.WriteAllText(Path.Combine(headsDir, "master"), commitSha); + } + + if (originUrl != null) + { + File.WriteAllText(Path.Combine(gitDir, "config"), $""" + [remote "origin"] + url = {originUrl} + """); + } + + File.WriteAllText(Path.Combine(repoDir, ".gitignore"), """ + [Bb]in/ + [Oo]bj/ + """); + } + + private unsafe void ValidatePdb(string pdbPath, bool expectedEmbeddedSources) + { + // Validates that *.AssemblyAttributes.cs file is embedded in the PDB. + + var pdb = File.ReadAllBytes(pdbPath); + fixed (byte* pdbPtr = pdb) + { + var mdReader = new MetadataReader(pdbPtr, pdb.Length); + var attrDocHandle = mdReader.Documents.Single(h => mdReader.GetString(mdReader.GetDocument(h).Name).EndsWith(".AssemblyAttributes.cs")); + var cdis = mdReader.GetCustomDebugInformation(attrDocHandle); + + Assert.Equal(expectedEmbeddedSources, cdis.Any(h => mdReader.GetGuid(mdReader.GetCustomDebugInformation(h).Kind) == s_embeddedSourceKindGuid)); + } + } + + [Fact] + public void WithNoGitMetadata() + { + // We need to copy the test project to a directory outside of the SDK repo, + // otherwise we would find .git directory in the SDK repo root. + + var testAsset = _testAssetsManager + .CopyTestAsset("SourceLinkTestApp", testDestinationDirectory: Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())) + .WithSource(); + + var buildCommand = new BuildCommand(testAsset); + buildCommand.Execute().Should().Pass(); + + var intermediateDir = buildCommand.GetIntermediateDirectory(); + intermediateDir.Should().NotHaveFile("SourceLinkTestApp.sourcelink.json"); + } + + /// + /// When creating a new repository locally we want the build to work and not report warnings even before the remote is set. + /// + [Fact] + public void WithNoRemoteNoCommit() + { + var testAsset = _testAssetsManager + .CopyTestAsset("SourceLinkTestApp") + .WithSource(); + + CreateGitFiles(testAsset.Path, originUrl: null, commitSha: null); + + var buildCommand = new BuildCommand(testAsset); + buildCommand.Execute().Should().HaveStdOutContaining($"warning : Repository '{testAsset.Path}' has no remote."); + + var intermediateDir = buildCommand.GetIntermediateDirectory(); + intermediateDir.Should().NotHaveFile("SourceLinkTestApp.sourcelink.json"); + } + + /// + /// When creating a new repository locally we want the build to work and not report warnings even before the remote is set. + /// + [Fact] + public void WithNoRemote() + { + var testAsset = _testAssetsManager + .CopyTestAsset("SourceLinkTestApp") + .WithSource(); + + CreateGitFiles(testAsset.Path, originUrl: null); + + var buildCommand = new BuildCommand(testAsset); + buildCommand.Execute().Should().HaveStdOutContaining($"warning : Repository '{testAsset.Path}' has no remote."); + + var intermediateDir = buildCommand.GetIntermediateDirectory(); + intermediateDir.Should().NotHaveFile("SourceLinkTestApp.sourcelink.json"); + } + + [Fact] + public void WithRemoteOrigin_UnknownDomain() + { + var testAsset = _testAssetsManager + .CopyTestAsset("SourceLinkTestApp") + .WithSource(); + + CreateGitFiles(testAsset.Path, originUrl: "https://contoso.com"); + + var buildCommand = new BuildCommand(testAsset) + { + WorkingDirectory = testAsset.Path + }; + + var result = buildCommand.Execute().Should().Pass(); + + var intermediateDir = buildCommand.GetIntermediateDirectory(); + intermediateDir.Should().NotHaveFile("SourceLinkTestApp.sourcelink.json"); + } + + [Theory] + [InlineData("https://github.com/org/repo", "https://raw.githubusercontent.com/org/repo/1200000000000000000000000000000000000000/*")] + [InlineData("https://gitlab.com/org/repo", "https://gitlab.com/org/repo/-/raw/1200000000000000000000000000000000000000/*")] + [InlineData("https://bitbucket.org/org/repo", "https://api.bitbucket.org/2.0/repositories/org/repo/src/1200000000000000000000000000000000000000/*")] + [InlineData("https://test.visualstudio.com/org/_git/repo", "https://test.visualstudio.com/org/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=1200000000000000000000000000000000000000&path=/*")] + public void WithRemoteOrigin_KnownDomain(string origin, string expectedLink) + { + var testAsset = _testAssetsManager + .CopyTestAsset("SourceLinkTestApp", identifier: origin) + .WithSource(); + + CreateGitFiles(testAsset.Path, origin); + + var buildCommand = new BuildCommand(testAsset) + { + WorkingDirectory = testAsset.Path + }; + + var result = buildCommand.Execute().Should().Pass(); + + var intermediateDir = buildCommand.GetIntermediateDirectory(); + var sourceLinkFilePath = Path.Combine(intermediateDir.FullName, "SourceLinkTestApp.sourcelink.json"); + var actualContent = File.ReadAllText(sourceLinkFilePath, Encoding.UTF8); + var expectedPattern = Path.Combine(testAsset.Path, "*").Replace("\\", "\\\\"); + + Assert.Equal($$$"""{"documents":{"{{{expectedPattern}}}":"{{{expectedLink}}}"}}""", actualContent); + + ValidatePdb(Path.Combine(intermediateDir.FullName, "SourceLinkTestApp.pdb"), expectedEmbeddedSources: true); + } + + [Fact] + public void SuppressImplicitGitSourceLink_SetExplicitly() + { + var testAsset = _testAssetsManager + .CopyTestAsset("SourceLinkTestApp") + .WithSource().WithProjectChanges(p => + { + var ns = p.Root.Name.Namespace; + + var propertyGroup = new XElement(ns + "PropertyGroup"); + p.Root.Add(propertyGroup); + + propertyGroup.Add(new XElement(ns + "SuppressImplicitGitSourceLink", "true")); + }); + + CreateGitFiles(testAsset.Path, "https://github.com/org/repo"); + + var buildCommand = new BuildCommand(testAsset) + { + WorkingDirectory = testAsset.Path + }; + + var result = buildCommand.Execute().Should().Pass(); + + var intermediateDir = buildCommand.GetIntermediateDirectory(); + intermediateDir.Should().NotHaveFile("SourceLinkTestApp.sourcelink.json"); + } + + [Fact] + public void SuppressImplicitGitSourceLink_ExplicitPackage() + { + var testAsset = _testAssetsManager + .CopyTestAsset("SourceLinkTestApp") + .WithSource() + .WithProjectChanges(p => + { + var ns = p.Root.Name.Namespace; + + var itemGroup = new XElement(ns + "ItemGroup"); + p.Root.Add(itemGroup); + + itemGroup.Add(new XElement(ns + "PackageReference", + new XAttribute("Include", "Microsoft.SourceLink.GitHub"), + new XAttribute("Version", "1.0.0"))); + }); + + CreateGitFiles(testAsset.Path, "https://github.com/org/repo"); + + var buildCommand = new BuildCommand(testAsset) + { + WorkingDirectory = testAsset.Path + }; + + var result = buildCommand.Execute().Should().Pass(); + + var intermediateDir = buildCommand.GetIntermediateDirectory(); + intermediateDir.Should().HaveFile("SourceLinkTestApp.sourcelink.json"); + + // EmbedUntrackedSources is not set by default by SourceLink v1.0.0 package: + ValidatePdb(Path.Combine(intermediateDir.FullName, "SourceLinkTestApp.pdb"), expectedEmbeddedSources: false); + } + } +} diff --git a/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs b/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs index e239e27c1d13..64d5ee549b45 100644 --- a/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -43,13 +43,13 @@ public TestAsset CopyTestAsset( [CallerFilePath] string callerFilePath = null, string identifier = "", string testAssetSubdirectory = "", + string testDestinationDirectory = null, bool allowCopyIfPresent = false) { var testProjectDirectory = GetAndValidateTestProjectDirectory(testProjectName, testAssetSubdirectory); var fileName = Path.GetFileNameWithoutExtension(callerFilePath); - var testDestinationDirectory = - GetTestDestinationDirectoryPath(testProjectName, callingMethod + "_" + fileName, identifier, allowCopyIfPresent); + testDestinationDirectory ??= GetTestDestinationDirectoryPath(testProjectName, callingMethod + "_" + fileName, identifier, allowCopyIfPresent); TestDestinationDirectories.Add(testDestinationDirectory); var testAsset = new TestAsset(testProjectDirectory, testDestinationDirectory, TestContext.Current.SdkVersion, Log); From bcc5923a5334969acca7825026c9fe829ecefcc3 Mon Sep 17 00:00:00 2001 From: tmat Date: Fri, 7 Apr 2023 14:29:03 -0700 Subject: [PATCH 2/9] Feedback --- .../TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj | 1 - src/Layout/redist/targets/RestoreDependency.proj | 4 +--- .../targets/Microsoft.NET.Sdk.SourceLink.props | 2 +- .../targets/Microsoft.NET.Sdk.SourceLink.targets | 2 +- src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs | 4 ++-- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Assets/TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj b/src/Assets/TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj index e3a5924a1691..213a534d1e60 100644 --- a/src/Assets/TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj +++ b/src/Assets/TestProjects/SourceLinkTestApp/SourceLinkTestApp.csproj @@ -1,6 +1,5 @@  - $(MSBuildThisFileDirectory)..\..\..\artifacts\.nuget\packages true $(CurrentTargetFramework) Exe diff --git a/src/Layout/redist/targets/RestoreDependency.proj b/src/Layout/redist/targets/RestoreDependency.proj index e3308ce93a93..bb9d05098845 100644 --- a/src/Layout/redist/targets/RestoreDependency.proj +++ b/src/Layout/redist/targets/RestoreDependency.proj @@ -10,8 +10,7 @@ --> @@ -33,7 +32,6 @@ diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props index 0a612ad1cda2..bcd63bda473c 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props @@ -1,6 +1,6 @@ - - $(MSBuildThisFileDirectory)..\tools\ net8.0 diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CrossTargeting.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CrossTargeting.targets index 350beee8d03a..b6affa9c19c9 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CrossTargeting.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.CrossTargeting.targets @@ -12,6 +12,7 @@ Copyright (c) .NET Foundation. All rights reserved. + + + true diff --git a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs index 30db29c2bc8b..e3f6cff81da9 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs @@ -68,6 +68,40 @@ private unsafe void ValidatePdb(string pdbPath, bool expectedEmbeddedSources) } } + private static TestAsset Multitarget(TestAsset testAsset, string targetFrameworks) + => testAsset.WithProjectChanges(p => + { + var tfmNode = p.Root.Descendants().Single(e => e.Name.LocalName == "TargetFramework"); + tfmNode.Name = p.Root.Name.Namespace + "TargetFrameworks"; + tfmNode.Value = targetFrameworks; + }); + + private static TestAsset WithProperties(TestAsset testAsset, params (string key, string value)[] properties) + => testAsset.WithProjectChanges(p => + { + var ns = p.Root.Name.Namespace; + var propertyGroup = p.Root.Descendants(ns + "PropertyGroup").First(); + + foreach (var (key, value) in properties) + { + propertyGroup.Add(new XElement(ns + key, value)); + } + }); + + private static TestAsset WithItems(TestAsset testAsset, params (string key, XAttribute[] attributes)[] items) + => testAsset.WithProjectChanges(p => + { + var ns = p.Root.Name.Namespace; + + var itemGroup = new XElement(ns + "ItemGroup"); + p.Root.Add(itemGroup); + + foreach (var (key, attributes) in items) + { + itemGroup.Add(new XElement(ns + key, (object[])attributes)); + } + }); + [Fact] public void WithNoGitMetadata() { @@ -151,24 +185,16 @@ public void WithRemoteOrigin_UnknownDomain() [InlineData("https://test.visualstudio.com/org/_git/repo", "https://test.visualstudio.com/org/_apis/git/repositories/repo/items?api-version=1.0&versionType=commit&version=1200000000000000000000000000000000000000&path=/*")] public void WithRemoteOrigin_KnownDomain(string origin, string expectedLink, bool multitarget = false) { - string targetFrameworks = null; + string targetFrameworks = ToolsetInfo.CurrentTargetFramework + (multitarget ? ";netstandard2.0" : ""); var testAsset = _testAssetsManager .CopyTestAsset("SourceLinkTestApp", identifier: origin) - .WithSource() - .WithProjectChanges(p => - { - var ns = p.Root.Name.Namespace; - var tfmNode = p.Root.Descendants().Single(e => e.Name.LocalName == "TargetFramework"); - - targetFrameworks = tfmNode.Value; - if (multitarget) - { - tfmNode.Name = ns + "TargetFrameworks"; - targetFrameworks += ";netstandard2.0"; - tfmNode.Value = targetFrameworks; - } - }); + .WithSource(); + + if (multitarget) + { + testAsset = Multitarget(testAsset, targetFrameworks); + } Assert.NotNull(targetFrameworks); @@ -199,29 +225,18 @@ public void WithRemoteOrigin_KnownDomain(string origin, string expectedLink, boo [InlineData(false)] public void SuppressImplicitGitSourceLink_SetExplicitly(bool multitarget) { - string targetFrameworks = null; + string targetFrameworks = ToolsetInfo.CurrentTargetFramework + (multitarget ? ";netstandard2.0" : ""); var testAsset = _testAssetsManager .CopyTestAsset("SourceLinkTestApp") - .WithSource() - .WithProjectChanges(p => - { - var ns = p.Root.Name.Namespace; - var tfmNode = p.Root.Descendants().Single(e => e.Name.LocalName == "TargetFramework"); - - targetFrameworks = tfmNode.Value; - if (multitarget) - { - tfmNode.Name = ns + "TargetFrameworks"; - targetFrameworks += ";netstandard2.0"; - tfmNode.Value = targetFrameworks; - } + .WithSource(); - var propertyGroup = new XElement(ns + "PropertyGroup"); - p.Root.Add(propertyGroup); + testAsset = WithProperties(testAsset, ("SuppressImplicitGitSourceLink", "true")); - propertyGroup.Add(new XElement(ns + "SuppressImplicitGitSourceLink", "true")); - }); + if (multitarget) + { + testAsset = Multitarget(testAsset, targetFrameworks); + } Assert.NotNull(targetFrameworks); @@ -246,31 +261,18 @@ public void SuppressImplicitGitSourceLink_SetExplicitly(bool multitarget) [InlineData(false)] public void SuppressImplicitGitSourceLink_ExplicitPackage(bool multitarget) { - string targetFrameworks = null; + string targetFrameworks = ToolsetInfo.CurrentTargetFramework + (multitarget ? ";netstandard2.0" : ""); var testAsset = _testAssetsManager .CopyTestAsset("SourceLinkTestApp") - .WithSource() - .WithProjectChanges(p => - { - var ns = p.Root.Name.Namespace; - var tfmNode = p.Root.Descendants().Single(e => e.Name.LocalName == "TargetFramework"); - - targetFrameworks = tfmNode.Value; - if (multitarget) - { - tfmNode.Name = ns + "TargetFrameworks"; - targetFrameworks += ";netstandard2.0"; - tfmNode.Value = targetFrameworks; - } + .WithSource(); - var itemGroup = new XElement(ns + "ItemGroup"); - p.Root.Add(itemGroup); + if (multitarget) + { + testAsset = Multitarget(testAsset, targetFrameworks); + } - itemGroup.Add(new XElement(ns + "PackageReference", - new XAttribute("Include", "Microsoft.SourceLink.GitHub"), - new XAttribute("Version", "1.0.0"))); - }); + testAsset = WithItems(testAsset, ("PackageReference", new[] { new XAttribute("Include", "Microsoft.SourceLink.GitHub"), new XAttribute("Version", "1.0.0") })); Assert.NotNull(targetFrameworks); @@ -292,5 +294,33 @@ public void SuppressImplicitGitSourceLink_ExplicitPackage(bool multitarget) ValidatePdb(Path.Combine(intermediateDir.FullName, "SourceLinkTestApp.pdb"), expectedEmbeddedSources: false); } } + + [FullMSBuildOnlyFact] + public void Cpp() + { + var testAsset = _testAssetsManager + .CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib") + .WithSource(); + + testAsset = WithProperties(testAsset, ("EnableManagedpackageReferenceSupport", "true")); + + CreateGitFiles(testAsset.Path, "https://github.com/a/b"); + + var buildCommand = new BuildCommand(testAsset, "NETCoreCppCliTest") + { + WorkingDirectory = testAsset.Path + }; + + buildCommand.Execute("-p:Platform=x64").Should().Pass(); + + var outputDir = Path.Combine(testAsset.Path, "NETCoreCppCliTest", "x64", "Debug"); + var sourceLinkFilePath = Path.Combine(outputDir, "NETCoreCppCliTest.sourcelink.json"); + var actualContent = File.ReadAllText(sourceLinkFilePath, Encoding.UTF8); + var expectedSourceLink = """{"documents":{"https://github.com/org/repo":"https://raw.githubusercontent.com/org/repo/1200000000000000000000000000000000000000/*"}}"""; + Assert.Equal(expectedSourceLink, actualContent); + + var pdbText = File.ReadAllText(Path.Combine(outputDir, "NETCoreCppCliTest.pdb"), Encoding.UTF8); + Assert.Contains(expectedSourceLink, pdbText); + } } } From 2722041323b62b137a958d4ac4cb7b3c7b960001 Mon Sep 17 00:00:00 2001 From: tmat Date: Mon, 10 Apr 2023 08:56:22 -0700 Subject: [PATCH 6/9] Fix --- src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs index e3f6cff81da9..da9c777203e8 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs @@ -316,7 +316,8 @@ public void Cpp() var outputDir = Path.Combine(testAsset.Path, "NETCoreCppCliTest", "x64", "Debug"); var sourceLinkFilePath = Path.Combine(outputDir, "NETCoreCppCliTest.sourcelink.json"); var actualContent = File.ReadAllText(sourceLinkFilePath, Encoding.UTF8); - var expectedSourceLink = """{"documents":{"https://github.com/org/repo":"https://raw.githubusercontent.com/org/repo/1200000000000000000000000000000000000000/*"}}"""; + var expectedPattern = Path.Combine(testAsset.Path, "*").Replace("\\", "\\\\"); + var expectedSourceLink = $$$"""{"documents":{"{{{expectedPattern}}}":"https://raw.githubusercontent.com/org/repo/1200000000000000000000000000000000000000/*"}}"""; Assert.Equal(expectedSourceLink, actualContent); var pdbText = File.ReadAllText(Path.Combine(outputDir, "NETCoreCppCliTest.pdb"), Encoding.UTF8); From a2eed8985b99b1908ea6db4158188aa0f2d63c9a Mon Sep 17 00:00:00 2001 From: tmat Date: Mon, 10 Apr 2023 14:30:53 -0700 Subject: [PATCH 7/9] Fix --- src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs index da9c777203e8..2dc3c001eb0a 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs @@ -304,7 +304,7 @@ public void Cpp() testAsset = WithProperties(testAsset, ("EnableManagedpackageReferenceSupport", "true")); - CreateGitFiles(testAsset.Path, "https://github.com/a/b"); + CreateGitFiles(testAsset.Path, "https://github.com/org/repo"); var buildCommand = new BuildCommand(testAsset, "NETCoreCppCliTest") { From 4e881c6511e7ffc37cd02fd43e68acf7982c488d Mon Sep 17 00:00:00 2001 From: tmat Date: Thu, 13 Apr 2023 10:43:30 -0700 Subject: [PATCH 8/9] Fix versions --- eng/Versions.props | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index 17e54786e71f..59b314468abc 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -176,12 +176,12 @@ - 1.2.0-beta-23207-01 - 1.2.0-beta-23207-01 - 1.2.0-beta-23207-01 - 1.2.0-beta-23207-01 - 1.2.0-beta-23207-01 - 1.2.0-beta-23207-01 + 8.0.0-beta.23210.3 + 8.0.0-beta.23210.3 + 8.0.0-beta.23210.3 + 8.0.0-beta.23210.3 + 8.0.0-beta.23210.3 + 8.0.0-beta.23210.3 From 1d0dbb20591c3d6ab403856b773f5489ad249eed Mon Sep 17 00:00:00 2001 From: tmat Date: Fri, 14 Apr 2023 09:07:31 -0700 Subject: [PATCH 9/9] Feedback --- .../Microsoft.NET.Sdk.SourceLink.props | 14 +++++++------ .../SourceLinkTests.cs | 20 +++++++------------ 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props index bcd63bda473c..d04371ecb88a 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.SourceLink.props @@ -20,11 +20,13 @@ Copyright (c) .NET Foundation. All rights reserved. true - - - - - - + + + + + + + + diff --git a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs index 2dc3c001eb0a..3188f9fd7499 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/SourceLinkTests.cs @@ -171,7 +171,7 @@ public void WithRemoteOrigin_UnknownDomain() WorkingDirectory = testAsset.Path }; - var result = buildCommand.Execute().Should().Pass(); + buildCommand.Execute().Should().Pass(); var intermediateDir = buildCommand.GetIntermediateDirectory(); intermediateDir.Should().NotHaveFile("SourceLinkTestApp.sourcelink.json"); @@ -196,8 +196,6 @@ public void WithRemoteOrigin_KnownDomain(string origin, string expectedLink, boo testAsset = Multitarget(testAsset, targetFrameworks); } - Assert.NotNull(targetFrameworks); - CreateGitFiles(testAsset.Path, origin); var buildCommand = new BuildCommand(testAsset) @@ -205,7 +203,7 @@ public void WithRemoteOrigin_KnownDomain(string origin, string expectedLink, boo WorkingDirectory = testAsset.Path }; - var result = buildCommand.Execute().Should().Pass(); + buildCommand.Execute().Should().Pass(); foreach (var targetFramework in targetFrameworks.Split(';')) { @@ -228,7 +226,7 @@ public void SuppressImplicitGitSourceLink_SetExplicitly(bool multitarget) string targetFrameworks = ToolsetInfo.CurrentTargetFramework + (multitarget ? ";netstandard2.0" : ""); var testAsset = _testAssetsManager - .CopyTestAsset("SourceLinkTestApp") + .CopyTestAsset("SourceLinkTestApp", identifier: multitarget.ToString()) .WithSource(); testAsset = WithProperties(testAsset, ("SuppressImplicitGitSourceLink", "true")); @@ -238,8 +236,6 @@ public void SuppressImplicitGitSourceLink_SetExplicitly(bool multitarget) testAsset = Multitarget(testAsset, targetFrameworks); } - Assert.NotNull(targetFrameworks); - CreateGitFiles(testAsset.Path, "https://github.com/org/repo"); var buildCommand = new BuildCommand(testAsset) @@ -247,7 +243,7 @@ public void SuppressImplicitGitSourceLink_SetExplicitly(bool multitarget) WorkingDirectory = testAsset.Path }; - var result = buildCommand.Execute().Should().Pass(); + buildCommand.Execute().Should().Pass(); foreach (var targetFramework in targetFrameworks.Split(';')) { @@ -264,7 +260,7 @@ public void SuppressImplicitGitSourceLink_ExplicitPackage(bool multitarget) string targetFrameworks = ToolsetInfo.CurrentTargetFramework + (multitarget ? ";netstandard2.0" : ""); var testAsset = _testAssetsManager - .CopyTestAsset("SourceLinkTestApp") + .CopyTestAsset("SourceLinkTestApp", identifier: multitarget.ToString()) .WithSource(); if (multitarget) @@ -274,8 +270,6 @@ public void SuppressImplicitGitSourceLink_ExplicitPackage(bool multitarget) testAsset = WithItems(testAsset, ("PackageReference", new[] { new XAttribute("Include", "Microsoft.SourceLink.GitHub"), new XAttribute("Version", "1.0.0") })); - Assert.NotNull(targetFrameworks); - CreateGitFiles(testAsset.Path, "https://github.com/org/repo"); var buildCommand = new BuildCommand(testAsset) @@ -283,7 +277,7 @@ public void SuppressImplicitGitSourceLink_ExplicitPackage(bool multitarget) WorkingDirectory = testAsset.Path }; - var result = buildCommand.Execute().Should().Pass(); + buildCommand.Execute().Should().Pass(); foreach (var targetFramework in targetFrameworks.Split(';')) { @@ -302,7 +296,7 @@ public void Cpp() .CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib") .WithSource(); - testAsset = WithProperties(testAsset, ("EnableManagedpackageReferenceSupport", "true")); + testAsset = WithProperties(testAsset, ("EnableManagedPackageReferenceSupport", "true")); CreateGitFiles(testAsset.Path, "https://github.com/org/repo");