Skip to content

Commit 70f4dd8

Browse files
[xabt] suppress XA0101 for Razor class libraries (#10350)
Context: https://github.com/dotnet/sdk/blob/1ed2f8e9751753a1d249d129884a669363c1b832/src/StaticWebAssetsSdk/Sdk/Sdk.StaticWebAssets.StaticAssets.ProjectSystem.props#L30-L31 Fixes: https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2533207 1. Create a `dotnet new maui-blazor-web` project. 2. Open the `*.Shared.csproj`, the Razor class library project. 3. Change the `$(TargetFramework)` from `net9.0` to `net9.0-android`. 4. Build the project. Get the warnings: hellomauiblazor.Shared net9.0-android succeeded with 11 warning(s) (0.4s) → hellomauiblazor.Shared\bin\Debug\net9.0-android\hellomauiblazor.Shared.dll wwwroot\app.css : warning XA0101: @(Content) build action is not supported wwwroot\bootstrap\bootstrap.min.css : warning XA0101: @(Content) build action is not supported wwwroot\bootstrap\bootstrap.min.css.map : warning XA0101: @(Content) build action is not supported wwwroot\favicon.png : warning XA0101: @(Content) build action is not supported Layout\MainLayout.razor : warning XA0101: @(Content) build action is not supported Layout\NavMenu.razor : warning XA0101: @(Content) build action is not supported Pages\Counter.razor : warning XA0101: @(Content) build action is not supported Pages\Home.razor : warning XA0101: @(Content) build action is not supported Pages\Weather.razor : warning XA0101: @(Content) build action is not supported Routes.razor : warning XA0101: @(Content) build action is not supported _Imports.razor : warning XA0101: @(Content) build action is not supported Reviewing the code in the .NET SDK for Razor projects, they include most of these files such as: <!-- Publish everything under wwwroot, all JSON files, all config files and all Razor files --> <Content Include="wwwroot\**" ExcludeFromSingleFile="true" CopyToPublishDirectory="PreserveNewest" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" /> `%(ExcludeFromSingleFile)` looks like a useful metadata here, as Android is basically "single file mode" no matter what. We set `$(EnableSingleFileAnalyzers)=true` by default, so we consider mobile a "single file" platform. Let's suppress the `XA0101` warning for files that have `%(ExcludeFromSingleFile)` metadata, as this seems actually appropriate and solves the warnings in Razor class libraries.
1 parent 0f46d50 commit 70f4dd8

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildWithLibraryTests.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,55 @@ public void CheckContentBuildAction ()
769769
}
770770
}
771771

772+
[Test]
773+
public void ContentBuildActionForRazor ()
774+
{
775+
var lib = new XamarinAndroidLibraryProject {
776+
Sdk = "Microsoft.NET.Sdk.Razor",
777+
EnableDefaultItems = true,
778+
PackageReferences = {
779+
new Package { Id = "Microsoft.AspNetCore.Components.Web", Version = "9.0.0" },
780+
},
781+
Sources = {
782+
new BuildItem.Content ("_Imports.razor") {
783+
TextContent = () => """
784+
@using System.Net.Http
785+
@using System.Net.Http.Json
786+
@using Microsoft.AspNetCore.Components.Forms
787+
@using Microsoft.AspNetCore.Components.Routing
788+
@using Microsoft.AspNetCore.Components.Web
789+
@using static Microsoft.AspNetCore.Components.Web.RenderMode
790+
@using Microsoft.AspNetCore.Components.Web.Virtualization
791+
@using Microsoft.JSInterop
792+
""",
793+
},
794+
new BuildItem.Content ("Pages/Hello.razor") {
795+
TextContent = () => """
796+
@page "/counter"
797+
<PageTitle>Counter</PageTitle>
798+
<h1>Counter</h1>
799+
<p role="status">Current count: @currentCount</p>
800+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
801+
@code {
802+
private int currentCount = 0;
803+
804+
private void IncrementCount()
805+
{
806+
currentCount++;
807+
}
808+
}
809+
""",
810+
},
811+
new BuildItem.Content ("wwwroot/favicon.png") {
812+
BinaryContent = () => XamarinAndroidApplicationProject.icon_binary_mdpi,
813+
}
814+
}
815+
};
816+
using var b = CreateDllBuilder ();
817+
Assert.IsTrue (b.Build (lib), "library should have built successfully");
818+
b.AssertHasNoWarnings ();
819+
}
820+
772821
// Combination of class libraries that triggered the problem:
773822
// error APT2144: invalid file path 'obj/Release/net8.0-android/lp/86.stamp'.
774823
[Test]

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ because xbuild doesn't support framework reference assemblies.
842842
<ItemGroup>
843843
<_ContentIncludedForCheck
844844
Include="@(Content)"
845-
Condition="'%(Content.ExcludeFromContentCheck)' != 'true'" />
845+
Condition="'%(Content.ExcludeFromContentCheck)' != 'true' and '%(Content.ExcludeFromSingleFile)' != 'true'" />
846846
</ItemGroup>
847847

848848
<LogWarningsForFiles

0 commit comments

Comments
 (0)