C
C#2y 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]

...
...

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"]
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.
3 Replies
JakenVeina
JakenVeina2y ago
sounds like the source generator isn't getting bundled up in the docker image?
333fred
333fred2y ago
You need to depend on the right version of roslyn Your docker image only has roslyn 4.3. You're depending on 4.5 in your generator Remember, your generator is a plugin. You don't get to chose what version of roslyn calls you If you want to use 4.5, you would need to use a newer version of .NET and the SDK
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server