1

Building a project file with the Microsoft.Build.BuildEngine library seems to be ignoring the BeforeTargets attribute.

My project file has the following target:

<Target Name="MyTestTarget" BeforeTargets="Build">
  <Message Importance="High" Text="Running MyTestTarget" />
</Target>

and when I compile it with the following code (F#, but does the same in C#):

open System
open Microsoft.Build.BuildEngine
open Microsoft.Build.Framework

[<EntryPoint; STAThread>]
let main argv =
    let engine = new Engine()
    engine.RegisterLogger(new ConsoleLogger())
    let project = new Project(engine)
    project.Load("MyProjectFile.fsproj")
    let succeeded = engine.BuildProject(project, "Build")
    0

the target "MyTestTarget" is not run. Note that when I use MSBuild.exe 4.0 instead, "MyTestTarget" is correctly run.

I know that BeforeTargets was added in MSBuild 4.0, and would throw an exception if used with an older version of Microsoft.Build. But I haven't found anything about BeforeTargets being ignored by MSBuild 4.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Tarmil
  • 11,177
  • 30
  • 35

1 Answers1

0

You're using deprecated types, if you use the new types then it works:

let projects = new ProjectCollection()
projects.RegisterLogger(new ConsoleLogger())
let project = projects.LoadProject("MyProjectFile.fsproj")
let succeeded = project.Build("Build")

If you didn't see any warnings about deprecated types, then perhaps you are referencing older versions of the MSBuild assemblies.

Leaf Garland
  • 3,647
  • 18
  • 18
  • I do see the warnings, and I know those are deprecated. However my code needs to run on mono, and the newer APIs such as `ProjectCollection` are not implemented there yet. Moreover, I don't have any warning or error about `BeforeTargets` being an unknown attribute, which is what happens (based on my googling) when using older assemblies. – Tarmil Apr 23 '14 at 10:14
  • It turns out that the mono implementation of `BuildEngine` doesn't have this bug. So I am now implementing two code paths, one for mono using `BuildEngine` and one for .NET using `Execution`. – Tarmil Apr 24 '14 at 09:05