1

I have a jenkins build that cleans, restores, and then builds a unit test project. This project, along with my others, have recently been migrated to use PackageReference. The clean works, and the restore works. I verified that NUnit exists in %userprofile%/.nuget/packages/. Here are the commands that are in my build script:

msbuild /p:Configuration="Debug" /t:clean OtherProjectName.Test.csproj
msbuild /p:Configuration="Debug" /t:restore OtherProjectName.Test.csproj
msbuild /p:Configuration="Debug" OtherProjectName.Test.csproj

This later results in output that looks something like this:

csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /highentropyva+
     /reference:<one of my references> /reference:<another one> /reference:<and so on>
     /out:obj\Debug\OtherProjectName.Test.dll /subsystemversion:6.00 /target:library /utf8output <a list of source files>

The build for my project fails because it can't find NUnit (see below). I believe it can't find NUnit because the compilation command does not include a "/reference" to the NUnit nuget package.

error CS0246: The type or namespace name 'NUnit' could not be found (are you missing a using directive or an assembly reference?

My other projects do have '/reference' for NuGet packages. So I compared them to see if anything looked significantly different. I did notice that most of my projects include this line at the start:

<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

I thought that might be the culprit, based on my brief reading on what this line does, but when I tried the same steps on my local machine (it has VS2017 and MSbuild 15, just like the jenkins server) I could not replicate the issue.

My .csproj file has the following:

    <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{a guid}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>OtherProjectName.Test</RootNamespace>
    <AssemblyName>OtherProjectName.Test</AssemblyName>
    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
    <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
    <IsCodedUITest>False</IsCodedUITest>
    <TestProjectType>UnitTest</TestProjectType>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <!-- a couple entries like this -->
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="log4net">
      <HintPath>Z:\path\to\log4net.dll</HintPath>
    </Reference>
    <!-- a couple references like this -->
    <Reference Include="System" />
    <Reference Include="System.Configuration" />
    <Reference Include="System.Web.Extensions" />
    <Reference Include="System.Xml" />
    <Reference Include="WindowsBase" />
  </ItemGroup>
  <Choose>
    <When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
      <ItemGroup>
        <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
      </ItemGroup>
    </When>
    <Otherwise />
  </Choose>
  <ItemGroup>
    <!-- all my C# source files -->
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\OtherProjectName\OtherProjectName.csproj">
      <Project>{a guid}</Project>
      <Name>OtherProjectName</Name>
    </ProjectReference>
    <ProjectReference Include="..\OtherProjectName2\OtherProjectName2.csproj">
      <Project>{another guid}</Project>
      <Name>OtherProjectName2</Name>
    </ProjectReference>
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config">
      <SubType>Designer</SubType>
    </None>
    <None Include="App.config.1" />
    <None Include="Unit Test Playlists\Fast.playlist" />
    <!-- all my unit test playlists -->
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="NUnit">
      <Version>3.11.0</Version>
    </PackageReference>
    <PackageReference Include="NUnit.ConsoleRunner">
      <Version>3.9.0</Version>
    </PackageReference>
    <PackageReference Include="NUnit3TestAdapter">
      <Version>3.11.0</Version>
    </PackageReference>
    <!-- other nuget packages -->
  </ItemGroup>
  <Choose>
    <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
      <ItemGroup>
        <Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
        <Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
          <Private>False</Private>
        </Reference>
      </ItemGroup>
    </When>
  </Choose>
  <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>
Chris
  • 3,400
  • 1
  • 27
  • 41
  • 1
    can you please add the error that you are getting? Check the nugget restore step and see if Nunit package is getting restored as well. – Isaiah4110 Feb 11 '19 at 17:53
  • @Isaiah4110 I added both of those things to the first and second paragraphs. – Chris Feb 11 '19 at 21:25
  • 1
    %userprofile%/.nuget/packages/ - is the local nuget store not where your application will be using it. When you do restore, the nuget will first check the local store and if the package is there it will download it to the application folder. So can you see the nUnit within the packages folder within your application? You don't need to add nunit reference during compilation. All that is handled by the package reference. Is this a Dotnet core application? – Isaiah4110 Feb 12 '19 at 15:21
  • No, standard .Net. What you're describing [only applies to `packages.config`, not `PackageReference`](https://learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders). I am using `PackageReference`. My question is about why that reference is not getting added during compilation. – Chris Feb 12 '19 at 17:47
  • yes, you are right about packages.config.. I will check more on this tomorrow and get back. – Isaiah4110 Feb 13 '19 at 01:08
  • 1
    So I created a test Nunit project on my local and recreated your scenario and sure enough it works for me. In the MSBUILD corecompile step (csc.exe that you mentioned above), I do see a reference to the Nunit.framework.dll - you don't have it? Here is the actual reference: /reference:C:\Users\abc\.nuget\packages\nunit\3.11.0\lib\net45\nun it.framework.dll – Isaiah4110 Feb 13 '19 at 16:54
  • Correct, it is missing from the CoreCompile step. I see NuGet entries for my other projects, but no NuGet references for my unit test project, and only on the jenkins build server. It's there locally. – Chris Feb 14 '19 at 16:14
  • 1
    when you say Nuget entries for other projects- are you saying that in your "OtherProjectName.Test.csproj" you have other nuget references (other than Nunit) and they are being added to the /references (in CoreCompile) without any issue? – Isaiah4110 Feb 14 '19 at 16:50
  • 1
    I compared your csproj file with mine and I do see lot of other steps/targets missing as well. I have a target called "EnsureNuGetPackageBuildImports" before prepare for build and I don't see that in your csproj. Did you create your csproj manually by hand? – Isaiah4110 Feb 14 '19 at 17:00
  • I mean "OtherProjectName" has NuGet references that are included as they should be, when compiled with msbuild. I also have other NuGet references in OtherProjectName.Test and they are not being included either, it's not just NUnit. I created the test project through Visual Studio 2015, not manually. I don't see that target in any of my project files. – Chris Feb 14 '19 at 19:53
  • I believe EnsureNuGetPackageBuildImports comes from the packages.config style of NuGet referencing. It was present before I migrated to PackageReference. – Chris Feb 14 '19 at 19:57
  • 1
    okay I did spend lot of time on this, only thing I can think of is that somehow on your build machine the different version (lower) of MSBUILD is being used for building this test project compared to your other projects. So it is not adding the package references properly. – Isaiah4110 Feb 15 '19 at 17:22
  • You know what, it is an older version: 15.1.1012 vs 15.9.20. I can't believe I hadn't noticed. I swear I looked at that earlier and it was the same, but no. I'll update it and see what happens. – Chris Feb 15 '19 at 18:06
  • finally!!! :) :) – Isaiah4110 Feb 15 '19 at 19:06
  • did it work? ??? – Isaiah4110 Feb 16 '19 at 18:23
  • Don't know yet, I can't touch the build server, I have to have my IT people do it. Might take a few days or weeks. If it does, I'll reply here so you can post it as an answer and get credit. – Chris Feb 18 '19 at 01:14
  • Please mark it as the answer, if it fixed the issue. – Isaiah4110 Mar 05 '19 at 22:49
  • My IT guys are slow so I have no idea yet if it's the solution. I won't forget. – Chris Mar 06 '19 at 13:38

1 Answers1

1

Please compare the version of the MSBUILD on your local and the build server. Looks like you might have a lower version of MSBUILD running on the build server that doesn't support packagereferences.

Isaiah4110
  • 9,855
  • 1
  • 40
  • 56
  • 1
    Speak of the devil, they finally got this taken care of this morning, and it did fix the problem. For the record, the version affected was 15.1.1012.6693, and it was corrected somewhere between there and 15.9.20+g88f5fadfbe. Both were part of Visual Studio 2017. – Chris Mar 06 '19 at 13:59