Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate error message for duplicate items #624

Merged
merged 4 commits into from
Jan 13, 2017

Conversation

dsplaisted
Copy link
Member

Fixes #600

Generate an error message if there are duplicate Compile, EmbeddedResource, or Content items. The error message looks like this:

C:\git\dotnet-sdk\bin\Debug\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.DefaultItems.targets(66,5): error : 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://github.com/dotnet/sdk The duplicate items were: 'Class1.cs', 'DuplicateCompileItems.cs'

If default items are disabled, then the error message won't include information about how to disable them:

C:\git\dotnet-sdk\bin\Debug\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.DefaultItems.targets(66,5): error : Duplicate Compile items were included. The duplicate items were: 'DuplicateCompileItems.cs'

Open questions or other notes:

  • Create a fwlink that will point to the documentation for default items. @blackdwarf, can you do this?
  • Should the .NET SDK check for duplicate Content items, or since it doesn't have any logic for them by default should that go in the Web SDK?
  • This doesn't check for duplicate None items, as I don't think those would typically cause an issue
  • Does formatting items in a list need to be localized?
  • Should we check for duplicate items at all if the default items are disabled?

<value>Duplicate {0} items were included.{1} The duplicate items were: {2}</value>
</data>
<data name="DuplicateItemsHowToFix" xml:space="preserve">
<value> You can either remove these items from your project file, or set the '{0}' property to '{1}' if you want to explicitly include them in your project file.{2}</value>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that the experiences of #623 and dotnet/project-system#1126 indicate that we should also promote the Update and Remove mechanisms here. But that makes a very long error, which isn't ideal.

MoreInformationLink="$(DefaultItemsMoreInformationLink)"
/>

<!-- TODO: Do we check for duplicate content items here, or in the Web SDK? EnableDefaultContentItems isn't defined or used at all by the .NET SDK -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say to do it here because Content is a type known in Common.targets, and this is the closest layer above that. If it were defined and used only in Web, I'd say to move it there.

}

// TODO: Does quoting and the separator between items in the list need to be localized?
string duplicateItemsFormatted = string.Join(", ", duplicateItems.Select(d => $"'{d.Key}'"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what the right answer is. My guess is: change this to a semicolon (like MSBuild uses to separate items) and don't localize it (MSBuild itself doesn't seem to, probably in part because there's no distinction between "format list of items to print" and "format list of items to pass a string to code" which would have different localization needs).

<value>Duplicate {0} items were included.{1} The duplicate items were: {2}</value>
</data>
<data name="DuplicateItemsHowToFix" xml:space="preserve">
<value> You can either remove these items from your project file, or set the '{0}' property to '{1}' if you want to explicitly include them in your project file.{2}</value>
Copy link
Contributor

@nguerrera nguerrera Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really hard to follow for translators: leading whitespace, placeholder followed by two spaces followed by another sentence, placeholder immediately after a sentence. Can we just log consecutive independent messages? If not, maybe don't worry about DRY and just put the full text of each message a user can see so the translator has all of the context. At a minimum, use the <comment> element to explain how these compose.


protected override void ExecuteCore()
{
var duplicateItems = Items.GroupBy(i => i.ItemSpec).Where(g => g.Count() > 1).ToList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why ToList()?

Copy link
Contributor

@nguerrera nguerrera Jan 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also is g.Count() cheap here in practice? (do the groups implement ICollection?)

I would be interested in seeing some measurment where this task shows up on the perf summary. Maybe it doesn't register in comparison to surrounding logic, but my instinct from lower level work is to cringe at all of this LINQ.

@dsplaisted
Copy link
Member Author

Do we check for duplicate content items here, or in the Web SDK? EnableDefaultContentItems isn't defined or used at all by the .NET SDK

I'd say to do it here because Content is a type known in Common.targets, and this is the closest layer above that. If it were defined and used only in Web, I'd say to move it there.

I think the question is, are we checking for duplicate items in general, or are we just checking for duplicate items caused by the implicit includes in the SDK? Does adding an extra check and error message add value if the problem is unrelated to the implicit includes?

I'm leaning towards saying that these checks and error messages should only apply when implicit includes are enabled. That would also mean that the validation for Content items more logically belongs in the Web SDK.

@rainersigwald @nguerrera @srivatsn Thoughts?

… generate error if implicit items are enabled
@dsplaisted
Copy link
Member Author

I've updated the PR to only use one resource string for the error message. It should now look like this (mostly the same as it was previously):

C:\git\dotnet-sdk\bin\Debug\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.DefaultItems.targets(66,5): error : 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. The duplicate items were: 'Class1.cs'; 'DuplicateCompileItems.cs' [C:\git\dotnet-sdk\bin\Debug\Tests\It_gives_an_error_message_if_duplicate_compile_items_are_included\DuplicateCompileItems\DuplicateCompileItems\DuplicateCompileItems.csproj]

I've also modified it to only generate the error if the implicit items were enabled. If not, then it will be up to targets further down the pipeline to detect the issue and generate an error as they always have.

@dsplaisted
Copy link
Member Author

@MattGertz for approval

Scenario

A user upgrades from RC2 to RC3. The projects they created with RC2 will generate compile errors saying that the source files were specified multiple times. This is because we now include the source files implicitly. This change will generate an error message that explains how to fix the issue:

error : 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. The duplicate items were: 'Class1.cs'; 'DuplicateCompileItems.cs'

Bug

#600

Workarounds

n/a

Risk

Low - Tests should cover any impacted scenarios

Performance Impact

TBD but should be low - it involves calling an additional task for each of the Compile, EmbeddedResource, and Content item types and checking for duplicates.

Regression Analysis

n/a


<PropertyGroup>
<!-- TODO: Create a fwlink for this -->
<DefaultItemsMoreInformationLink>https://github.com/dotnet/sdk</DefaultItemsMoreInformationLink>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to have an acceptable link before we merge.

{
if (DefaultItemsEnabled && DefaultItemsOfThisTypeEnabled)
{
var duplicateItems = Items.GroupBy(i => i.ItemSpec).Where(g => g.Count() > 1).ToList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can required inputs be null? Change to

(Items ?? Enumerable<ITaskItem>.Empty())

@natidea
Copy link
Contributor

natidea commented Jan 12, 2017

Shiproom approved

@blackdwarf
Copy link

@dsplaisted where does the new link go to?

@dsplaisted
Copy link
Member Author

@blackdwarf It goes to https://github.com/dotnet/core/blob/master/release-notes/sdk-implicit-items.md. You're an owner of the link as well @terrajobst, @richlander, and a few others.

@AdrienTorris
Copy link

AdrienTorris commented Mar 9, 2017

When I upgraded my project from VS2017 RC to VS2017 it causes an error in all my class library projects. After reinstalling VS (yes I read that it was the solution ...) I deleted the line <Compile Include="**\*.cs" /> in all the csproj files of my class libraries to can build again.
Is it the good workaround ? Or set the 'EnableDefaultCompileItems' property to 'false' is a the good way to pass through this situation ?

@richlander
Copy link
Member

mmitche pushed a commit to mmitche/sdk that referenced this pull request Jun 5, 2020
…0190424.4 (dotnet#624)

- Microsoft.AspNetCore.Mvc.Analyzers - 3.0.0-preview6-19224-04
- Microsoft.AspNetCore.Mvc.Api.Analyzers - 3.0.0-preview6-19224-04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants