Skip to content

NUnit3TestAdapter NuGet package hides included assets #1052

Open

Description

@Stefan75

Hello,

I have created a project with a dependency to following NuGet package:
NUnit3TestAdapter 4.3.1

After restore and build I observed that the created project.assets.json file does not contain the assets of the package:

nunit.engine.api.dll
nunit.engine.core.dll
nunit.engine.dll
NUnit3.TestAdapter.dll
NUnit3.TestAdapter.pdb
Project.dll
Project.pdb
testcentric.engine.metadata.dll

This leads to missing assets during a SBOM creation, or we would miss the files we want to sign.

I have created a NuGet issue for it:
NuGet/Home#12406

This issue has been closed with following comment:

The only asset consumed by NuGet is build/net35/NUnit3TestAdapter.props. If the package author used contentFiles instead, they would be listed in the assets file. I suggest you file an issue against the package author to make this package more compatible with SBOM.

Please have a look to the issue, maybe the nuspec file could easily improved so that all assets are listed.

Thanks,
Stefan

Activity

  1. changed the title [-]NUnit3TestAdapter NuGet package hides copied assets[/-] [+]NUnit3TestAdapter NuGet package hides included assets[/+] on Feb 3, 2023
  2. OsirisTerje commented on Feb 5, 2023

    @OsirisTerje
    Member

    Not suite sure what @jeffkl is pointing to. The nuspec file contains all the files needed. The props file are just there to cover the different targets for MSBuild, so not sure why they would include that file, and not the Files listed in the nuspec. Content files are normally other non-executable files, (ref https://devblogs.microsoft.com/nuget/nuget-contentfiles-demystified/) so don't think it is wise to include the executables there. Suggest @jeffkl elaborates on this.

  3. Stefan75 commented on Feb 6, 2023

    @Stefan75
    Author

    Hello,

    thanks for the fast response.

    I am not an expert, my I understand @jeffkl in a way that the current nuspec is using a more "low-level" way via none to copy the required files to the output directory:

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemGroup>
        <None Include="$(MSBuildThisFileDirectory)NUnit3.TestAdapter.dll">
          <Link>NUnit3.TestAdapter.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Visible>False</Visible>
        </None>
        <None Include="$(MSBuildThisFileDirectory)NUnit3.TestAdapter.pdb" Condition="Exists('$(MSBuildThisFileDirectory)NUnit3.TestAdapter.pdb')">
          <Link>NUnit3.TestAdapter.pdb</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Visible>False</Visible>
        </None>
        <None Include="$(MSBuildThisFileDirectory)nunit.engine.dll">
          <Link>nunit.engine.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Visible>False</Visible>
        </None>
        <None Include="$(MSBuildThisFileDirectory)nunit.engine.api.dll">
          <Link>nunit.engine.api.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Visible>False</Visible>
        </None>
        <None Include="$(MSBuildThisFileDirectory)nunit.engine.core.dll">
          <Link>nunit.engine.core.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Visible>False</Visible>
        </None>
        <None Include="$(MSBuildThisFileDirectory)testcentric.engine.metadata.dll">
          <Link>testcentric.engine.metadata.dll</Link>
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Visible>False</Visible>
        </None>
      </ItemGroup>
    </Project>

    It seems for me that the asset creation does not "monitor" such low-level operations to collect all assets.

    In my understanding by the usage of nuspec contentfiles those files becomes visible to the NuGet client and not just to msbuild itself.

    I will ask jeffkl for it...

  4. OsirisTerje commented on Feb 6, 2023

    @OsirisTerje
    Member

    This section is already in the nuspec

    <files>
        <file src="build\net35\NUnit3.TestAdapter.dll" target="build\net35\NUnit3.TestAdapter.dll" />
        <file src="build\net35\NUnit3.TestAdapter.pdb" target="build\net35\NUnit3.TestAdapter.pdb" />
        <file src="build\net35\nunit.engine.dll" target="build\net35\nunit.engine.dll" />
        <file src="build\net35\nunit.engine.api.dll" target="build\net35\nunit.engine.api.dll" />
        <file src="build\net35\nunit.engine.core.dll" target="build\net35\nunit.engine.core.dll" />
        <file src="build\net35\testcentric.engine.metadata.dll" target="build\net35\testcentric.engine.metadata.dll"/>
        <file src="build\net35\NUnit3TestAdapter.props" target="build\net35\NUnit3TestAdapter.props" />
    
        <file src="build\netcoreapp3.1\NUnit3.TestAdapter.dll" target="build\netcoreapp3.1\NUnit3.TestAdapter.dll" />
        <file src="build\netcoreapp3.1\NUnit3.TestAdapter.pdb" target="build\netcoreapp3.1\NUnit3.TestAdapter.pdb" />
        <file src="build\netcoreapp3.1\nunit.engine.dll" target="build\netcoreapp3.1\nunit.engine.dll" />
        <file src="build\netcoreapp3.1\nunit.engine.api.dll" target="build\netcoreapp3.1\nunit.engine.api.dll" />
        <file src="build\netcoreapp3.1\nunit.engine.core.dll" target="build\netcoreapp3.1\nunit.engine.core.dll" />
        <file src="build\netcoreapp3.1\testcentric.engine.metadata.dll" target="build\netcoreapp3.1\testcentric.engine.metadata.dll"/>
        <file src="build\netcoreapp3.1\NUnit3TestAdapter.props" target="build\netcoreapp3.1\NUnit3TestAdapter.props" />
      </files>

    This should be used.

    Adding executables in a contentFiles section doesn't seem correct.

  5. jeffkl commented on Feb 6, 2023

    @jeffkl

    There are two .nuspecs in NuGet. The one checked into this repository, tells NuGet how to pack up a package. This one contains all of the files so that they end up in the .nupkg. The .nuspec is then added to the .nupkg but files that are already in the .nupkg. are stripped out because they don't take part in restore.

    The .nupsec in the .nupkg looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
      <metadata>
        <id>NUnit3TestAdapter</id>
        <version>4.3.1</version>
        <title>NUnit3 Test Adapter for Visual Studio and DotNet</title>
        <authors>Charlie Poole, Terje Sandstrom</authors>
        <requireLicenseAcceptance>false</requireLicenseAcceptance>
        <license type="expression">MIT</license>
        <licenseUrl>https://licenses.nuget.org/MIT</licenseUrl>
        <projectUrl>https://docs.nunit.org/articles/vs-test-adapter/Index.html</projectUrl>
        <iconUrl>https://cdn.rawgit.com/nunit/resources/master/images/icon/nunit_256.png</iconUrl>
        <description>The NUnit3 TestAdapter for Visual Studio, all versions from 2012 and onwards, and DotNet (incl. .Net core).
    
          Note that this package ONLY contains the adapter, not the NUnit framework.
          For VS 2017 and forward, you should add this package to every test project in your solution. (Earlier versions only require a single adapter package per solution.)</description>
        <summary>NUnit3 adapter for running tests in Visual Studio and DotNet. Works with NUnit 3.x, use the NUnit 2 adapter for 2.x tests.</summary>
        <releaseNotes>See https://docs.nunit.org/articles/vs-test-adapter/Adapter-Release-Notes.html</releaseNotes>
        <copyright>Copyright (c) 2011-2021 Charlie Poole, 2014-2022 Terje Sandstrom</copyright>
        <language>en-US</language>
        <tags>test visualstudio testadapter nunit nunit3 dotnet</tags>
        <repository type="git" url="https://github.com/nunit/nunit3-vs-adapter" />
      </metadata>
    </package>

    When the package is restored, NuGet looks at the folders in the package and sees a build folder, a folder for the target framework, and a file named NUnit3TestAdapter.props:

    image

    So in this case, the only "asset" that NuGet sees during restore, is the .props file so an import is added. This import, NUnit3TestAdapter.props, implements custom logic to copy the files to the output directory. Since these files are placed on disk using custom logic, NuGet does not report their existence in its own assets file because it does not know what they are for.

    This package could instead place the files in contentFiles and indicate that they need to be copied to the output directory, and NuGet would generate the appropriate <None /> items with CopyToOutputDirectory=PreserveNewest. The contentFiles would also show up in the project.assets.json file because NuGet would know they are content files. This would help @Stefan75 detect which files take place in a build for SBOM purposes.

    That said, the package still functions correctly, it just implements content files in a custom way so NuGet does not report them in the assets file after restore.

  6. OsirisTerje commented on Feb 7, 2023

    @OsirisTerje
    Member

    @jeffkl Ok, but I assume you then mean the 4 "engine" files, not the adapter.dll ?

  7. Stefan75 commented on Feb 7, 2023

    @Stefan75
    Author

    @OsirisTerje

    From my side, I talk about all files. Because I have following use-cases in mind:

    • Creation of a SBOM where we know the original of all files in our output dir
    • Further processing of those files (e.g. code signing)

    Regards,
    stefan

  8. jeffkl commented on Feb 7, 2023

    @jeffkl

    @jeffkl Ok, but I assume you then mean the 4 "engine" files, not the adapter.dll ?

    All of the files could be just content files that get copied to the output directory. The way the Visual Studio test adapter logic works, is it just looks in the output directory for files named *.TestAdapter.dll. So the goal of this package is really to just place the files in the output directory and contentFiles would work just fine.

  9. OsirisTerje commented on Jul 3, 2023

    @OsirisTerje
    Member

    @jeffkl I looked at https://learn.microsoft.com/en-us/nuget/reference/nuspec#using-the-contentfiles-element-for-content-files but it seems to only work for non-framework related stuff. We have two different frameworks included. How would you suggest that to be specified?

  10. jeffkl commented on Jul 3, 2023

    @jeffkl

    The Package Folder Structure section gives examples on how to specify target framework-specific assets.

    /contentFiles/{codeLanguage}/{TxM}/{any?}
    

    So if you place files under /contentFiles/any/net45, they would be copied to the output directory for any project that targets a framework compatible with net45.

  11. OsirisTerje commented on Jul 3, 2023

    @OsirisTerje
    Member

    Ok, so to paraphrase, I need to copy the output from either release or debug, whatever I build, into a folder named according to the structure above. The same with any other files I need.
    I can try this, but it doesn't look right.

  12. jeffkl commented on Jul 3, 2023

    @jeffkl

    In order to correctly do this, you'll need to do the following:

    1. Pack the appropriate assets into the correct location inside the .nupkg by telling NuGet which files go where
    2. Have the .nuspec that ends up in the .nupkg have the appropriate information to tell NuGet that files should be copied out of the .nupkg during restore.

    Let me type up a quick sample...

  13. manfred-brands commented on Jul 3, 2023

    @manfred-brands
    Member

    If the files are needed for building, they should be in the build folder, if they are only needed at runtime they should be in the runtimes folder. If both it should be in the lib folder. See https://learn.microsoft.com/en-us/nuget/create-packages/creating-a-package#from-a-convention-based-working-directory

  14. jeffkl commented on Jul 3, 2023

    @jeffkl

    Here's an example:

    Project.csproj:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFrameworks>netstandard2.0;net45</TargetFrameworks>
        
        <!-- Tells NuGet to not include the assemblies built by this project in the normal location of lib\TargetFramework since we want them to be in contentFiles -->
        <IncludeBuildOutput>false</IncludeBuildOutput>
        
        <!-- Tells NuGet to run a custom target when gathering files to be included in the package -->
        <TargetsForTfmSpecificContentInPackage>AddOutputAssembliesToPackage</TargetsForTfmSpecificContentInPackage>
        
        <!--
          NU5100 - The assembly 'contentFiles\any\net45\MyAssembly.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced.
            This is okay since this package only needs to copy its outputs to the consumers directory so that the Visual Studio test extensions can find it
          
          NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below: 
          - Add lib or ref assemblies for the net45 target framework
          - Add lib or ref assemblies for the netstandard2.0 target framework
            NuGet wants to keep the package quality high and doesn't understand that the package layout is correct, it is a utility package whose purpose is to place files in the consumer project's output directory.
         -->
        <NoWarn>NU5100;NU5128</NoWarn>
      </PropertyGroup>
      
      <Target Name="AddOutputAssembliesToPackage"
              DependsOnTargets="GetTargetPath">
        <ItemGroup>
            <!--
                Adds the @(TargetPathWithTargetPlatformMoniker) items which represent the assemblies built by this project
                to the TfmSpecificPackageFile item group which represents the files to be included in the package.
                The files are placed in the package under contentFiles/any/$(TargetFramework) and NuGet is told that they need
                to be copied to the output directory during restore.
            -->
            <TfmSpecificPackageFile Include="%(TargetPathWithTargetPlatformMoniker.Identity)"
                                    PackagePath="contentFiles/any/$(TargetFramework)"
                                    PackageCopyToOutput="true" />
        </ItemGroup>
      </Target>
    </Project>

    When you call Pack, the package has the following contents:

    image

    Also, the .nupsec inside of the .nupkg tells NuGet to copy the files to the output directory:
    image

    The consumer of the package looks like this:

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net7.0</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="My.TestAdapter" Version="1.0.0" />
      </ItemGroup>
    </Project>

    And its output folder now contains My.TestAdapter.dll:

    D:\REPROS\NUGETCONTENTFILES\CONSUMER\BIN
    └───Debug
        └───net7.0
                consumer.deps.json
                consumer.dll
                consumer.pdb
                My.TestAdapter.dll
    

    More importantly, the project.assets.json of the consuming project now shows these assets as being consumed:

    {
      "version": 3,
      "targets": {
        "net7.0": {
          "My.TestAdapter/1.0.0": {
            "type": "package",
            "contentFiles": {
              "contentFiles/any/netstandard2.0/My.TestAdapter.dll": {
                "buildAction": "None",
                "codeLanguage": "any",
                "copyToOutput": true,
                "outputPath": "My.TestAdapter.dll"
              }
            }
          }
        }
      },
      "libraries": {
        "My.TestAdapter/1.0.0": {
          "sha512": "U3s2sbNJXQYvARkHkwKzOGlnpgZ+92+JGJ3corKA9HqLMNfoAzC/0DBeeiW9ZyBdUYs52pul2ikJOA83KbxR1Q==",
          "type": "package",
          "path": "my.testadapter/1.0.0",
          "files": [
            ".nupkg.metadata",
            "contentFiles/any/net45/My.TestAdapter.dll",
            "contentFiles/any/netstandard2.0/My.TestAdapter.dll",
            "my.testadapter.1.0.0.nupkg.sha512",
            "my.testadapter.nuspec"
          ]
        }
      }
    }
  15. 24 remaining items

  16. jeffkl commented on Jul 3, 2023

    @jeffkl

    @jeffkl This is exactly what the runtimes folder is for

    That is not correct. A "normal" NuGet package is used to pass around a class library that your code can consume. For example, I have a package that contains a class library for logging. I name my package Jeff.Loging and inside I have an Jeff.Logging.dll which contains the implementation of my logging framework.

    lib\net45\Jeff.Logging.dll

    Now consider another project is consuming the package. At restore time, NuGet pulls down the package and unzips it, then passes the path of Jeff.Logging.dll to the C# compiler for type checking to ensure that the consumer is only calling the available APIs in the logging framework. The .NET SDK also copies Jeff.Logging.dll to the output directory so that the .NET runtime will be able to locate it, load it, and allow my consumer EXE to call into it.

    There are also "reference" assemblies, which are created and compilation time with dynamically generated code which represents only the public facing APIs. Reference assemblies can help reduce rebuilds for large repositories since the .NET SDK is only taking into account if a public API changed before rebuilding the closure of projects. These assemblies can be included in a package as well in the ref folder.

    lib\net45\Jeff.Logging.dll
    ref\net45\Jeff.Logging.dll

    Now when a consumer uses my package, the ref\net45\Jeff.Logging.dll is passed to the compiler since it contains all of the public facing API and is enough for all of the type checking needed. But the .NET SDK then copies the lib\net45\Jeff.Logging.dll to the output directory because this assembly actually contains my implementation.

    Now consider I want to have different implementations for runtime identifiers (RIDs), for example Linux vs Windows. I would compile my assembly once for each RID and include the RID-specific implementations in my package with these folders:

    runtimes\win10-x64\net45\Jeff.Logging.dll
    runtimes\linux-x64\net45\Jeff.Logging.dll
    ref\net45\Jeff.Logging.dll

    This means that during compilation, the ref\net45\Jeff.Logging.dll is passed to the compiler for type checking because it contains all of the public APIs of my library but the .NET SDK will determine what RID my application is targeting and copy the runtime specific implementation of the library. Assemblies under the lib folder can only specify a specific target framework and not a runtime. In this case the assembly in lib or ref is only used during compilation for type checking.

    The NUnit test adapter is needed at runtime, but is not a runtime specific implementation of a test adapter. The code it contains will work for any runtime (operating system). You can include it in the runtimes folder and it will end up in the output directory, but I still don't think the NUnit test adapter really fits this model. Instead, it is a plug-in for the Visual Studio Test infrastructure and so we just want NuGet to copy it to the output directory. It is not a reference assembly or anything that my code is actually consuming.

    As mentioned content files is not supported by old style .csproj using package.json files and would require 2 copies.

    project.json was deprecated more than 8 years ago. The telemetry in Visual Studio shows it is not used very much at all. I'm not sure if its worth making the package support this technology if so few people are using it. If they're using such an older deprecated technology, I doubt they're using the latest and greated NUnit test adapter package.

    I tried my example with a "legacy" csproj project and it worked fine with PackageReference but not with packages.config. Content files are not supported in packages.config and a custom build\MyPackage.props to copy stuff to the output directory is the only good solution. You could still include the build\MyPackage.props but disable the logic if the package is using PackageReference. I'm not sure how many people are using the latest and greated NUnit test adapter but are also using packages.config when packages.config is considered legacy.

  17. manfred-brands commented on Jul 4, 2023

    @manfred-brands
    Member

    As said before runtimes is for binaries needed at runtime. They will not be passed as reference for building your project.

    Just like with content files, one can use the any for the runtimes folder to indicate that the files are runtime independent.

  18. OsirisTerje commented on Jul 4, 2023

    @OsirisTerje
    Member

    @jeffkl Is there any problems related to using the runtimes/any as @manfred-brands suggests? To me it looks semantically more correct than using contentfiles. (I understand it is opiniated). If the two has the same end effect, and the runtimes works for legacy projects too, then that seems to be a more simple solution.

  19. added this to the 4.6 milestone on Jul 4, 2023
  20. OsirisTerje commented on Jul 4, 2023

    @OsirisTerje
    Member

    @Stefan75 Can you confirm the enclosed package works for you?
    NUnit3TestAdapter.4.6.0-alpha.3.zip

  21. OsirisTerje commented on Jul 4, 2023

    @OsirisTerje
    Member

    @manfred-brands Tried using runtimes, didn't work.

  22. OsirisTerje commented on Jul 4, 2023

    @OsirisTerje
    Member

    @jeffkl You mentioned a condition for the props file for legacy projects, what would that be?

  23. jeffkl commented on Jul 5, 2023

    @jeffkl

    NuGet adds imports to the project when you install a package that contains build extensions.

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <Import Project="packages\My.TestAdapter.1.0.0\build\My.TestAdapter.props" Condition="Exists('packages\My.TestAdapter.1.0.0\build\My.TestAdapter.props')" />
      <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
      ...
      <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
      <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
        <PropertyGroup>
          <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
        </PropertyGroup>
        <Error Condition="!Exists('packages\My.TestAdapter.1.0.0\build\My.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', 'packages\My.TestAdapter.1.0.0\build\My.TestAdapter.props'))" />
        <Error Condition="!Exists('packages\My.TestAdapter.1.0.0\build\My.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\My.TestAdapter.1.0.0\build\My.TestAdapter.targets'))" />
      </Target>
      <Import Project="packages\My.TestAdapter.1.0.0\build\My.TestAdapter.targets" Condition="Exists('packages\My.TestAdapter.1.0.0\build\My.TestAdapter.targets')" />
    </Project>

    I guess the best thing to condition on is the presence of a packages.config file. Legacy C# projects only really support targeting .NET Framework so you'll just want to add <Content /> items for the .NET Framework assembly.

    I tried this and it worked:

    <Project>
      <ItemGroup Condition="Exists('$(MSBuildProjectDirectory)\packages.config')">
        <Content Include="$(MSBuildThisFileDirectory)..\contentFiles\any\net45\*.dll">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
          <Visible>false</Visible>
        </Content>
      </ItemGroup>
    </Project>

    image

    The build logic will always be imported regardless of the target framework of the project. You could go a step further and put the .props/.targets file in build\net45 but the condition is technically filtering out the <Content /> item if the project isn't using packages.config.

    Let me know if you want me to create a sample you can interact with.

  24. OsirisTerje commented on Jul 6, 2023

    @OsirisTerje
    Member

    Thanks @jeffkl ! I'll try that suggestion!

    Another thing I noticed is that it seems VIsual Studio show these content files at a projects root level, although they are not present there as files.
    image

  25. jeffkl commented on Jul 6, 2023

    @jeffkl

    Sorry this looks like the current design by NuGet at the moment. It generates a <None /> item but doesn't set Visible="false".

    You can work around this in your .targets with an Update gesture:

    <ItemGroup>
      <None Update="@(None->WithMetadataValue('NuGetPackageId', 'My.TestAdapter'))" Visible="false" />
    </ItemGroup>

    image

  26. jeffkl commented on Jul 6, 2023

    @jeffkl

    Forgot to link existing issue: NuGet/Home#4856

  27. OsirisTerje commented on Jul 6, 2023

    @OsirisTerje
    Member

    Quite a long lived issue NuGet/Home#4856 . Is it even planned to be done?
    What is the purpose of adding content files that way? It looks more like a bug than a feature.

    However, if I think about a package that is intended to ADD content to a project, it may make sense. That is also why I feel that contentfiles is not quite right for a package containing non-content like this.

    I'll try the updategesture too, it might be the solution to that issue. And, since the adapter rarely (if ever) is added into anything but the root test projects and not passed around in libraries, it should work.

  28. removed this from the 4.6 milestone on Jul 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions