TExcelTAD
Home › .NET Builds: MSBuild & NuGet › Querying: -getProperty, -getItem & -pp

Querying: -getProperty, -getItem & -pp

"What is OutputPath in this project?" used to mean adding a <Message> target, building, and searching the log. Recent MSBuild versions answer such questions directly from the command line. Three switches cover most needs: -getProperty, -getItem and -getTargetResult, plus -pp to see the whole project after every import.

-getProperty: evaluation results

bash · try it yourself
mkdir q && cd q
cat > q.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
echo 'System.Console.WriteLine();' > Program.cs
dotnet msbuild -getProperty:TargetPath
dotnet msbuild -getProperty:AssemblyName -getProperty:LangVersion -getProperty:IntermediateOutputPath -getProperty:UseAppHost
dotnet msbuild -getProperty:TargetPath -p:Configuration=Release
/home/learner/q/bin/Debug/net10.0/q.dll
{
  "Properties": {
    "AssemblyName": "q",
    "LangVersion": "14.0",
    "IntermediateOutputPath": "obj\\Debug/net10.0/",
    "UseAppHost": "true"
  }
}
/home/learner/q/bin/Release/net10.0/q.dll
  • With one property the value is printed bare, ready for a shell variable: dll=$(dotnet msbuild -getProperty:TargetPath). With several, the output is JSON.
  • Global properties (-p:, -c on dotnet build) are applied, so you can ask "what would Release use?".
  • No target ran and nothing was built: -getProperty alone stops after evaluation. It's fast and safe to run in scripts.

dotnet build accepts the same switches; then the values are read after the build ran.

-getItem: lists with metadata

Items come back as JSON with all their metadata, which is a lot. grep (or jq, where installed) narrows it down:

bash · try it yourself
mkdir q && cd q
cat > q.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
    <PackageReference Include="Humanizer.Core" Version="2.14.1" PrivateAssets="all" />
  </ItemGroup>
</Project>
XML
dotnet msbuild -getItem:PackageReference | grep -E '"(Identity|Version|PrivateAssets)"'
echo "--- after running the ResolvePackageAssets target"
dotnet msbuild -restore -t:ResolvePackageAssets -getItem:RuntimeCopyLocalItems | grep '"Identity"' | sed 's|/home/learner/.nuget/packages/|~/.nuget/packages/|'
        "Identity": "Newtonsoft.Json",
        "Version": "13.0.4",
        "Identity": "Humanizer.Core",
        "Version": "2.14.1",
        "PrivateAssets": "all",
--- after running the ResolvePackageAssets target
        "Identity": "~/.nuget/packages/humanizer.core/2.14.1/lib/net6.0/Humanizer.dll",
        "Identity": "~/.nuget/packages/newtonsoft.json/13.0.4/lib/net6.0/Newtonsoft.Json.dll",

The first query is pure evaluation. The second combines -t: with -getItem: MSBuild ran the target (and restore first, because of -restore) and then reported the items as they were after execution. RuntimeCopyLocalItems is an item list the SDK computes from project.assets.json: the DLLs that will be copied to bin/. This is the quickest way to answer "which file of this package actually ships?".

-getTargetResult: what a target returned

Targets can return items (Returns or Outputs). -getTargetResult prints them along with whether the target succeeded:

bash · try it yourself
mkdir q && cd q
cat > q.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
echo 'public class Q {}' > Q.cs
dotnet build -nologo -getTargetResult:Build | grep -E '"(Result|Identity|TargetFrameworkVersion|ReferenceAssembly)"'
      "Result": "Success",
          "Identity": "/home/learner/q/bin/Debug/net10.0/q.dll",
          "TargetFrameworkVersion": "10.0",
          "ReferenceAssembly": "/home/learner/q/obj/Debug/net10.0/ref/q.dll",

The Build target returns the main output assembly, with metadata such as the path of the reference assembly (obj/…/ref/q.dll, a metadata-only copy that other projects compile against, so that changing a method body doesn't force dependants to recompile). Add -getResultOutputFile:result.json to write the JSON to a file instead of the console, which keeps CI logs readable.

-pp: the project after every import

The -pp (preprocess) switch writes the project as MSBuild sees it after inlining every import, with comments marking where each file starts. It's the ultimate answer to "where does this value come from?":

bash · try it yourself
mkdir q && cd q
cat > q.csproj <<'XML'
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net10.0</TargetFramework>
  </PropertyGroup>
</Project>
XML
dotnet msbuild -pp:full.xml
wc -l < full.xml
grep -c '<Import ' full.xml
grep -B2 '<Optimize Condition' full.xml | sed 's/^ *//; s/ xmlns="[^"]*"//'
17245
409
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
<Optimize Condition=" '$(Optimize)' == '' ">false</Optimize>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>

The eight-line project expands to about seventeen thousand lines. Searching it for <Optimize finds the SDK's two defaults: false inside a property group conditioned on Debug, true inside one conditioned on Release. That's why the custom Staging configuration in the conditions lesson got no value at all. Scrolling up from there to the nearest <!-- ===… Import … --> comment names the file. Use -pp when a property comes from somewhere unexpected; use a binary log when you need to know what happened during execution.

Test yourself

What does dotnet msbuild -getProperty:TargetPath print for a project?

  1. A JSON object
  2. The bare value of TargetPath, after evaluation only
  3. The full build log
  4. Nothing unless you also pass -t:Build

You want the list of DLLs a package contributes to bin/. Which command fits best?

  1. dotnet msbuild -getItem:PackageReference
  2. dotnet msbuild -restore -t:ResolvePackageAssets -getItem:RuntimeCopyLocalItems
  3. dotnet list package
  4. dotnet msbuild -pp

When is -pp the right tool?

  1. To see the order targets ran in
  2. To find which imported file defines a property, by reading the fully inlined project
  3. To build faster
  4. To list package versions