MsBuild item evaluation surprise

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /customers/c/b/1/cactcxjko/webroots/www/blog/wp-content/plugins/wp-syntax/wp-syntax.php on line 380 Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /customers/c/b/1/cactcxjko/webroots/www/blog/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Solved a problem with a build script last week. I had somehow caused the build to under some conditions take a very long time to start with heavy disk IO. After reviewing some recent changes I found the cause.

I had a ItemGroup that looked something like the following.

<ItemGroup Condition="'$(PathProperty)'!=''">
    <SomeItem Include="$(PathProperty)/**/*.dll"/>
</ItemGroup>

Apparently MsBuild evaluates all ItemGroups even if the condition of the group or item evaluates to false. The result is just discarded if the condition evaluates to false. So the item above included all dll files on the entire hard drive when PathProperty was not set. By trial and error I found out that the condition of Choose elements are evaluated before the items they contain.

So to speed things up I changed the above to the following.

<Choose>
    <When Condition="'$(PathProperty)'!=''">
        <ItemGroup>
            <SomeItem Include="$(PathProperty)/**/*.dll"/>
        </ItemGroup>
    </When>
</Choose>

Is this behaviour obvious to everyone else? Couldn’t find anything about it while reading the conditions documentation nor the documentation on items and properties on MSDN.

113 thoughts on “MsBuild item evaluation surprise”

  1. For a Flamingo chapter lesson plan, tracking vocabulary density and reading time helps pacing. I use real-time character counting for worksheets, then check ??????? alongside line and paragraph counts. Since everything stays in the browser, student drafts are never saved or sent.

  2. Captain Dave clearly knows how to put people on fish, which takes patience and timing. The same feel applies to aiming drills, where reading the moment matters more than rushing. I have been using okiaimx to practice peek timing and crosshair control, and it genuinely helps sharpen reactions.

  3. Interesting MSBuild gotcha — the fact that ItemGroups are evaluated even when their condition is false is exactly the kind of subtle behavior that bites you in a long build. I ran into something similar when setting up an AI stem separation pipeline: the model (Demucs v4) is open-source, but getting PyTorch and GPU dependencies right locally was its own rabbit hole. Ended up using a hosted version instead — Demucs Online runs the same model on cloud GPUs, upload a track and get vocals, drums, bass and other stems in about a minute, no Python or local GPU required. Handy if you ever need clean stems without wrestling with the toolchain.

Leave a Reply

Your email address will not be published. Required fields are marked *