microsoft / MSBuildSdks

MSBuild project SDKs
MIT License
460 stars 82 forks source link

How to express project dependencies in Traversal SDK? #504

Open hickford opened 1 year ago

hickford commented 1 year ago

In a Visual Studio solution, I can explicitly express dependencies between projects. This is useful when projects implicitly depend on the output of other projects, so that Visual Studio can calculate a compatible build order.

How can I describe project dependencies in a Microsoft.Build.Traversal SDK proj?

jeffkl commented 1 year ago

Unfortunately, build ordering in a solution file is not a very good best practice. Since the ordering is only respected when building the solution file, this means that when you build projects instead, the build ordering is not guaranteed. The better solution is to declare dependencies directly in each project. This will have your build order respected in Visual Studio and the command-line, whether you're building a solution or a project.

Are you able to add <ProjectReference /> items in the projects to explicitly declare a dependency? You can set metadata like ReferenceOutputAssembly to not actually pass the reference to the compiler and just have it declared as a dependency:

<ItemGroup>
  <!-- Declare a build dependency on a project but don't actually depend on the assembly at compile time -->
  <ProjectReference Include="..\SomeDependency\SomeDependency.csproj" ReferenceOutputAssembly="false" />
</ItemGroup>

If you run into target framework incompatibility, you can also set SkipGetTargetFrameworkProperties=false to bypass the check and just have the dependency declared for build ordering.