Is there a way to get the Name of the currently running Target in MSBuild?
2 Answers
MSBuild does not provide a property (or other mechanism) that provides the name of the current executing target.
If you are thinking in terms of languages and frameworks that expose a stack trace at runtime, MSBuild has nothing equivalent to that.
If you are debugging and trying to visualize the order of target invocation, one trick is to define your own property and successively add values to it as a breadcrumb trail.
e.g.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="A" DependsOnTargets="B">
<PropertyGroup>
<Trace>$(Trace);A</Trace>
</PropertyGroup>
<Message Text="$(Trace)" />
</Target>
<Target Name="B" DependsOnTargets="C">
<PropertyGroup>
<Trace>$(Trace);B</Trace>
</PropertyGroup>
</Target>
<Target Name="C">
<PropertyGroup>
<Trace>$(Trace);C</Trace>
</PropertyGroup>
</Target>
</Project>
In the example, each PropertyGroup
in each target is hard-coding a value that is added to the $(Trace)
property.
Running this MSBuild file with verbosity set to normal will produce the output:
;C;B;A
It might be nice to have a generic snippet of code that could be pasted unchanged into each target but currently that's not possible.

- 2,654
- 1
- 10
- 14
You are probably looking for the $(MSBuildProjectFile) but "Target" is kind of vague.

- 11
- 3
-
An MSBuild `Target` groups tasks. It is a specific construct. See [MSBuild targets](https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-targets?view=vs-2022). The `$(MSBuildProjectFile)` property is not related to targets. – Jonathan Dodds Oct 31 '22 at 18:24
-
Yeah, forgot Targets were a specific thing in MSBuild. Ignore me. – Matthew Asplund Nov 03 '22 at 01:35