Search code examples
nugetcsprojmsbuild-taskcsharp-source-generator

Hide embedded nuget files from VS solution explorer


I create my first Nuget Package of my incremental source generator. All was OK, targeting it as an analyzer in .csproj, embedded under analyzer/cs Nuget subfolders.

But now I want to improve my package to allow consumer to directly edit a given shipped .cs file along this package to provide a customization of my (non editable) generated sources.

So I modified my .csproj in order to embed my .cs file and also create a a build/xxx.props files to drive the installed Nuget package of the consuming projects with a task to copy the embedded .cs in their solution.

<ItemGroup>
  <None Include="Custom_Warnings\SettableNTimesProperty.cs">
    <Pack>true</Pack>
    <PackagePath>Copied_Files\Custom_Warnings</PackagePath>
    <PackageCopyToOutput>false</PackageCopyToOutput>
  </None>
  <None Include="build\*.props">
    <Pack>true</Pack>
    <PackagePath>build\$(TargetFramework)\</PackagePath>
    <PackageCopyToOutput>false</PackageCopyToOutput>
  </None>
</ItemGroup>

That work, but as a unwanted side effect, now consuming projects see in solution explorer the .props file (and my readme files too). Those wasn't visible back then when my Nuget was only targeted as an analyzer.

How to hide them back ?

Note I tried this, but it didn't work for me.

Here is what i saw in solution explorer (and would like to not being seen anymore if possible) : enter image description here

Edit, I found the, NOT automatic, solution is to set ExcludeAssets="Compile"on my consuming project .csproj :

<ItemGroup>
  <PackageReference Include="SettableOnceProperty" Version="0.1.1">
    <ExcludeAssets>Compile</ExcludeAssets>
  </PackageReference>
</ItemGroup>

So now my asking may become : How to, automatically, add this <ExcludeAssets>Compile</ExcludeAssets> in my consuming projects .csproj ? Do you know if it is doable via the .props file ?


Solution

  • Turns out this is by design, in order to prevent hiding any potential malicious or unwanted msbuild actions someone could publish along its package. I can live with that, though i'd rather inverse the logic to hide it by default (majority of use case) and allow to disclose it at will (to allow checking any potential unwanted / malicious actions).

    By the way, according to the answer i got on GitHub, it seems like my "manual" solution of using ExcludeAssets="Compile" is a bug, and shouldn't be allowed.