Skip to content

Commit 1e85144

Browse files
authored
Handle PDB location when project build externally (#1354)
Handle PDB location when project build externally
1 parent e50e24d commit 1e85144

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/coverlet.core/Abstractions/ISourceRootTranslator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Coverlet.Core.Abstractions
88
{
99
internal interface ISourceRootTranslator
1010
{
11+
bool AddMappingInCache(string originalFileName, string targetFileName);
1112
string ResolveFilePath(string originalFileName);
1213
string ResolveDeterministicPath(string originalFileName);
1314
IReadOnlyList<SourceRootMapping> ResolvePathRoot(string pathRoot);

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,32 @@ public bool HasPdb(string module, out bool embedded)
9090
if (entry.Type == DebugDirectoryEntryType.CodeView)
9191
{
9292
CodeViewDebugDirectoryData codeViewData = peReader.ReadCodeViewDebugDirectoryData(entry);
93-
if (_sourceRootTranslator.ResolveFilePath(codeViewData.Path) == $"{Path.GetFileNameWithoutExtension(module)}.pdb")
93+
string modulePdbFileName = $"{Path.GetFileNameWithoutExtension(module)}.pdb";
94+
if (_sourceRootTranslator.ResolveFilePath(codeViewData.Path) == modulePdbFileName)
9495
{
9596
// PDB is embedded
9697
embedded = true;
9798
return true;
9899
}
99100

100-
return _fileSystem.Exists(_sourceRootTranslator.ResolveFilePath(codeViewData.Path));
101+
if (_fileSystem.Exists(_sourceRootTranslator.ResolveFilePath(codeViewData.Path)))
102+
{
103+
// local PDB is located within original build location
104+
embedded = false;
105+
return true;
106+
}
107+
108+
string localPdbFileName = Path.Combine(Path.GetDirectoryName(module), modulePdbFileName);
109+
if (_fileSystem.Exists(localPdbFileName))
110+
{
111+
// local PDB is located within same folder as module
112+
embedded = false;
113+
114+
// mapping need to be registered in _sourceRootTranslator to use that discovery
115+
_sourceRootTranslator.AddMappingInCache(codeViewData.Path, localPdbFileName);
116+
117+
return true;
118+
}
101119
}
102120
}
103121

src/coverlet.core/Helpers/SourceRootTranslator.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ private Dictionary<string, List<SourceRootMapping>> LoadSourceRootMapping(string
108108
return mapping;
109109
}
110110

111+
public bool AddMappingInCache(string originalFileName, string targetFileName)
112+
{
113+
if (_resolutionCacheFiles != null && _resolutionCacheFiles.ContainsKey(originalFileName))
114+
{
115+
return false;
116+
}
117+
118+
(_resolutionCacheFiles ??= new Dictionary<string, string>()).Add(originalFileName, targetFileName);
119+
return true;
120+
}
121+
111122
public IReadOnlyList<SourceRootMapping> ResolvePathRoot(string pathRoot)
112123
{
113124
return _sourceRootMapping.TryGetValue(pathRoot, out List<SourceRootMapping> sourceRootMapping) ? sourceRootMapping.AsReadOnly() : null;

test/coverlet.core.tests/Helpers/InstrumentationHelperTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,27 @@ public void EmbeddedPortablePDPHasLocalSource_AllDocumentsExist_ReturnsTrue()
5454
}
5555

5656
[Fact]
57-
public void TestHasPdb()
57+
public void TestHasPdbOfLocalAssembly()
5858
{
5959
Assert.True(_instrumentationHelper.HasPdb(typeof(InstrumentationHelperTests).Assembly.Location, out bool embeddedPdb));
6060
Assert.False(embeddedPdb);
6161
}
6262

63+
[Fact]
64+
public void TestHasPdbOfExternalAssembly()
65+
{
66+
string testAssemblyLocation = GetType().Assembly.Location;
67+
68+
string externalAssemblyFileName = Path.Combine(
69+
Path.GetDirectoryName(testAssemblyLocation),
70+
"TestAssets",
71+
"75d9f96508d74def860a568f426ea4a4.dll"
72+
);
73+
74+
Assert.True(_instrumentationHelper.HasPdb(externalAssemblyFileName, out bool embeddedPdb));
75+
Assert.False(embeddedPdb);
76+
}
77+
6378
[Fact]
6479
public void TestBackupOriginalModule()
6580
{

0 commit comments

Comments
 (0)