Skip to content

Commit 90a8b36

Browse files
Include PDBs in CopyLocal outputs if they exist
Fixes NuGet/Home#2009.
1 parent 01fad15 commit 90a8b36

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/Microsoft.NuGet.Build.Tasks.Tests/AssertHelpers.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,28 @@ public static void AssertCountOf(int expectedCount, IEnumerable<ITaskItem> items
2626
/// </summary>
2727
public static void PathEndsWith(string expectedPath, string actualPath)
2828
{
29-
if (expectedPath != actualPath)
29+
// We could implement this with a simple Assert.True(...EndsWithPath),
30+
// but that results in less user-friendly output from the test than a smarter call to
31+
// Assert.EndsWith
32+
if (!actualPath.EndsWithPath(expectedPath))
3033
{
31-
// This means it must be a subfolder, so we have to prefix with a path component to ensure
32-
// we are matching full components
3334
Assert.EndsWith("\\" + expectedPath, actualPath);
35+
36+
// If we get out of sync with this function or EndsWithPath, that Assert might not
37+
// fail. In that case, fail the test.
38+
throw new Exception("The Assert.EndsWith in the previous line should have failed.");
3439
}
3540
}
3641

42+
/// <summary>
43+
/// Returns that the expected path ends with our actual path. "Ends with" in this case is defined by path
44+
/// components, so "Directory\File" doesn't end with "ile", to prevent any bugs where path components get mangled.
45+
/// </summary>
46+
public static bool EndsWithPath(this string path, string suffix)
47+
{
48+
return path == suffix || path.EndsWith("\\" + suffix);
49+
}
50+
3751
public static void AssertNoTargetPaths(IEnumerable<ITaskItem> items)
3852
{
3953
foreach (var item in items)

src/Microsoft.NuGet.Build.Tasks.Tests/ReferenceResolutionTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ public static void IncludingFrameworkReferencesActuallyIncludesFrameworkReferenc
218218
Assert.Contains("FluentAssertions", packageNames);
219219
}
220220

221+
[Fact]
222+
public static void CopyLocalContentsIncludePdbsIfAvailable()
223+
{
224+
var result = NuGetTestHelpers.ResolvePackagesWithJsonFileContents(
225+
Default.GetString(Json.Json.FluentAssertionsAndWin10),
226+
targetMoniker: "UAP,Version=v10.0",
227+
runtimeIdentifier: "win10-x86",
228+
includeFrameworkReferences: true);
229+
230+
Assert.Single(result.CopyLocalItems, r => r.ItemSpec.EndsWithPath("FluentAssertions.dll"));
231+
Assert.Single(result.CopyLocalItems, r => r.ItemSpec.EndsWithPath("FluentAssertions.pdb"));
232+
Assert.Single(result.CopyLocalItems, r => r.ItemSpec.EndsWithPath("FluentAssertions.Core.dll"));
233+
Assert.Single(result.CopyLocalItems, r => r.ItemSpec.EndsWithPath("FluentAssertions.Core.pdb"));
234+
}
235+
221236
[Fact]
222237
public static void FrameworkReferencesAreNotProvidedIfAlreadyProvidedByAnotherPackage()
223238
{

src/Microsoft.NuGet.Build.Tasks/ResolveNuGetPackageAssets.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ private void GetReferences(JObject lockFile)
262262

263263
foreach (var package in GetPackagesFromTarget(lockFile, target))
264264
{
265-
foreach (var referenceItem in CreateItems(package, NuGetAssetTypeCompile))
265+
foreach (var referenceItem in CreateItems(package, NuGetAssetTypeCompile, includePdbs: false))
266266
{
267267
_references.Add(referenceItem);
268268

@@ -746,7 +746,7 @@ private string GetTargetMonikerWithOptionalRuntimeIdentifier(ITaskItem preferred
746746
return needsRuntimeIdentifier ? preferredTargetMoniker.ItemSpec + "/" + RuntimeIdentifier : preferredTargetMoniker.ItemSpec;
747747
}
748748

749-
private IEnumerable<ITaskItem> CreateItems(NuGetPackageObject package, string key)
749+
private IEnumerable<ITaskItem> CreateItems(NuGetPackageObject package, string key, bool includePdbs = true)
750750
{
751751
var values = package.TargetObject[key] as JObject;
752752
var items = new List<ITaskItem>();
@@ -777,6 +777,22 @@ private IEnumerable<ITaskItem> CreateItems(NuGetPackageObject package, string ke
777777
item.SetMetadata(NuGetIsFrameworkReference, "false");
778778

779779
items.Add(item);
780+
781+
// If there's a PDB alongside the implementation, we should copy that as well
782+
if (includePdbs)
783+
{
784+
var pdbFileName = Path.ChangeExtension(item.ItemSpec, ".pdb");
785+
786+
if (_fileExists(pdbFileName))
787+
{
788+
var pdbItem = new TaskItem(pdbFileName);
789+
790+
// CopyMetadataTo also includes an OriginalItemSpec that will point to our original item, as we want
791+
item.CopyMetadataTo(pdbItem);
792+
793+
items.Add(pdbItem);
794+
}
795+
}
780796
}
781797

782798
return items;

0 commit comments

Comments
 (0)