0

I've got this weird issue adding a Project Reference via a relative path in csproj file &/or the DLL directly. It keeps reverting back to the NuGet DLL in my Nuget folder: C:\Users\jeremy.nuget\packages\

Steps to cause problem:

  1. Delete a Base NuGet Project Ref
  2. Add a reference to the Base DLL of a Project in a different Solution.
  3. Check the Path to the referenced DLL's properties and it still points to the NuGet not the DEBUG Project Ref.

I've turned off the NuGet Source and Visual Studio still uses the NuGet.

If I rename the NuGet DLL and then reference by Project specifying the Debug folder DLL it then fails! So what is causing it to look up the NuGet as a priority over the Project?

When I clear the NuGet cache it gives me an error saying not all packages could be deleted, however when all my packages are gone this doesn't fix up the problem, it actually creates more issues.

I should note there's another project in this solution that also references Base.DLL but for the life of me I can't change it to not use a NuGet Reference either, this really feels like a bug.

Problem is it overrides my Project Reference. And yes I've tried copying over the Debug folder DLL over the NuGet one locally but that still doesn't get me the pdb and symbolic debug info to debug into Base.

Any advice, suggestions, feedback much appreciated?

Visual Studio 2022 v17.5.4

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Is the local copy different from the nuget one? Or can you explain why you want to do this? – PMF Apr 21 '23 at 10:00
  • Yes its different Nuget version to the Project Ref version. The aim is to be able to debug into the Base with the DEBUG Project Ref. – Jeremy Thompson Apr 21 '23 at 10:55

2 Answers2

2

So I found what was happening, the project.assets.json in the Obj folder contains all the NuGet Packages and removing the Base reference in this file solved the problem of NuGet overriding Debug Project References.

I settled on deleting all the Artifactory NuGets and the standard approach of conditionally loading DLLs:

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <ReferencePath>..\..\..\svc-base\src\bin\Debug\net6.0\</ReferencePath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  </PropertyGroup>

  <ItemGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <Reference Include="Company.MS.Base">
      <HintPath>$(ReferencePath)Company.MS.Base.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <PackageReference Include="Company.MS.Base" Version="2.1.12" />
  </ItemGroup>

TRICK: Say you have another project that also references the Base you can use this trick with your own tag for the ReferencePath, eg: ReferenceInfraHttpPath:

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <ReferenceInfraHttpPath>..\..\..\svc-infrastructure\src\bin\Debug\net6.0\</ReferenceInfraHttpPath>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  </PropertyGroup>

  <ItemGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <Reference Include="Company.MS.Infrastructure">
      <HintPath>$(ReferenceInfraHttpPath)Company.MS.Infrastructure.dll</HintPath>
    </Reference>
  </ItemGroup>
  <ItemGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <PackageReference Include="Company.MS.Infrastructure" Version="2.1.1" />
  </ItemGroup>
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Hi Jeremy, since you found the right way to solve this issue, you can [accept it as the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235), this will help others when they search for the similar issue in stackoverflow. :) – Bowman Zhu-MSFT Apr 28 '23 at 06:07
  • 1
    @BowmanZhu-MSFT hey you can look me up in Visual KB, I should still be in there. Lets let the voting do its work. I had "[Nuget Switcher](https://github.com/RicoSuter/NuGetReferenceSwitcher)" working perfect until I did a BuildKite build and the Choose in the project file didn't work. So while this get me around the problem and allows me Debug Base &/or any Shared Library Project references it doesn't fully solve the problem of switching. Give me some time with this one, thanks. – Jeremy Thompson Apr 28 '23 at 08:20
1

What I do to debug a nuget-library locally is the following:

  • Make sure my local version number is different from the last version on the nuget server.
  • Build the nuget file locally (using dotnet pack or the equivalent from whatever build system you use) and place it in some well-known folder, such as "artifacts".
  • Update the version reference in the consuming project.
  • In the consuming project/solution, open the Nuget explorer and add the "artifacts" folder as a local nuget source.
  • Build the consuming project. You should now be able to step into the library code (eventually, you'll need to disable the "just my code" option under Tools->Options->Debugging)

Of course, you can reference just a dll, but since there can be only one dll with the same name in the output folder, all references to the same dll (or their corresponding nuget) must be equivalent. Also, to avoid pitfalls in this case, you need to use the <HintPath> element in the project reference, or VS will just pick the first .dll that matches the given name:

    <Reference Include="..\Cyara.MS.base\bin\Debug\Cyara.MS.Base.dll">
      <Name>CyAra.MS</Name>
      <HintPath>..\Cyara.MS.base\bin\Debug\Cyara.MS.Base.dll</HintPath>
    </Reference>

This might work, but from my experience this should not be used anymore because it is prone to all kinds of problems (e.g. what if you're building Release?)

PMF
  • 14,535
  • 3
  • 23
  • 49
  • This is what I've done with HintPath sorry I didn't mention that. And that does work in Solution with projects having a single ref to Base.DLL, but it doesn't work in a Solution with Projects that also have Ref's to Base.DLL. And I'm starting to see this weird reference issue is a symptom of the root cause - where if you have a sub/nested Nuget ref and then try to add a Project Ref then it will take the sub/nested one as a priority. – Jeremy Thompson Apr 21 '23 at 11:53
  • Yea, I guess you need to make sure all references to the same nuget/dll are equivalent (e.g. only project references, or only dll references or only nuget references). If the dlls are in the same solution, obviously you use project references, otherwise I favor nuget references, because they work most consistently. – PMF Apr 21 '23 at 12:03