Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moving the <None> items into targets to force correct ordering #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SimaTian
Copy link

@SimaTian SimaTian commented Jan 9, 2025

Follow up for Order of projects in solution file affects build results
Original reproduction here

The blog here mentions how to include generated file. This case was a bit specific and required slightly different setup because the wasn't enough to enforce correct ordering.

Basically the issue is that if the item isn't within a target, it is resolved during Evaluation phase - e.g. as soon as the project file is loaded into the MSBuild, but ProjectReference that is setting up the project dependency ordering is a Target so this happens:

  • MSBuild first loads a project (including evaluation) as specified by the .sln file
    • this is affected by the original project ordering change that resulted in the build failing
  • Then it evaluates the targets related to project references, see here.
    • specifically ResolveProjectReferences target
  • this builds the referenced projects - however the item is already processed by this point, resulting in an empty copy

By moving the item into a custom target and forcing order by setting AfterTargets, the files are moved correctly. Tests pass.

Interestingly enough, since the files are not processed further, another(albeit rather weird) way to have a working build without using the target is to run the build twice. This both made my reproduction attempts somewhat unwieldy and hinted me to a correct solution.

Kudos for Jan Krivanek for letting me know about the MSBuildism blog and to Jenny Bai for her work on reproducing the issue and collecting the logs.

@chtenb
Copy link
Member

chtenb commented Jan 15, 2025

@SimaTian Thanks for looking into this. If I understand your explanation correctly, the issue is that by default, plain <ItemGroup> entries are resolved before things like build and project references are resolved. I can see why that is a problem here. I understand how putting it in a target may resolve this issue, as you demonstrated in this PR.

I'm currently working on a linux port of this library, and I tried to apply your idea to this branch as well (the linux branch). For various reasons, I needed to make these None definition more complex here, so it looks like this:

<Target Name="RunAfterBuildIsDone" AfterTargets="ResolveProjectReferences">
  <ItemGroup>
    <!-- Include all files in the Resources folder but copy them directly to the output directory -->
    <None Update="Resources/*">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <Link>%(RecursiveDir)/../%(Filename)%(Extension)</Link>
      <Pack>true</Pack>
      <PackageCopyToOutput>true</PackageCopyToOutput>
    </None>
    <!-- The graphviz subfolder is only present and relevant on linux --> 
    <None Update="Resources/graphviz/*">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <Link>%(RecursiveDir)/../../graphviz/%(Filename)%(Extension)</Link>
      <Pack>true</Pack>
      <PackageCopyToOutput>true</PackageCopyToOutput>
    </None>
  </ItemGroup>
</Target>

However, after putting these in a target per your suggestion, these commands start misbehaving. Among other things, they now emit the following warnings:

/usr/lib/dotnet/sdk/8.0.111/Sdks/NuGet.Build.Tasks.Pack/build/NuGet.Build.Tasks.Pack.targets(221,5): warning NU5119: File '/home/chiel/prj/Graphviz.NetWrapper/Rubjerg.Graphviz/.editorconfig' was not added to the package. Files and folders starting with '.' or ending with '.nupkg' are excluded by default. To include this file, use -NoDefaultExcludes from the commandline [/home/chiel/prj/Graphviz.NetWrapper/Rubjerg.Graphviz/Rubjerg.Graphviz.csproj]
/usr/lib/dotnet/sdk/8.0.111/Sdks/NuGet.Build.Tasks.Pack/build/NuGet.Build.Tasks.Pack.targets(221,5): warning NU5119: File '/home/chiel/prj/Graphviz.NetWrapper/Rubjerg.Graphviz/.editorconfig' was not added to the package. Files and folders starting with '.' or ending with '.nupkg' are excluded by default. To include this file, use -NoDefaultExcludes from the commandline [/home/chiel/prj/Graphviz.NetWrapper/Rubjerg.Graphviz/Rubjerg.Graphviz.csproj]
/usr/lib/dotnet/sdk/8.0.111/Sdks/NuGet.Build.Tasks.Pack/build/NuGet.Build.Tasks.Pack.targets(221,5): warning NU5119: File '/home/chiel/prj/Graphviz.NetWrapper/Rubjerg.Graphviz/.gitignore' was not added to the package. Files and folders starting with '.' or ending with '.nupkg' are excluded by default. To include this file, use -NoDefaultExcludes from the
commandline [/home/chiel/prj/Graphviz.NetWrapper/Rubjerg.Graphviz/Rubjerg.Graphviz.csproj]
/usr/lib/dotnet/sdk/8.0.111/Sdks/NuGet.Build.Tasks.Pack/build/NuGet.Build.Tasks.Pack.targets(221,5): warning NU5119: File '/home/chiel/prj/Graphviz.NetWrapper/Rubjerg.Graphviz/.gitignore' was not added to the package. Files and folders starting with '.' or ending with '.nupkg' are excluded by default. To include this file, use -NoDefaultExcludes from the
commandline [/home/chiel/prj/Graphviz.NetWrapper/Rubjerg.Graphviz/Rubjerg.Graphviz.csproj]

I'm extremely confused as to how putting the items in a target results in these warnings. I looks like the <None> definitions no longer just apply to files in the Resources subfolder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants