C#C
C#3y ago
Alexicon

❔ Docker dotnet msbuild fails when using source generator reference

I have a solution with two projects. An aspnet api 'MyApiProject' and a regular class library 'MyLibraryProject'. The library project references a source generator NuGet package 'MyGenerator' I created in a separate solution.

When I build and run the solution in visual studio everything works fine but when I build it through docker it is failing because the class file that is generated "could not be found".

Relevant extract from the docker output:
...

MSBuild version 17.3.2+561848881 for .NET
  Determining projects to restore...
  Restored /src/MyLibraryProject/MyLibraryProject.csproj (in 435 ms).
  Restored /src/MyApiProject/MyApiProject.csproj (in 205 ms).
CSC : warning CS9057: The analyzer assembly '/root/.nuget/packages/MyGenerator/1.20230412.1/analyzers/dotnet/cs/MyGenerator.dll' references version '4.5.0.0' of the compiler, which is newer than the currently running version '4.3.0.0'. [/src/MyLibraryProject/MyLibraryProject.csproj]
/src/MyLibraryProject/SomeService.cs(113,21): error CS0246: The type or namespace name 'MyGeneratedClass' could not be found (are you missing a using directive or an assembly reference?) [/src/MyLibraryProject/MyLibraryProject.csproj]

...


Relevant parts of my docker file looks like this:
FROM mcr.microsoft.com/dotnet/aspnet:6.0-focal AS base
RUN apt-get update

...

WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:6.0-focal AS build
WORKDIR /src
COPY ["MyApiProject/MyApiProject.csproj", "MyApiProject/"]
COPY ["MyLibraryProject/MyLibraryProject.csproj", "MyLibraryProject/"]
COPY ["NuGet.config", ""]
RUN dotnet restore --configfile NuGet.config "MyApiProject/MyApiProject.csproj"

COPY . .
WORKDIR "/src/MyApiProject"
RUN dotnet build -c Release -o /app
FROM build AS publish
RUN dotnet publish -c Release -o /app

FROM base AS final
WORKDIR /app

...

COPY --from=publish /app .
ENTRYPOINT ["dotnet", "MyApiProject.dll"]


All of this makes me think that its not running the source code generator during the build.
Was this page helpful?