project-system icon indicating copy to clipboard operation
project-system copied to clipboard

.netStandard2.0 DefaultCompileItems causing problems when switching between Debug/Release builds

Open StacyHenwood opened this issue 8 years ago • 20 comments

I have a .netStandard2.0 class library that contains about 25 classes.

By default, in Debug build. The library compiles perfectly fine, and I can debug it normally.

But, when I try to do a Release build (in VS2017), I get the following message:

Duplicate 'Compile' items were included. The .NET SDK includes 'Compile' items from your project directory by default. You can either remove these items from your project file, or set the 'EnableDefaultCompileItems' property to 'false' if you want to explicitly include them in your project file. For more information, see https://aka.ms/sdkimplicititems. The duplicate items were: [SNIP] C:\Program Files\dotnet\sdk\2.1.104\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.DefaultItems.targets 262

After some research, it appears that I need to remove the <ItemGroup><Compile Include="{filename}"></ItemGroup> nodes from the .csproj file. So I do so. Yay, it now compiles in Release mode.

However, when I switch back to Debug mode, all the files disappear from the Solution Tree, and now it wont compile in Debug mode. They literally disappear, and the project thinks it has no files.

Adding the following node into the project file fixes this and enables me to build in both Debug/Release.

<PropertyGroup>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

Visual Studio 2017 15.6.5

StacyHenwood avatar May 09 '18 00:05 StacyHenwood

@StacyHenwood Can you show me your project file?

davkean avatar May 09 '18 00:05 davkean

Here you go:

netstandard2.0 true Stacy Henwood [REMOVED] [REMOVED] 1.0.7 C:\Users\stacy\ false *.xaml

On Wed, May 9, 2018 at 12:35 PM, David Kean [email protected] wrote:

@StacyHenwood https://github.com/StacyHenwood Can you show me your project file?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/dotnet/project-system/issues/3526#issuecomment-387584191, or mute the thread https://github.com/notifications/unsubscribe-auth/ANNB998IYNZsTLcuaE1jl3xqin-ythIjks5twjnCgaJpZM4T3hSn .

-- Regards,

Stacy

StacyHenwood avatar May 09 '18 00:05 StacyHenwood

If you change your project to the following, what happens?

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <Authors>Stacy Henwood</Authors>
    <Company>[REMOVED]</Company>
    <Product>[REMOVED]</Product>
    <Version>1.0.7</Version>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>C:\Users\stacy\</OutputPath>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
  </ItemGroup>

</Project>

davkean avatar May 09 '18 00:05 davkean

Same issue.

Release: compiles normally. Debug: all the files disappear from the project

StacyHenwood avatar May 09 '18 00:05 StacyHenwood

@davkean won't this still be required in @StacyHenwood's project?

  <ItemGroup>
    <Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="MainPage.xaml.cs">
      <DependentUpon>*.xaml</DependentUpon>
    </Compile>
  </ItemGroup>

jmarolf avatar May 09 '18 01:05 jmarolf

I don't know why that MainPage.xaml.cs node is even there - that file does not exist in the project at all (and never has!) - I don't think its required.

StacyHenwood avatar May 09 '18 01:05 StacyHenwood

@StacyHenwood oh, that's good to know! That sort of item group is normally required if you are building a user interface in a project with <EnableDefaultCompileItems>true</EnableDefaultCompileItems> so that the xaml files correctly update. If no such file exists then you can safely remove it from your project.

on my machine I've created a new .net standard class library with the following project file:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <Authors>Jonathon Marolf</Authors>
    <Company>Microsoft</Company>
    <Product>Test</Product>
    <Version>1.0.7</Version>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>C:\Users\jmarolf\Downloads\OutputPath</OutputPath>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
  </ItemGroup>

</Project>

Debug and release work for me: image image

So there must be something else I am missing. I assume you solution is proprietary so you can't just zip it up and send it to via email. @davkean @Pilchie do either of you know the process of us signing an NDA so @StacyHenwood can send us their solution?

jmarolf avatar May 09 '18 01:05 jmarolf

I'm just upgrading my VS installation to the latest release (15.7.1?) as I noticed that there was a new version. Ill re-check after this has finished installing, and if the problem persists, will send some screenshots for you.

StacyHenwood avatar May 09 '18 01:05 StacyHenwood

I think i have worked out what it is. Here is what I did:

I updated to VS2017 5.7.1. Same issue was observed.

I decided to see if this was limited to my machine by zipping up the project and sending it to another developer. Initially, they could not build AT ALL due to a an error about GenerateDepsFile failing. What I found interesting about this, is that the path that was being reported as missing was MY local path. Huh.

So, I removed this node from the project on the other developers machine, as it was the ONLY reference that to the path in the entire solution.

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>C:\Users\stacy\</OutputPath>
  </PropertyGroup>

After this the other developer was able to compile without issue, both in Release and Debug mode.

I went back to my machine, removed the same node, and now the project compiles fine, both in Debug and Release mode, without needing the EnableDefaultCompileItems node as discussed earlier.

So, the problem seems to be caused by the OutputPath property. It would be interesting to see if you can emulate this with your local project?

So, for clarity, my project file now looks like this and I can build in release and debug modes.

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <Authors>Stacy Henwood</Authors>
    <Company>[REMOVED]</Company>
    <Product>[REMOVED]</Product>
    <Version>1.0.7</Version>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
  </ItemGroup>
</Project>

Thank you for your assistance!

StacyHenwood avatar May 09 '18 02:05 StacyHenwood

I guess this might have something to do with permissions on that path and/or nuget outputting. The only downside that I have noted is that now my nuget package output is way down in the \bin output folder - but I can live with that!

StacyHenwood avatar May 09 '18 02:05 StacyHenwood

possibly related to: https://github.com/dotnet/sdk/issues/706 ?

StacyHenwood avatar May 09 '18 02:05 StacyHenwood

@StacyHenwood glad everything is working. I'm going to close this issue. Please reopen it if you encounter this problem again

jmarolf avatar May 09 '18 19:05 jmarolf

Happy for you to close it. Is this working as designed? It's very confusing as a user, and the information that can be found only moves the problem elsewhere.

StacyHenwood avatar May 09 '18 20:05 StacyHenwood

@StacyHenwood here is how I expect things to work:

  1. User creates .NET standard class library in Visual Studio image
  2. User goes to project properties and sets up their nuget package image NOTE: at this point everything builds and a nuget package is produced on build
  3. User edits their csproj file to place project output in a different path
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <PackageId>ExamplePackage</PackageId>
    <Authors>Jon Marolf</Authors>
    <Company>Microsoft</Company>
    <Version>1.0.7</Version>
    <Product>ExamplePackage</Product>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputPath>C:\Users\jmarolf\Downloads\DebugPath</OutputPath>
  </PropertyGroup>
</Project>

@StacyHenwood is the above what you did? Can you tell me what you did differently? Thanks!

jmarolf avatar May 09 '18 20:05 jmarolf

At no time did I ever manually amend the .csproj file.

The OutputPath node was added by the VS2017 at some point (sorry, I don't know when) and this was the root cause.

The thing that (IMHO) needs to be fixed is the physical removal of the classes from the project when you swapped back to Debug mode in the UI - this was very confusing, and I only stumbled across the fix because I took this project to a physically different machine.

StacyHenwood avatar May 09 '18 21:05 StacyHenwood

The OutputPath node was added by the VS2017 at some point (sorry, I don't know when) and this was the root cause.

It would be useful to know what caused that to happen. At no point should file entries have been added to your csproj file, it should have always looked like this:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <Authors>Stacy Henwood</Authors>
    <Company>[REMOVED]</Company>
    <Product>[REMOVED]</Product>
    <Version>1.0.7</Version>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
  </ItemGroup>
</Project>

Can you give any hint of what operations you were doing that caused this? Thanks!

jmarolf avatar May 09 '18 21:05 jmarolf

I just managed to get the UI to do this again (VS2017 5.7.1).

If you go to the project properties, (Build tab), and use the Browse button to select a local path, then the same issue occurs. I guess this is by design, and I must have done this at some point - I can't imagine that VS would find my local user dir and add it for any good reason.

Interestingly though, if I set the folder to c:\users\stacy using the UI, then the value in the .csproj is set to c:\users\stacy, but when I reopen the properties in the UI the output path is set to c:\users\stacy\netstandard2.0\ despite that not being what is actually in the .csproj. By design?

image

I guess, considering this, that you could consider this a PEBKAC error. It's just confusing from the users perspective as the adding/removing of files from the UI is in no way related to the output path.

StacyHenwood avatar May 09 '18 21:05 StacyHenwood

but when I reopen the properties in the UI the output path is set to c:\users\stacy\netstandard2.0\ despite that not being what is actually in the .csproj. By design?

This is by design. The SDK will actually put your dlls in c:\users\stacy\netstandard2.0\. The build appends your target framework name (netstandard2.0) to the end of the output path variable (OutputPath). In the property pages we made the decision to do the evaluation and show you the actual location that your build artifacts are going to end up instead of just the value of the OutputPath variable. I do think this can be confusing though. What would you prefer instead?

jmarolf avatar May 09 '18 21:05 jmarolf

I think that your explanation makes sense. Its just not obvious from a users perspective. I don't really have an opinion on this specific thing - I'm not sure how I would do it differently.

Interestingly, now that I have done this again (through the UI), the problem of disappearing files is no longer apparent. I suspect that this project just got itself in a weird state. Without a reproducible test case, I am aware that this will be difficult to resolve.

More than happy for you to leave this closed, and if I come across this again, I'll try to send you a project that exhibits the file removal issue.

Thank you very much for your help!

StacyHenwood avatar May 09 '18 21:05 StacyHenwood

I think that your explanation makes sense. Its just not obvious from a users perspective. I don't really have an opinion on this specific thing - I'm not sure how I would do it differently.

No problem! Its on us to figure out something better.

if I come across this again, I'll try to send you a project that exhibits the file removal issue.

Please do, I'll close this for now

jmarolf avatar May 09 '18 21:05 jmarolf