2

As far as I understand, Roslyn have introduced the concept of Workspaces. One implementation of Workspaces is the MsBuildWorkspace.

My question is, can I from within a custom build task access a Roslyn Workspace representing the project being built?

I suspect that this is the purpose of MsBuildWorkspace. If so, can I access this workspace from the Execute method in my custom task (derived from Microsoft.Build.Utilities.Task)?

In case you are wondering why, I need to traverse other aspects of the project being built. It is not enough for me to have access to the specific input file of the task to generate the output.

Jack Wester
  • 5,170
  • 3
  • 28
  • 47
  • Have you found a solution? This is insane, I don't even want to build, I want to use Roslyn for code analysis in a build task, but I get the error opening the solution. – Stefano d'Antonio Mar 14 '16 at 15:14

1 Answers1

2

I suspect that this is the purpose of MsBuildWorkspace

No.
The point of MsBuildWorkspace is to parse an MSBuild project or solution into a Roslyn workspace.

MSBuild itself (which is what actually runs your MSBuild task) does not use Roslyn at all (it just invokes the C# compilation task, which is implemented using Roslyn), so there is no existing MsBuildWorkspace that you could fetch.

You could create your own MsBuildWorkspace from the project file.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • And the technical reason it doesn't work: MSBuildWorkspace behind the scenes starts a build to figure out what's in the project. This doesn't work well when a build is already happening. – Jason Malinowski Jan 14 '15 at 18:08
  • @JasonMalinowski: You mean the reason that there is no way to get the original `MsBuildWorkspace` when the build is initiated by Roslyn? – SLaks Jan 14 '15 at 18:10
  • Simpler reason: Not all builds are initiated by Roslyn. – SLaks Jan 14 '15 at 18:11
  • It's not just that you can't access some mythical original MsBuildWorkspace, it's that trying to construct *any* MsBuildWorkspace will trigger a deadlock. – Kevin Pilch Jan 14 '15 at 18:12
  • @KevinPilch-Bisson: I did not know that. Why? Does MSBuild use non-reentrancy-safe global state (eek)? – SLaks Jan 14 '15 at 18:19
  • Why wouldn't it? You can't have two builds running against the same project at once. Even if you used a thread-safe object, the entire concept is still broken: what if you have two instances of a build task writing to the same file at once? The only way to "fix" this is to reimagine a fundamentally different build system. – Jason Malinowski Jan 14 '15 at 22:35
  • Added a related question: http://stackoverflow.com/questions/31084013/roslyn-how-to-load-an-existing-project-outside-of-visual-studio – thekip Jun 27 '15 at 00:53