RuntimeIdentifier ignored in csproj (.Net 8)

Alberto 25 Puntos de reputación
2025-12-01T08:05:23.78+00:00

I have this in my csproj in .NET 8.

<PropertyGroup>
  <IsLinux Condition="$(RuntimeIdentifier.StartsWith('linux'))">true</IsLinux>
</PropertyGroup>
<PropertyGroup>
  <DefineConstants Condition="'$(IsLinux)' == 'true'">$(DefineConstants);IS_LINUX</DefineConstants>
</PropertyGroup>

When running dotnet publish MySolution.sln -c Release -r linux-x64 -f net8.0 --self-contained, to build my solution which contains this csproj, the symbol is not created, even though the following folder structure is created:

Release/net8.0/linux-x64/publish

And the built files can be found 3 times (once in net8.0, once in linux-x64 and once in publish).

If I add a message to check the RuntimeIdentifier, e.g.:

<Target Name="ShowRID" AfterTargets="Build">
  <Message Importance="high" Text="RuntimeIdentifier: '$(RuntimeIdentifier)'" />
</Target>

It looks like there's 2 build passes, and the one of them doesn't have anything defined in RuntimeIdentifier. I suspect this builds the files for the net8.0 folder, and those files are then copied to linux-x64 and publish. The other one does show linux-x64.

If I define this in the csproj, it works:

<RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>

And if I add the message, these time there are 3 passes (no runtime identifier in one of them, linux-64 in the other 2).

Also, when building the specific project (instead of the solution), the build works properly even without the <RuntimeIdentifiers> property in the csproj.

Is there a bug when building a solution?

Tecnologías de desarrolladores | ASP.NET Core | Otros
0 comentarios No hay comentarios

Respuesta aceptada por el autor de la pregunta
Tom Tran (WICLOUD CORPORATION) 5,970 Puntos de reputación Personal externo de Microsoft Moderador
2025-12-01T10:41:06.7166667+00:00

Hi @Alberto ,

Thanks for sharing the detailed breakdown of your setup and observations!

From what you described, I think the root cause might be how MSBuild handles solution-level publishing with RuntimeIdentifiers.

As far as I know when you run:

  • dotnet publish MySolution.sln -c Release -r linux-x64 --self-contained

MSBuild builds each project multiple times. The first pass often has no RuntimeIdentifier set, so your condition:

Condition="'$(RuntimeIdentifier.StartsWith('linux'))'"

doesn’t evaluate as true. That means IsLinux and DefineConstants aren’t applied during that initial compile. Later passes do have RuntimeIdentifier=linux-x64, but by then, the earlier build outputs (without the symbol) are already copied into the publish folder. This explains why you see mixed outputs and why the constant isn’t reliably defined. This is expected behavior for solution-level publishing, it’s not a bug, but just how MSBuild resolves dependencies.

Publishing a single project works as expected because it only runs the RID-aware build once, so your condition fires correctly.


Here are some workarounds you can try out first:

1. Add <RuntimeIdentifiers> in the project file like you did

This forces MSBuild to consider RID-specific builds even when publishing a solution:

<RuntimeIdentifiers>win-x64;linux-x64</RuntimeIdentifiers>

You can check out MSBuild conditions for details on how conditions are evaluated.

2. Publish the project instead of the solution
Use:

dotnet publish MyProject.csproj -c Release -r linux-x64 --self-contained

This avoids the extra “empty RID” build pass. More info in dotnet publish.

3. Move constant logic into a target before compile

Instead of relying on global property evaluation, set it in a target only when RuntimeIdentifier is not empty:

<Target Name="SetLinuxConstant" BeforeTargets="Compile" Condition="'$(RuntimeIdentifier)' != ''">
  <PropertyGroup>
       <DefineConstants>$(DefineConstants);IS_LINUX</DefineConstants>
  </PropertyGroup>

This ensures the constant applies only during RID-aware builds. See MSBuild targets for guidance.


Publishing at solution level can be confusing because of those extra passes. Using project-level publish or forcing RuntimeIdentifiers usually clears this up.

Hope this helps! If you have any more questions, please feel free to ask below. I'll be happy to help!

¿Le ha resultado útil esta respuesta?

0 comentarios No hay comentarios

1 respuesta adicional

Ordenar por: Más antiguo
  1. Alberto 25 Puntos de reputación
    2025-12-02T07:50:47.47+00:00

    Thanks for your reply!

    It might not be a bug, but it's definitely unexpected to me as the -r argument is accepted without warning.

    Unfortunately building a single csproj in this case is less convenient as it's a solution with multiple projects and I'd need to handle dependencies manually.

    So I'll keep the runtime identifiers in the csproj.

    Would it be possible to update the dotnet publish docs so the behaviour regarding solutions vs project is more clear?

    ¿Le ha resultado útil esta respuesta?


Su respuesta

Las respuestas pueden ser marcadas como "Aceptadas" por el autor de la pregunta y "Recomendadas" por los moderadores, lo que ayuda a los usuarios a saber que la respuesta ha resuelto el problema del autor.