Sdk: Warning MSB4011: "Microsoft.CSharp.targets" cannot be imported again.

Created on 11 Jan 2018  路  28Comments  路  Source: dotnet/sdk

Steps to reproduce

dotnet build

Expected behavior

No build warnings

Actual behavior

C:\Program Filesdotnet\sdk\2.0.3\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(41,3): warning MSB4011: "C:\Program Filesdotnet\sdk\2.0.3\Microsoft.CSharp.targets" cannot be imported again. It was already imported at "C:\Projects\SomeProject.csproj (106,3)". This is most likely a build authoring error. This subsequent import will be ignored.

Environment data

dotnet --info output:
.NET Command Line Tools (2.0.3)

Product Information:
Version: 2.0.3
Commit SHA-1 hash: 12f0c7efcc

Runtime Environment:
OS Name: Windows
OS Version: 6.1.7601
OS Platform: Windows
RID: win7-x64
Base Path: C:\Program Filesdotnet\sdk\2.0.3\

Microsoft .NET Core Shared Framework Host

Version : 2.0.3
Build : a9190d4a75f4a982ae4b4fa8d1a24526566c69df

Most helpful comment

This is what a regular console app csproj file looks like:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

</Project>

With this, I don't see the error you are mentioning above.

All 28 comments

Can you share your csproj file? Did you do any customization to it?

This is what a regular console app csproj file looks like:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

</Project>

With this, I don't see the error you are mentioning above.

It is because of the import below, but if I remove it, then I cannot build as I'm targeting the two -netstandard2.0 and net46.

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
    </PropertyGroup>  
    <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Configuration" />
        <Reference Include="System.Data" />
        <Reference Include="System.IO.Compression.FileSystem" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Xml" />
        <Reference Include="System.Xml.Linq" />
    </ItemGroup>
    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

You should not need the bottom import for cross targeting. What are you seeing when you remove that import?

I get below and it thinks I'm targeting .NET Core 4.6?

C:\Program Files\dotnet\sdk\2.0.3\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.TargetFrameworkInference.targets(135,5): error : The current .NET SDK does not support targeting .NET Core 4.6. Either target .NET Core 2.0 or lower, or use a version of the .NET SDK that supports .NET Core 4.6.

Do you have any customization in your project? Something that is overriding TargetFrameworkIdentifier and setting it to .NetCoreApp?

I tried a csproj exactly like you have above (without the import) and it builds successfully for me. It has some warnings regarding conflict resolution, but other than that it works.

If possible, run a build with dotnet build /bl, which will generate a msbuild.binlog file. If you could then share that file, it may help us determine why this is happening to you.

Please get the attached and remove .txt from both binlog files.

This one is with original csproj file
msbuild.binlog.txt

This one is with modified csproj file after removing the import line
msbuild.binlog.modified.txt

And this one is from Visual Studio 2017 after I removed the import line
VsProjectFault_3c817999-e229-4d35-a816-8d2fe643f643.failure.txt

I noticed from the logs that you are trying to build a different project that references the failing one, is that the case? Could you maybe share a small repro of the issue in a zip or on github? I am suspecting it may be the interaction between the two projects that is causing this. Which would explain why I can't repro this with just my one project.

I wish I could do that :( Anyway, I've cleaned up the project file and managed to get one of the two building. But one gives me this error:

(RunResolvePackageDependencies target) ->
                     C:\Program Files\dotnet\sdk\2.0.3\Sdks\Microsoft.NET.Sdk\build\Microsoft.PackageDependencyResolution.targets(165,5): error : Assets file 'C:\Projects\SomeProject\obj\project.assets.json' doesn't have a target for '.NETFramework,Version=v4.0'. Ensure that restore has run and that you have included 'net40' in the TargetFrameworks for your project. [C:\Projects\SomeProject\Project.Common.csproj]

And I don't have net40 dependency anywhere in the project.

So I deleted all cached packages including the entire source branch and did a fresh get on everything. Now I'm getting error about the project.json, which isn't even part of anything.

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\NuGet\15.0\Microsoft.NuGet.targets(184,5): error : Your project.json doesn't have a runtimes section. You should add '"runtimes": { "win": { } }' to your project.json and then re-run NuGet restore.

The previous projects that weren't building (original issue) are now building without issues.

Are any of these a UWP projects? Also, @emgarten and @rrelyea to comment on the restore issue.

Try adding the property <RuntimeIdentifier>win7-x64</RuntimeIdentifier> if this a non-NETCore SDK project that is using PackageReference.

@dmumladze did @emgarten's suggestion above work for you?

It took me a while to get to the root cause and it seems to be the <TargetFrameworkVersion>v4.6</TargetFrameworkVersion> entry in the .csproj file. If I remove it, all hell breaks loose. I add it and I'm able to build and debug .NET Core 2 as well as .NET Framework 46 app.

Without this entry, the build error is:

C:\Program Filesdotnet\sdk\2.0.3\Sdks\Microsoft.NET.Sdk\build\Microsoft.PackageDependencyResolution.targets(165,5): error : Assets file 'C:...\Common\obj\project.assets.json' doesn't have a target for '.NETFramework,Version=v4.0'. Ensure that restore has run and that you have included 'net40' in the TargetFrameworks for your project. [C:...\Common.csproj]

I'm still not able to get rid of the original warning I opened this issue with:

Warning MSB4011: "Microsoft.CSharp.targets" cannot be imported again.

@livarcocc Are any of these a UWP projects? --- No

@emgarten Try adding the property win7-x64 --- I can try later, but I have multi-targeting project, so it has to be added with condition?

@dmumladze RIDs need to apply to all frameworks, so do not add a condition on it.

Attached the project file, when built, results with the build warning and I cannot seem to get rid of it. What is the issue?

C:\Program Files\dotnet\sdk\2.0.3\Sdks\Microsoft.NET.Sdk\Sdk\Sdk.targets(41,3): warning MSB4011: "C:\Program Files\dotnet\sdk\2.0.3\Microsoft.CSharp.targets" cannot be imported again. It was already imported at "C:\Projects\SomeProject.csproj (106,3)". This is most likely a build authoring error. This subsequent import will be ignored.

OneCRM.Common.Domain.csproj.txt

I've got 2059 build warnings once the entire production build is completed. I would really like to get rid of them, but I don't seem to find any way...

You can suppress it yes, but that's not what we want.

/p:NoWarn="MSB4011"

@livarcocc Could you or someone from your team help me take a look at this issue? Thanks!

C:\Usersdotnet-bot.nuget\packages\microbuild.plugins.swixbuild\1.1.0-g0701ee829f\build\Microsoft.VisualStudio.Setup.Tools.targets(106,3): error MSB4011: "C:\Usersdotnet-bot.nuget\packages\RoslynTools.MSBuild\0.4.0-alpha\tools\msbuild\Microsoft.Common.targets" cannot be imported again. It was already imported at "C:\Usersdotnet-bot.nuget\packages\microbuild.plugins.swixbuild\1.0.295\build\Microsoft.VisualStudio.Setup.Tools.targets (119,3)". This is most likely a build authoring error. This subsequent import will be ignored. [D:\j\workspace\windows_debug---24b23635\src\Setup\DevDivVsix\PortableFacades\PortableFacades.vsmanproj]

@dsplaisted @nguerrera might be able to help.

@JieCarolHu The error you're hitting doesn't look like it's a bug in the SDK. It looks like there are two different versions of the MicroBuild.Plugins.SwixBuild plugin in the MicroBuildPluginDirectory (versions 1.1.0-g0701ee829f and 1.0.295). They are both getting imported by a statement like this:

<Import Project="$(MicroBuildPluginDirectory)\MicroBuild.Plugins.*\**\build\MicroBuild.Plugins.*.props" Condition="'$(MicroBuildPluginDirectory)' != ''" />

Then when they both try to import the common targets, you get the duplicate import error.

Given that one of the issues reported here have been addressed, @dmumladze is this still happening to you?

Not if I add /p:NoWarn:MSB4011 to the command-line.

@dmumladze I noticed that the project you added above is actually using packages.config. We don't support packages.config with SDK based projects and the dotnet CLI. Could you try replacing that with packagereferences and seeing if that fixes your issues?

@livarcocc It's using PackageReference, but should I just remove packages.config completely:

  <ItemGroup>
    <None Include="packages.config">
      <SubType>Designer</SubType>
    </None>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="log4net" Version="2.0.8" />
    <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
  </ItemGroup>

Do you think multi-targetting is not an issues with that project?

  <PropertyGroup> 
    <TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>   
  </PropertyGroup>

I get the same error when I target two SDKs.
<Project Sdk="Microsoft.NET.Sdk;Microsoft.NET.Sdk.Web;Microsoft.NET.Sdk.WindowsDesktop"> <PropertyGroup> <OutputType>WinExe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> </Project>

I need them because I am hosting a Web Api inside WPF

@prashantchoudhary targeting multiple SDKs like that is not supported. If you target WindowsDesktop, it implicitly brings in Microsoft.Net.SDK for you.

I get it, but mostly the problem is due to these two Sdk="Microsoft.NET.Sdk.Web;Microsoft.NET.Sdk.WindowsDesktop"

However I can build it and use it and it works fine. Why I have two targets is because I am hosting few Controllers inside my WPF app. (Residue from some legacy design)

Was this page helpful?
0 / 5 - 0 ratings