Search code examples
c#msbuild

Build a single project respecting solution-level configuration mappings


I have a solution (My.sln) with two projects

  • App.csproj - application
  • Lib.csproj - class library

The App project references the Lib project via the <ProjectReference/>

I have configuration mappings in place for the solution in the following way:

Project Solution config = Debug Solution config = DebugJS Solution config = Release
App Debug DebugJS Release
Lib Debug Debug Release

So, the point of intereset here is that the for DebugJS configuration the App project should be built in DebugJS config and the Lib project should remain in the Debug config.

This works fine when I build from IDE or with dotnet build my.sln -c DebugJS.

But in the real solution I have a lot of other projects and I want to build only the project I need right now (also, some of the projects may be built only on MacOS, others only on Windows and so on, so it is not very convenient to build the whole sln every time).

I can build the single project with dotnet build ./App/App.csproj -c DebugJS. But in this case the sln file and its configuration mappings will be completely ignored and both the App.csproj and Lib.csproj will be built with the DebugJS configuration.

So, is there a way (using the dotnet build or the msbuild tasks) to build a single project from the solution with a respect to the solution-level configuration bindings?


Solution

  • At the command line there is no standard option or property or other mechanism for selecting a subset of the projects that are in the given solution file.

    To build different sets of projects used to require creating different solution files. The situation improved with Visual Studio 2019 which introduced solution filter files.

    In the VS IDE in the solution explorer window, projects can be "loaded" or "unloaded". On a "Build -> Build Solution" action in the IDE, only "loaded" projects will be built.

    A solution filter file (.slnf) is tied to a specific solution file and specifies a set of projects that should be loaded. When Visual Studio opens a .slnf file, the 'filter' is applied to the associated solution file and only the "loaded" projects are displayed.

    MSBuild v16.7 added support for .slnf files.

    If you are building with dotnet, you can check the version of MSBuild that is embedded in the dotnet tool with:

    dotnet msbuild --version
    

    With net8.0 you will probably have v17+ of MSBuild.

    There can be multiple solution filter files for one solution. The solution filter file stores the path of the associated solution. The file names don't need to match or follow a particular pattern but it's a good idea to maintain the same relative path relationship between a .slnf and its .sln.

    A solution filter file will be needed per subset of interest.