Nuget cannot restore Microsoft.Net.Compilers.1.0.0

LP13 picture LP13 · Feb 12, 2018 · Viewed 13.4k times · Source

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?

Answer

LP13 picture LP13 · Feb 12, 2018

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!!