Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error CS9137 Occurred When Building a MAUI Project Targetting to Windows #9785

Open
SloneWang opened this issue Feb 24, 2024 · 8 comments · May be fixed by #9786
Open

Error CS9137 Occurred When Building a MAUI Project Targetting to Windows #9785

SloneWang opened this issue Feb 24, 2024 · 8 comments · May be fixed by #9786
Assignees
Labels

Comments

@SloneWang
Copy link

SloneWang commented Feb 24, 2024

Description

Configuration Binding Source Generator is introduced by .NET8.0, and it works well in my web projects. I attempt to use this generator in my MAUI Blazor App by modifying the .csproj file like:

<Project Sdk="Microsoft.NET.Sdk.Razor">
    <PropertyGroup>
        ...
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
        ...
    </PropertyGroup>
    ...
</Project>

It does function when targetting to net8.0-android, net8.0-ios, and net8.0-maccatalyst . However, a cs9137 error occurrs when set targetting to net8.0-windows10.0.19041.0. I followed the message and added <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces> to project file, that error still occurrs.

Reproduction Steps

Add codes below in PropertyGroup:

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces>

Here is my test project TestMauiBlazor.zip.

Expected behavior

The Configuration Binding Source Generator will generate codes, and the compiler will compile the codes successfully.

Actual behavior

The Configuration Binding Source Generator does generate codes. But the compiler throws a CS9137 error and messages " The 'interceptors' experimental feature is not enabled. Add <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces> to your project. "

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK Version: 8.0.201
Operating System: Windows 10.0.22631 x64
maui-windows Version:  8.0.6/8.0.100

Other information

No response

@ghost ghost added the untriaged label Feb 24, 2024
@ghost
Copy link

ghost commented Feb 24, 2024

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Configuration Binding Source Generator is introduced by .NET8.0, and it works well in my web projects. I attempt to use this generator in my MAUI Blazor App by modifying the .csproj file like:

<Project Sdk="Microsoft.NET.Sdk.Razor">
    <PropertyGroup>
        ...
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
        ...
    </PropertyGroup>
    ...
</Project>

It does function when targetting to net8.0-android, net8.0-ios, and net8.0-maccatalyst . However, a cs9137 error occurrs when set targetting to net8.0-windows10.0.19041.0. I followed the message and added <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces> to project file, that error still occurrs.

Reproduction Steps

Add codes below in PropertyGroup:

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces>

Here is my test project Uploading TestMauiBlazor.zip….

Expected behavior

The Configuration Binding Source Generator will generate codes, and the compiler will compile the codes successfully.

Actual behavior

The Configuration Binding Source Generator does generate codes. But the compiler throws a CS9137 error and messages " The 'interceptors' experimental feature is not enabled. Add <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces> to your project. "

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK Version: 8.0.201
Operating System: Windows 10.0.22631 x64
maui-windows Version:  8.0.6/8.0.100

Other information

No response

Author: SloneWang
Assignees: -
Labels:

untriaged, area-Extensions-Configuration

Milestone: -

@tarekgh tarekgh removed the untriaged label Feb 24, 2024
@ghost
Copy link

ghost commented Feb 24, 2024

Tagging subscribers to 'arch-android': @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

Configuration Binding Source Generator is introduced by .NET8.0, and it works well in my web projects. I attempt to use this generator in my MAUI Blazor App by modifying the .csproj file like:

<Project Sdk="Microsoft.NET.Sdk.Razor">
    <PropertyGroup>
        ...
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
        ...
    </PropertyGroup>
    ...
</Project>

It does function when targetting to net8.0-android, net8.0-ios, and net8.0-maccatalyst . However, a cs9137 error occurrs when set targetting to net8.0-windows10.0.19041.0. I followed the message and added <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces> to project file, that error still occurrs.

Reproduction Steps

Add codes below in PropertyGroup:

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces>

Here is my test project TestMauiBlazor.zip.

Expected behavior

The Configuration Binding Source Generator will generate codes, and the compiler will compile the codes successfully.

Actual behavior

The Configuration Binding Source Generator does generate codes. But the compiler throws a CS9137 error and messages " The 'interceptors' experimental feature is not enabled. Add <InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces> to your project. "

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK Version: 8.0.201
Operating System: Windows 10.0.22631 x64
maui-windows Version:  8.0.6/8.0.100

Other information

No response

Author: SloneWang
Assignees: -
Labels:

os-android, os-ios, area-Extensions-Configuration, os-maccatalyst

Milestone: -

@tarekgh
Copy link
Member

tarekgh commented Feb 24, 2024

@SloneWang looking at the doc, it says

In order to use interceptors, you must set the <Features>InterceptorsPreview<Features> element in your project file. Without this flag, interceptors are disabled, even when other C# 12 features are enabled.

Could you please try adding the Features element to you project and try again. Also, make sure C# 12 compiler version is used. <LangVersion>12</LangVersion>

@ghost
Copy link

ghost commented Feb 24, 2024

This issue has been marked needs-author-action and may be missing some important information.

@SloneWang
Copy link
Author

文档,它说

In order to use interceptors, you must set the <Features>InterceptorsPreview<Features> element in your project file. Without this flag, interceptors are disabled, even when other C# 12 features are enabled.

您能否尝试将该元素添加到您的项目中,然后重试。此外,请确保使用 C# 12 编译器版本。Features``<LangVersion>12</LangVersion>

I tried to add

<Features>InterceptorsPreview</Features>
<LangVersion>12.0</LangVersion>

in PropertyGroup, but it didn't work and the error still occurred.

@tarekgh
Copy link
Member

tarekgh commented Feb 25, 2024

CC @jaredpar @eiriktsarpalis @ericstj any thoughts?

@ericstj
Copy link
Member

ericstj commented Feb 26, 2024

Looks to me like the failure is occurring in the XamlPreCompile target. That target is missing the InterceptorsPreviewNamespaces and Features parameters to CSC. Bug is here;

<Csc Condition=" '%(_CoreCompileResourceInputs.WithCulture)' != 'true' "
AdditionalLibPaths="$(AdditionalLibPaths)"
AddModules="@(AddModules)"
AdditionalFiles="@(AdditionalFiles)"
AllowUnsafeBlocks="$(AllowUnsafeBlocks)"
AnalyzerConfigFiles="@(EditorConfigFiles)"
Analyzers="@(Analyzer)"
ApplicationConfiguration="$(AppConfigForCompiler)"
BaseAddress="$(BaseAddress)"
CheckForOverflowUnderflow="$(CheckForOverflowUnderflow)"
CodeAnalysisRuleSet="$(ResolvedCodeAnalysisRuleSet)"
CodePage="$(CodePage)"
DebugType="$(DebugType)"
DefineConstants="$(DefineConstants)"
DelaySign="$(DelaySign)"
DisabledWarnings="$(NoWarn)"
DocumentationFile="@(DocFileItem)"
EmitDebugInformation="$(DebugSymbols)"
EnvironmentVariables="$(CscEnvironment)"
ErrorEndLocation="$(ErrorEndLocation)"
ErrorLog="$(ErrorLog)"
ErrorReport="$(ErrorReport)"
FileAlignment="$(FileAlignment)"
GenerateFullPaths="$(GenerateFullPaths)"
HighEntropyVA="$(HighEntropyVA)"
KeyContainer="$(KeyContainerName)"
KeyFile="$(KeyOriginatorFile)"
LangVersion="$(LangVersion)"
LinkResources="@(LinkResource)"
MainEntryPoint="$(StartupObject)"
ModuleAssemblyName="$(ModuleAssemblyName)"
NoConfig="true"
NoLogo="$(NoLogo)"
NoStandardLib="$(NoCompilerStandardLib)"
NoWin32Manifest="$(NoWin32Manifest)"
Nullable="$(Nullable)"
Optimize="$(Optimize)"
OutputAssembly="@(XamlIntermediateAssembly)"
PdbFile="$(PdbFile)"
Platform="$(PlatformTarget)"
Prefer32Bit="$(Prefer32Bit)"
PreferredUILang="$(PreferredUILang)"
References="@(ReferencePath)"
ReportAnalyzer="$(ReportAnalyzer)"
Resources="@(_CoreCompileResourceInputs);@(CompiledLicenseFile)"
ResponseFiles="$(CompilerResponseFile)"
SkipAnalyzers="true"
Sources="@(Compile)"
SubsystemVersion="$(SubsystemVersion)"
TargetType="$(OutputType)"
ToolExe="$(CscToolExe)"
ToolPath="$(CscToolPath)"
TreatWarningsAsErrors="$(TreatWarningsAsErrors)"
UseHostCompilerIfAvailable="$(UseHostCompilerIfAvailable)"
UseSharedCompilation="$(UseSharedCompilation)"
Utf8Output="$(Utf8Output)"
VsSessionGuid="$(VsSessionGuid)"
WarningLevel="$(WarningLevel)"
WarningsAsErrors="$(WarningsAsErrors)"
WarningsNotAsErrors="$(WarningsNotAsErrors)"
Win32Icon="$(ApplicationIcon)"
Win32Manifest="$(Win32Manifest)"
Win32Resource="$(Win32Resource)"
/>

To workaround this, I was able to do the following. Not sure if there's a better way.

In the project file set:

<CompilerResponseFile>$(CompilerResponseFile);WorkaroundXamlPreCompilePreviewFeatures.rsp</CompilerResponseFile>

Include a file next to the project named WorkaroundXamlPreCompilePreviewFeatures.rsp with the content:

/features:"InterceptorsPreviewNamespaces=Microsoft.Extensions.Configuration.Binder.SourceGeneration"

I think the bug here is that XamlPreCompile is missing support for new parameters that were added to CSC. Moving this to MSBuild repo where that target is defined.

@ericstj ericstj transferred this issue from dotnet/runtime Feb 26, 2024
@rainersigwald
Copy link
Member

@ericstj nailed it. And a nice workaround!

The full diff on that task invocation is now

diff --git a/src/Tasks/Microsoft.CSharp.CurrentVersion.targets b/src/Tasks/Microsoft.CSharp.CurrentVersion.targets
index 772d1bbcf8e..f2ff137f100 100644
--- a/src/Tasks/Microsoft.CSharp.CurrentVersion.targets
+++ b/src/Tasks/Microsoft.CSharp.CurrentVersion.targets
@@ -244,21 +244,29 @@ Copyright (C) Microsoft Corporation. All rights reserved.
          ApplicationConfiguration="$(AppConfigForCompiler)"
          BaseAddress="$(BaseAddress)"
          CheckForOverflowUnderflow="$(CheckForOverflowUnderflow)"
+         ChecksumAlgorithm="$(ChecksumAlgorithm)"
          CodeAnalysisRuleSet="$(ResolvedCodeAnalysisRuleSet)"
          CodePage="$(CodePage)"
          DebugType="$(DebugType)"
          DefineConstants="$(DefineConstants)"
          DelaySign="$(DelaySign)"
          DisabledWarnings="$(NoWarn)"
+         DisableSdkPath="$(DisableSdkPath)"
          DocumentationFile="@(DocFileItem)"
+         EmbedAllSources="$(EmbedAllSources)"
+         EmbeddedFiles="@(EmbeddedFiles)"
          EmitDebugInformation="$(DebugSymbols)"
          EnvironmentVariables="$(CscEnvironment)"
          ErrorEndLocation="$(ErrorEndLocation)"
          ErrorLog="$(ErrorLog)"
          ErrorReport="$(ErrorReport)"
+         Features="$(Features)"
+         InterceptorsPreviewNamespaces="$(InterceptorsPreviewNamespaces)"
          FileAlignment="$(FileAlignment)"
+         GeneratedFilesOutputPath="$(CompilerGeneratedFilesOutputPath)"
          GenerateFullPaths="$(GenerateFullPaths)"
          HighEntropyVA="$(HighEntropyVA)"
+         Instrument="$(Instrument)"
          KeyContainer="$(KeyContainerName)"
          KeyFile="$(KeyOriginatorFile)"
          LangVersion="$(LangVersion)"
@@ -271,19 +279,30 @@ Copyright (C) Microsoft Corporation. All rights reserved.
          NoWin32Manifest="$(NoWin32Manifest)"
          Nullable="$(Nullable)"
          Optimize="$(Optimize)"
-            OutputAssembly="@(XamlIntermediateAssembly)"
+         Deterministic="$(Deterministic)"
+         PublicSign="$(PublicSign)"
+         OutputAssembly="@(IntermediateAssembly)"
+         OutputRefAssembly="@(IntermediateRefAssembly)"
          PdbFile="$(PdbFile)"
          Platform="$(PlatformTarget)"
          Prefer32Bit="$(Prefer32Bit)"
          PreferredUILang="$(PreferredUILang)"
-            References="@(ReferencePath)"
+         ProjectName="$(MSBuildProjectName)"
+         ProvideCommandLineArgs="$(ProvideCommandLineArgs)"
+         References="@(ReferencePathWithRefAssemblies)"
+         RefOnly="$(ProduceOnlyReferenceAssembly)"
          ReportAnalyzer="$(ReportAnalyzer)"
+         ReportIVTs="$(ReportIVTs)"
          Resources="@(_CoreCompileResourceInputs);@(CompiledLicenseFile)"
          ResponseFiles="$(CompilerResponseFile)"
-            SkipAnalyzers="true"
+         RuntimeMetadataVersion="$(RuntimeMetadataVersion)"
+         SharedCompilationId="$(SharedCompilationId)"
+         SkipAnalyzers="$(_SkipAnalyzers)"
+         SkipCompilerExecution="$(SkipCompilerExecution)"
          Sources="@(Compile)"
          SubsystemVersion="$(SubsystemVersion)"
          TargetType="$(OutputType)"
+         TargetFramework="$(TargetFramework)"
          ToolExe="$(CscToolExe)"
          ToolPath="$(CscToolPath)"
          TreatWarningsAsErrors="$(TreatWarningsAsErrors)"
@@ -297,6 +316,8 @@ Copyright (C) Microsoft Corporation. All rights reserved.
          Win32Icon="$(ApplicationIcon)"
          Win32Manifest="$(Win32Manifest)"
          Win32Resource="$(Win32Resource)"
+         PathMap="$(PathMap)"
+         SourceLink="$(SourceLink)"
          />
 
 <!-- Only Applicable to the regular CoreCompile:

Some of those diffs are intentional (SkipAnalyzers) and some look right, but I'm not sure.

@jaredpar heads up I'm going to send a PR to sync these two targets again and will need compiler team review please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants