'error MSB4019: The imported project "C:\Program Files\dotnet\sdk\1.0.3\Microsoft\Portable\v5.0\Microsoft.Portable.CSharp.targets" was not found

I have .net core test project. When I build it in visual studio or use the TestManager it build without a problem. But when I execute the following command on the package manager console I get an error:

dotnet test C:\projects\moneyfox\Src\MoneyFox.DataAccess.Tests\MoneyFox.DataAccess.Tests.csproj

error:

error MSB4019: The imported project "C:\Program Files\dotnet\sdk\1.0.3\Microsoft\Portable\v5.0\Microsoft.Portable.CSharp.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk

I checked the path, and there it really doesn't exist. But I'm kinda puzzled what to do with that, since it works when I build it over visual studio. Any suggestions?



Solution 1:[1]

My solution to this was to convert the project to an Sdk-type csproj.

Check out this answer for tips on how to accomplish this.

In addition to this, I had issues with the analysers so I turned them off via the following csproj properties:

<Project>
  <PropertyGroup>
    <RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
    <RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>
    <RunAnalyzers>false</RunAnalyzers>
  </PropertyGroup>
</Project>

Solution 2:[2]

I was able to hack my way around the issue by observing how Visual Studio 2017 was building the project (increase the build verbosity). I'm still uncertain what the actual cause of the issue is and will report the issue to the Xamarin team.

Step 1 - Manually reference the same targets file as Visual Studio

In my.csproj file, I had the following import statement

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />

which I changed to

<Import Project="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Portable\v5.0\Microsoft.Portable.CSharp.targets" />

to match the .targets file that Visual Studio imports.

Once I made this change, my build errors increase because now I was missing references to the standard namespaces (System, System.Net, etc) which Visual Studio magically references on our behalf. I also couldn't add the references to the assemblies via Visual Studio as it complained about duplicate references. So back to the text editor. I manually a=added the following references until my build errors went were resolved.

Step 2 - Manually reference the System* DLLs you need.

<Reference Include="System">
  <HintPath>..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\MonoAndroid\v1.0\mscorlib.dll</HintPath>
</Reference>
<Reference Include="System">
  <HintPath>..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\MonoAndroid\v1.0\System.dll</HintPath>
</Reference>    
<Reference Include="System.Linq">
  <HintPath>..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\MonoAndroid\v1.0\Facades\system.Linq.dll</HintPath>
</Reference>    
<Reference Include="System.Net">
  <HintPath>..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\MonoAndroid\v1.0\System.Net.dll</HintPath>
</Reference>        
<Reference Include="System.Net.Http">
  <HintPath>..\..\..\..\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\ReferenceAssemblies\Microsoft\Framework\MonoAndroid\v1.0\System.Net.Http.dll</HintPath>
</Reference>

Solution 3:[3]

My test project which was targeted at .Net 6 was referencing a different project that referenced .Net Framework 4.7.2. Once I removed that reference, the error was resolved

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel
Solution 2 Babak Naffas
Solution 3 Agreene