Skip to content

new File gets removed from custom ItemGroup #4483

Open

Description

@SabotageAndi

I have following csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <CustomFileTypeItemGroup Include="**\*.customFileType"></CustomFileTypeItemGroup>
  </ItemGroup>
</Project>

If I add via the Add new File dialog a new File from Text File template with the name TextFile1.customFileType, Visual Studio changes the csproj to following:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <CustomFileTypeItemGroup Include="**\*.customFileType"></CustomFileTypeItemGroup>
  </ItemGroup>

  <ItemGroup>
    <CustomFileTypeItemGroup Remove="TextFile1.customFileType" />
  </ItemGroup>
roject>

It is removing the new file from the custom ItemGroup.

Is there any workaround to stop this? This behavior makes us problems in SpecFlow, as VS is removing items from our ItemGroups (they are defined in a props file in the NuGet package), which we need in a MSBuild Task.

This happens in Visual Studio 2017 15.9.5 and Visual Studio 2017 16.0.0 Preview 1.1.

Activity

  1. davkean commented on Jan 22, 2019

    @davkean
    Member

    This is by-design. You are hitting two different things;

    1. The default item type for extensions that do not have any registration is the <None/> item type.

    2. Visual Studio tries very hard to avoid including an item in multiple item types within a project. It's already included in the <None /> item (the default glob), so it removes it from the <CustomFileTypeItemGroup/>

    You need registration for your customFileType to avoid it being assigned to the <None/> item type. You can read more here: https://github.com/Microsoft/VSProjectSystem/blob/master/doc/extensibility/custom_item_types.md.

  2. added
    Discussion/Question
    on Jan 22, 2019
  3. SabotageAndi commented on Jan 23, 2019

    @SabotageAndi
    Author

    Thanks for pointing me in the right direction @davkean.

    I read the documentation, but the examples were all for new project types. But I would need this in the normal C# and VB projects.
    Is there an example how to extend them with a new registration? Is this possible at all or is a change to the project system needed?

  4. added
    Triage-Approved
    on Jan 30, 2019
  5. added this to the Backlog milestone on Jan 30, 2019
  6. SabotageAndi commented on Feb 4, 2019

    @SabotageAndi
    Author

    Sorry for pinging again @davkean.
    This issue is blocking us from releasing SpecFlow 3 with .NET Core support.

    If it's not possible to extend the project system via VSIX or NuGet package, we have to find another way to solve this.
    But this would mean some ugly changes on our side, that I want to avoid.

    Thanks for your help!

  7. davkean commented on Feb 4, 2019

    @davkean
    Member

    You extend existing C#/VB the same way as a new project type - there's no difference.

  8. SabotageAndi commented on Mar 4, 2019

    @SabotageAndi
    Author

    I tried the last days to get this to work, but I wasn't successful.
    My code is here: https://github.com/SabotageAndi/CPSCustomItemTypeExample

    It's based on this example: https://github.com/Microsoft/VSProjectSystem/tree/master/samples/CpsExtension

    An example is also included: https://github.com/SabotageAndi/CPSCustomItemTypeExample/tree/master/examples/ReproItemGroupChange
    When I add a TextFile1.customFileType file, it is still generating the ItemGroup- Remove statement in the csproj.

    Any ideas what I am missing @davkean? Thanks!

  9. SabotageAndi commented on Mar 5, 2019

    @SabotageAndi
    Author

    Ok, I noticed that I don't import the https://github.com/SabotageAndi/CPSCustomItemTypeExample/blob/master/src/CpsExtensionCustomItemType.Vsix/BuildSystem/CpsExtension.DesignTime.targets not in the project.

    This seems to be necessary. Is this correct?
    Can't be than the whole customization done in a NuGet package and not in a VSIX?

  10. SabotageAndi commented on Mar 8, 2019

    @SabotageAndi
    Author

    Ok, I got it to work within a NuGet package. But I got into a new problem.
    When I add a new file with the .customFileType ending, it is removing it from the <None> ItemGroup. And with it, it is not displayed in the Solution Explorer.

    CSProj before:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <Import Project="ItemGroups.props" />
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <PropertyGroup>
        <CpsExtensionDesignTimeTargetsPath Condition="'$(CpsExtensionDesignTimeTargetsPath)' == ''">$(MSBuildThisFileDirectory)\BuildSystem\CpsExtension.DesignTime.targets</CpsExtensionDesignTimeTargetsPath>
      </PropertyGroup>
    
      <Import Project="$(CpsExtensionDesignTimeTargetsPath)" Condition="'$(DesignTimeBuild)' == 'true'" />
    
    </Project>

    After:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <Import Project="ItemGroups.props" />
    
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
    
      <PropertyGroup>
        <CpsExtensionDesignTimeTargetsPath Condition="'$(CpsExtensionDesignTimeTargetsPath)' == ''">$(MSBuildThisFileDirectory)\BuildSystem\CpsExtension.DesignTime.targets</CpsExtensionDesignTimeTargetsPath>
      </PropertyGroup>
    
      <Import Project="$(CpsExtensionDesignTimeTargetsPath)" Condition="'$(DesignTimeBuild)' == 'true'" />
    
      <ItemGroup>
        <customFileTypeSourceFile Remove="TextFile1.customFileType" />
      </ItemGroup>
    
      <ItemGroup>
        <None Remove="TextFile1.customFileType" />
      </ItemGroup>
    
      <ItemGroup>
        <customFileTypeCompile Include="TextFile1.customFileType" />
      </ItemGroup>
    
    </Project>

    In this example there is a remove and include of the customFileTypeCompile ItemGroup present. This doesn't happen in my real project.
    Only the None ItemGroup Remove is in the real project.

    But why is this added? Or why is the file not displayed in the Solution Explorer?

    Project is here: https://github.com/SabotageAndi/CPSCustomItemTypeExample/blob/master/examples/ReproItemGroupChange/ReproItemGroupChange/ReproItemGroupChange.csproj

  11. SabotageAndi commented on Mar 8, 2019

    @SabotageAndi
    Author

    I got the file to display in the real project. There was a typo in one import statement.
    But the removal in the None- ItemGroup is still there.
    I want to minimize the changes to the csproj. Is it possible to not have this change?

  12. dannyBies commented on Mar 20, 2019

    @dannyBies

    Any update on this? I would love to be able to use a stable version of Specflow running on .NET Core!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Discussion/QuestionTriage-Approved

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions