Search code examples
c#.netnuget

dotnet pack seems to use outdated sourcecode, even with a clean git clone


First of all: I know how unbelievable this sounds, but I have tried absolutely everything that came to my mind, so please hear me out.

I am making a .NET 6 C# library and at every release point, I increment the version number in the .csproj file and then run the commands dotnet clean, dotnet build -c Release, and dotnet pack -c Release. The library is on nuget and github. The problem is that when installing the built nuget package, everything is at the point where it was on the very first published version of the library.

I have tried:

  • Cloning the git repo again to get a clean environment
  • Deleting all other files in the environment, except for the main C# project (the .csproj and all .cs files)
  • Removing information from the .csproj file.
  • Deleting any obj and or build folders before running restore, clean, build, and pack
  • Running pack with --no-build after running build -c Release
  • Adding an AssemblyVersion to the .csproj file

When I change the version number to a new version that isn't on nuget already, it works. When I install that package, it is at the point where it should be. Running the command...

rmdir -r .\WeatherAPI-CSharp\bin;
rmdir -r .\WeatherAPI-CSharp\obj;
dotnet restore .\WeatherAPI-CSharp\WeatherAPI-CSharp.csproj;
dotnet clean .\WeatherAPI-CSharp\WeatherAPI-CSharp.csproj;
dotnet build -c Release .\WeatherAPI-CSharp\WeatherAPI-CSharp.csproj;
dotnet pack -c Release --no-build .\WeatherAPI-CSharp\WeatherAPI-CSharp.csproj

...generates an outdated .nupkg when the version is set to 0.4.1. When I then change the version to 0.4.2 and rerun the exact same command, it works. I though that maybe this is because the version already exists on nuget, so I tried disconnecting from the internet to fix it, but that did nothing.

Peeking at the source code in the dll in the nupkg file shows the correct source code, but when installing the file, it seems to fall back to v0.1.0. I install the nupkg in Visual Studio using the Nuget GUI and adding my output directory as a nuget source and disabling the online nuget source.


Solution

  • This seems to have been a problem with cached versions of the package being used, instead of the new one. Deleting all folders in C:/Users/{UserName}/.nuget/{LibraryName}/ fixed Visual Studio installing an outdated version of the same package. This also explains why changing the version number to one that isn't already on nuget caused the files to be built successfully, as I already had all versions cached.

    The real problem seems to have been that dotnet used an outdated build in the Release folder to generate the .nupkg. This could be fixed by running the commands dotnet clean, dotnet build -c Release, and dotnet pack -c Release --no-build.

    Because old versions were alredy cached, rebuilding the package using the above mentioned commands extended the problem, because it installed the cached versions instead of the newly built one (I think). Due to this, I didn't realize that the outdated Release build was the problem.

    So far, this explanation seems to be accurate, but I am keeping the question open until I was able to confirm this.