Build errors with multi-targeting Razor Class Library projects that include an MSBuild target depending on the CoreBuild target. (.NET SDK 9 Preview 7)
Describe the bug
I need to include the output files of my source generator to static web assets. So, I appended the following MSBuild target in the Blazor project files to follow @javiercn's advice.
...
<Target Name="..."
DependsOnTargets="CoreCompile"
BeforeTargets="AssignTargetPaths;ResolveStaticWebAssetsConfiguration">
<ItemGroup>
<!-- Include a source generator's output files here. -->
</ItemGroup>
</Target>
...
(See also: https://github.com/jsakamoto/Toolbelt.Blazor.I18nText/issues/63)
That worked fine for almost all kinds of Blazor projects. However, it failed with the following error messages when I tried to build a multi-targeting Razor Class Library project with the above MSBuild target, with the .NET SDK 9 Preview 7.
error CS0400: The type or namespace name 'System' could not be found in the global namespace (are you missing an assembly reference?)
error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
error CS0518: Predefined type 'System.String' is not defined or imported
...
Those errors were triggered when I just used the <TargetFrameworks> property in a Razor Class Library project file. It happened even when I specified only one target framework in that <TargetFrameworks> property, like below.
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
...
The build process succeeded when I rewrote the project file above to use the <TargetFramework> property. It also looks like there is no dependency on what the specified framework is. I've verified that this error happened on various of the "TargetFramworks" MSBuild property values, which are "net6.0", "net8.0", and "net9.0".
Again, the above MSBuild target worked fine on all kinds of projects, including multi-targeting projects, as long as they were Blazor Application projects, not Razor Class Library projects.
To Reproduce
Build the project I attached to this issue below with the .NET SDK 9 Preview 7. Then you will see the error messages I explained above.
Exceptions (if any)
The build process must be successful.
Further technical details
The output of the dotnet --info
dotnet --info
.NET SDK:
Version: 9.0.100-preview.7.24407.12
Commit: d672b8a045
Workload version: 9.0.100-manifests.baed1e37
MSBuild version: 17.12.0-preview-24374-02+48e81c6f1
Runtime Environment:
OS Name: Windows
OS Version: 10.0.22631
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.100-preview.7.24407.12\
.NET workloads installed:
Configured to use loose manifests when installing new manifests.
[android]
Installation Source: SDK 9.0.100-preview.7, VS 17.12.35209.166, VS 17.11.35208.52
Manifest Version: 35.0.0-preview.7.41/9.0.100-preview.7
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100-preview.7\microsoft.net.sdk.android\35.0.0-preview.7.41\WorkloadManifest.json
Install Type: Msi
[aspire]
Installation Source: SDK 9.0.100-preview.7, VS 17.11.35208.52
Manifest Version: 8.1.0/8.0.100
Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.1.0\WorkloadManifest.json
Install Type: FileBased
[ios]
Installation Source: SDK 9.0.100-preview.7, VS 17.12.35209.166, VS 17.11.35208.52
Manifest Version: 17.5.9231-net9-p7/9.0.100-preview.7
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100-preview.7\microsoft.net.sdk.ios\17.5.9231-net9-p7\WorkloadManifest.json
Install Type: Msi
[maccatalyst]
Installation Source: SDK 9.0.100-preview.7, VS 17.12.35209.166, VS 17.11.35208.52
Manifest Version: 17.5.9231-net9-p7/9.0.100-preview.7
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100-preview.7\microsoft.net.sdk.maccatalyst\17.5.9231-net9-p7\WorkloadManifest.json
Install Type: Msi
[maui-windows]
Installation Source: SDK 9.0.100-preview.7, VS 17.12.35209.166, VS 17.11.35208.52
Manifest Version: 9.0.0-preview.7.24407.4/9.0.100-preview.7
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100-preview.7\microsoft.net.sdk.maui\9.0.0-preview.7.24407.4\WorkloadManifest.json
Install Type: Msi
[wasm-tools]
Installation Source: SDK 9.0.100-preview.7, VS 17.12.35209.166
Manifest Version: 9.0.0-preview.7.24405.7/9.0.100-preview.7
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100-preview.7\microsoft.net.workload.mono.toolchain.current\9.0.0-preview.7.24405.7\WorkloadManifest.json
Install Type: Msi
[wasm-tools-net6]
Installation Source: SDK 9.0.100-preview.7, VS 17.12.35209.166
Manifest Version: 9.0.0-preview.7.24405.7/9.0.100-preview.7
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100-preview.7\microsoft.net.workload.mono.toolchain.net6\9.0.0-preview.7.24405.7\WorkloadManifest.json
Install Type: Msi
[wasm-tools-net7]
Installation Source: SDK 9.0.100-preview.7, VS 17.12.35209.166
Manifest Version: 9.0.0-preview.7.24405.7/9.0.100-preview.7
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100-preview.7\microsoft.net.workload.mono.toolchain.net7\9.0.0-preview.7.24405.7\WorkloadManifest.json
Install Type: Msi
[wasm-tools-net8]
Installation Source: SDK 9.0.100-preview.7
Manifest Version: 9.0.0-preview.7.24405.7/9.0.100-preview.7
Manifest Path: C:\Program Files\dotnet\sdk-manifests\9.0.100-preview.7\microsoft.net.workload.mono.toolchain.net8\9.0.0-preview.7.24405.7\WorkloadManifest.json
Install Type: Msi
Host:
Version: 9.0.0-preview.7.24405.7
Architecture: x64
Commit: static
.NET SDKs installed:
3.1.426 [C:\Program Files\dotnet\sdk]
5.0.408 [C:\Program Files\dotnet\sdk]
6.0.425 [C:\Program Files\dotnet\sdk]
7.0.410 [C:\Program Files\dotnet\sdk]
8.0.400 [C:\Program Files\dotnet\sdk]
9.0.100-preview.7.24407.12 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.0-preview.7.24406.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.0-preview.7.24405.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.33 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.0-preview.7.24405.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
C:\Projects\Feedbacks\Blazor\Net9andStaticWebAssets\RCL\global.json
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download