jbevain / cecil

Cecil is a library to inspect, modify and create .NET programs and libraries.
MIT License
2.71k stars 619 forks source link

Easiest way to map document paths in portable [embedded] PDBs? #903

Open safesparrow opened 1 year ago

safesparrow commented 1 year ago

Hi,

I'm trying to use Mono.Cecil to help implement a compilation cache: https://github.com/safesparrow/MSBuild.CompilerCache/

One of the problems with such a cache is that when reusing a dll from the cache that was previously compiled in a different location, its debug symbols contain absolute paths to the source files from that original compilation and will not be compatible with any other location.

I managed to use Mono.Cecil to rewrite all document URLs in all the methods found, to make the dll/pdb compatible (and it works).

However, I can see that roundtripping the whole module with Mono.Cecil has side effects, ie. a simple var m = ModuleDefinition.ReadModule(...); m.Write() does not yield an identical result. I'm assuming that that is somewhat expected and hard to avoid. (or maybe it isn't and I'm using the API wrong?)

Therefore I'm wondering if there is a less intrusive way to modify just the document URLs in the symbols of a module, that would guarantee that everything else stays untouched. I'm sure this can be done by low-level modifications of the dll/pdb files - after all it's almost a "find & replace". Is there a high-level API to achieve this though?

I'm interested in "portable" and "embedded [portable]" PDBs.