phmonte / Buildalyzer

A utility to perform design-time builds of .NET projects without having to think too hard about it.
MIT License
589 stars 92 forks source link

How to do a (fast) design-time build? #217

Closed safesparrow closed 4 months ago

safesparrow commented 1 year ago

Hi, Thanks for this project, it makes things easier for me!

How does one do a design-time build using Buildalyzer? It seems to me that the following does either a full build, or it does a design-time build in a very inefficient way:

let m = AnalyzerManager()
let p = manager.GetProject(projFile)
let r = analyzer.Build()

Running this for the FSharp.Compiler.Service.fsproj project in the F# compiler takes 57s.

I couldn't find any options that would allow me to say what kind of build I want to run. What am I missing?

EDIT: I think I found it:

let m = AnalyzerManager()
let p = m.GetProject(projPath)
let be = p.EnvironmentFactory.GetBuildEnvironment()
let be2 = BuildEnvironment(true, false, [||], be.MsBuildExePath, be.DotnetExePath, be.Arguments)
let r = p.Build(be2)
safesparrow commented 1 year ago

Actually my 'fix' doesn't work - it gives empty results (eg. no source files).

So still an open question for me as to how to do quick design-time builds.

daveaglick commented 1 year ago

Buildalyzer works by calling MSBuild from the command line (the same way you can) while passing it a special logger that sends messages over a pipe back to Buildalyzer for analysis. Performing a "real" design-time build is a challenge - Visual Studio and other IDEs set a lot of different MSBuild properties to make that work correctly, and the correct combinations aren't well documented (if at all).

For C# I was able to reverse-engineer the set of properties to use, which Buildalyzer does by setting environment variables before calling MSBuild. However, in F# the code was entirely donated by the community - I know very little about how the F# compiler works and interacts with MSBuild outside of the unit tests in Buildalyzer I use to verify it's at least returning results. My ability to debug Buildalyzer working against an F# project is very limited. If you happen to figure out the correct set of MSBuild properties to configure for a fast design-time build of an F# project, let me know and we can look at updating the MSBuild call with those. The first step would probably be seeing if you can get it to work from a raw MSBuild command-line (I.e. without Buildalyzer at all) by using the MSBuild CLI switch for setting properties - play around with that until you figure out the right super-set of properties, then we can integrate them.

alfonsogarciacaro commented 1 year ago

Thanks a lot for the explanation @daveaglick.

@safesparrow I am using Buildalyzer with Fable, I take the compiler options directly by splitting the lines of result.Command and it works (source files are lines not starting with a hyphen).

safesparrow commented 1 year ago

Thanks a lot for the explanation @daveaglick.

@safesparrow I am using Buildalyzer with Fable, I take the compiler options directly by splitting the lines of result.Command and it works (source files are lines not starting with a hyphen).

That should work - the main issue with it and the topic of this issue is that it actually performs a full build, which is not something you'd normally need just to get the compiler commandline, and it can be slow due to that.

It's also not trivial to construct FSharpOptions objects from it, especially in a project graph, rather than just a standalone project. But I'm sure what you've done in eg. https://github.com/fable-compiler/Fable/blob/b502e14cff8b05c92253112019b22474146b5414/src/Fable.Cli/ProjectCracker.fs handles that.

daveaglick commented 1 year ago

That should work - the main issue with it and the topic of this issue is that it actually performs a full build, which is not something you'd normally need just to get the compiler commandline, and it can be slow due to that.

Yeah, I'd be happy to merge in a PR that has the right set of MSBuild properties to trigger a design-time F# build, but I'm afraid without a contribution it's something that's out of my league and time availability to figure out on my own.

safesparrow commented 1 year ago

Yeah, I'd be happy to merge in a PR that has the right set of MSBuild properties to trigger a design-time F# build, but I'm afraid without a contribution it's something that's out of my league and time availability to figure out on my own.

Thanks - I was just explaining why using the output of Buildalyzer for generating compiler options, while works, is not exactly what I was hoping for 👍

phmonte commented 4 months ago

Closing the issue due to lack of activity, if you have any updates about F#, please open a new one.