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

Duplicate item metadata present in generated editorconfig with CompilerVisibleItemMetadata #74234

Open
AR-May opened this issue Jul 2, 2024 · 1 comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@AR-May
Copy link
Member

AR-May commented Jul 2, 2024


Issue moved from dotnet/msbuild#10309

  • Please respond to @kzu.

From @kzu on Friday, June 28, 2024 5:43:42 PM

Issue Description

Suppose you have three totally independent packages that provide analyzers that leverage additional files to do some processing. Additionally, all packages consume the same file types (i.e. .resx files).

The first package might just add the files as AdditionalFiles and check for file extension inside the analyzer:

  <ItemGroup>
    <AdditionalFiles Include="@(EmbeddedResource)" />
  </ItemGroup>

The second package, however, might use an additional piece of metadata for the embedded resources to distinguish them, and thus has a slightly different version:

  <ItemGroup>
    <CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="SourceItemType" />
    <!-- NOTE: added again, same identity, but with the additional metadata this time -->
    <AdditionalFiles Include="@(EmbeddedResource)" SourceItemType="EmbeddedResource" />
  </ItemGroup>

A third one might even use another totally different metadata annotation on the same items for its own purposes:

  <ItemGroup>
    <CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="Foo" />
    <AdditionalFiles Include="@(EmbeddedResource)" Foo="Bar" />
  </ItemGroup>

The resulting editorconfig will currently now look like the following:

[.../Resources.resx]
build_metadata.AdditionalFiles.SourceItemType = 
build_metadata.AdditionalFiles.SourceItemType = EmbeddedResource
build_metadata.AdditionalFiles.SourceItemType = 
build_metadata.AdditionalFiles.Foo = 
build_metadata.AdditionalFiles.Foo = 
build_metadata.AdditionalFiles.Foo = Bar

Regardless of the somewhat unintuitive behavior when retrieving this item's metadata from the analyzer using the AnalyzerConfigOptionsProvider for that file (will you get the empty value or EmbeddedResource when asking for SourceItemType with .TryGetValue("build_metadata.AdditionalFiles.SourceItemType", out var value)?), if you have many files and multiple packages leveraging compiler item metadata, this duplication can make the editorconfig unnecessarily large (and potentially impossible to get the right value, depending on whichever behavior Roslyn itself provides for such duplicated entries).

Steps to Reproduce

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

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <!-- Some nuget package via .targets -->
  <ItemGroup>
    <AdditionalFiles Include="@(EmbeddedResource)" />
  </ItemGroup>

  <!-- Some other package via .targets -->
  <ItemGroup>
    <CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="SourceItemType" />
    <!-- NOTE: added again, same identity, but with the additional metadata this time -->
    <AdditionalFiles Include="@(EmbeddedResource)" SourceItemType="EmbeddedResource" />
  </ItemGroup>

  <!-- A third package via .targets -->
  <ItemGroup>
    <CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="Foo" />
    <!-- NOTE: added again, same identity, but with the additional metadata this time -->
    <AdditionalFiles Include="@(EmbeddedResource)" Foo="Bar" />
  </ItemGroup>

</Project>

Expected Behavior

Emitted editorconfig similar to:

[.../Resources.resx]
build_metadata.AdditionalFiles.SourceItemType = EmbeddedResource
build_metadata.AdditionalFiles.Foo = Bar

Actual Behavior

Emitted editorconfig is actually similar to:

[.../Resources.resx]
build_metadata.AdditionalFiles.SourceItemType = 
build_metadata.AdditionalFiles.SourceItemType = EmbeddedResource
build_metadata.AdditionalFiles.SourceItemType = 
build_metadata.AdditionalFiles.Foo = 
build_metadata.AdditionalFiles.Foo = 
build_metadata.AdditionalFiles.Foo = Bar

Analysis

No response

Versions & Configurations

17.11.0.27902

@jaredpar
Copy link
Member

jaredpar commented Jul 8, 2024

@chsienki PTAL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

2 participants