25

I have Visual Studio Solution with more than 100 projects. Five of them have Fody's nuget packages installed (version 6.0.3). When I try to build the Solution I get the following error:

Fody.6.3.0\build\Fody.targets(38,12): error MSB4086: A numeric comparison was attempted on "$(MsBuildMajorVersion)" that evaluates to "" instead of a number, in condition "($(MsBuildMajorVersion) < 16)"

After this failed build I try to build repeatedly and after several attempts (sometimes one, sometimes five) building finishes with success. I was trying to reinstall all nuget packages, clear nuget caches, reinstall Visual Studio, set MSBUILDDISABLENODEREUSE variable and nothing helped.

Visual Studio 2019 is in version 16.8.4 and all projects have .NET Framework in version 4.8.

UPDATE:

The problem exists on my machine only. On colleagues' workstations, everything works fine. I found the tip that after an unsuccessful build Visual Studio should be restarted and the build process is successful. This tip is a sufficient workaround for me.

rgb
  • 1,750
  • 1
  • 20
  • 35
  • 1
    most likely some bug in VS but you should post project file – T.S. Jan 13 '21 at 16:46
  • 1
    You could try to share one of the error project with us with github link with us to let us troubleshoot the issue more quickly if the answer does not work. – Mr Qian Jan 14 '21 at 03:27
  • raise an issue here and ensure you follow all the steps in the template https://github.com/Fody/Fody/issues/new?assignees=&labels=&template=bug_report.md – Simon Feb 04 '21 at 00:28
  • @Simon I’ve already found the solution. – rgb Feb 09 '21 at 21:10
  • 1
    I upvoted your question for the solution that you posted underneath it. Contrary to all the solutions that were posted here that is the trick that solved it for me. – Jan and RESTless Apr 26 '21 at 17:29
  • Restarting VS worked for me too. You should really add that as a separate answer so that it can get voted separately and therefore inform people that it is helpful. – StayOnTarget Aug 08 '23 at 12:28

6 Answers6

23

The solution to the problem was to migrate from packages.config to PackageReference. You can do this in Visual Studio 2019: within Solution Explorer expand your project, than click right mouse button on the References and choose Migrate packages.config to PackageReference....

rgb
  • 1,750
  • 1
  • 20
  • 35
  • Hi I did the same thing now I get different Could not locate packages.config file error Ensure that this project has Microsoft.Bcl.Build installed and packages.config is located next to the project file – user123456 May 25 '21 at 07:11
  • Fixed it for me, this was on an older project and was trying to use a newer Visual Studio. Can also right click on the packages.config itself – gen Dec 04 '21 at 00:12
5

Try to install the latest Costura.Fody nuget package additionally on those five projects and also try to update Fody nuget package to the latest 6.3.0 version.

And then rebuild your project to check whether the issue happens.

Mr Qian
  • 21,064
  • 1
  • 31
  • 41
1

I was able to fix this problem by adding to .csproj code to restore nugets (just copied from project created in VS 2017). Converting packages.config to PackageReference also worked for me but causes some other problems when build by msbuild.

Need to add following lines at the end of first PropertyGroup

  <SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
  <RestorePackages>true</RestorePackages>
</PropertyGroup>

And following line EnsureNuGetPackageBuildImports target:

  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

See attached screenshot for more details:

enter image description here

1

May sound silly, but for me all I needed to do, is simply upgrading Fody nuget to another version and switching back to the original version, and my code compiled successfully again. Might not be your case, but worth to try this 2 minutes solution.

Tiger Galo
  • 289
  • 4
  • 15
1

I tried several of the solutions suggested here, but what finally worked was simply cleaning everything out, then performing the "Restore NuGet packages" from the solution context menu. Why this manual step worked when the auto-restore did restore the packages but caused the error, I'm not sure.

IanJ
  • 467
  • 7
  • 15
  • I didn't even clean anything out. Just went to the context menu and picked 'Restore NuGet packages'. VS reported 'All packages are already installed and there is nothing to restore'. After that I could build without error. – Joachim J Oct 26 '22 at 12:33
0

I was able to fix this problem by cleaning up my whole resolution and trying to run a rebuild, then succeeded to rebuild the project.

Maybe you should try.

0xAA55
  • 375
  • 2
  • 12