Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nuget cannot restore Microsoft.Net.Compilers.1.0.0

I am trying to install/restrore Microsoft.Net.Compilers.1.0.0 in VS 2017 using Nuget Package Manager. In the output it shows restore completed. however when i check the packages folder i dont see Microsoft.Net.Compilers folders. And beacuse of that i get error

Severity Code Description Project File Line Suppression State Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is ....\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props. XXXXX\Src\Api\Api.csproj 296

In csproj file there is a line at the top

<Import Project="..\..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props')" />

Background
This issue is occurring in Web API project with target framework 4.6.2. I also have NET Standard 1.4 library that i want to share with different types of .NET application. When i add reference of NET Standard Library to Web API project i got missing dependencies issues. So as per the suggestion i edited .csproj file and added

  <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

That fixed the missing dependencies issue.

Then i deleted package.config file, removed all packages from packages folder and added back all the packages (except i could not add Microsoft.Net.Compilers). The package reference is now in .csproj file

There SO post here, however in my case Microsoft.Net.Compilers does not even getting restored to packages folder. VS 2017 shows restored is complete but i don't know where its actually coping the files. (unless the folder name is different than Microsoft.Net.Compilers)

My original package.config file has this line

<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net462" developmentDependency="true" />

now in .csproj file i have

   <PackageReference Include="Microsoft.Net.Compilers">
      <Version>1.0.0</Version>
    </PackageReference>

UPDATE 1
So it looks like when packagereference is enabled nuget will install the packages at C:\Users\{username}\.nuget\packages folder

that means i need to update csproj file with correct relative path.

What would be the relative path here for packages folder?

like image 464
LP13 Avatar asked Feb 12 '18 19:02

LP13


People also ask

How do I fix NuGet recovery failed?

Quick solution for Visual Studio usersSelect the Tools > NuGet Package Manager > Package Manager Settings menu command. Set both options under Package Restore. Select OK. Build your project again.

How do I force a NuGet package to restore?

Enable package restore by choosing Tools > Options > NuGet Package Manager. Under Package Restore options, select Allow NuGet to download missing packages. In Solution Explorer, right click the solution and select Restore NuGet Packages.

Does Msbuild restore NuGet packages?

msbuild -t:Restore will restore nuget packages for projects with PackageReference nuget management format.

Does dotnet restore use NuGet?

The dotnet restore command uses NuGet to restore dependencies as well as project-specific tools that are specified in the project file.


1 Answers

Since setting the Package Management Format to PackageReference installs the packages to global nuger folder that is C:\Users\{username}\.nuget\packages i had to edit csproj file update the following lines

top of csproj

<Import Project="$(UserProfile)\.nuget\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform\1.0.7\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('$(UserProfile)\.nuget\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform\1.0.7\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
  <Import Project="$(UserProfile)\.nuget\packages\Microsoft.Net.Compilers\2.1.0\build\Microsoft.Net.Compilers.props" Condition="Exists('..\packages\Microsoft.Net.Compilers.2.1.0\build\Microsoft.Net.Compilers.props')" />

and then update the following lines at the bottom of csproj

<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(UserProfile)\.nuget\packages\Microsoft.Net.Compilers\2.1.0\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '$(UserProfile)\.nuget\packages\Microsoft.Net.Compilers\2.1.0\build\Microsoft.Net.Compilers.props'))" />
    <Error Condition="!Exists('$(UserProfile)\.nuget\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform\1.0.7\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '$(UserProfile)\.nuget\packages\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform\1.0.7\build\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
  </Target>

All of this is just to reference NET Standard 1.4 project into .NET 4.6.2. very annoying!!

like image 180
LP13 Avatar answered Sep 29 '22 09:09

LP13