dotnet / arcade

Tools that provide common build infrastructure for multiple .NET Foundation projects.
MIT License
667 stars 345 forks source link

XliffTasks breaks with strongly generated resources classes #14239

Open rolfbjarne opened 10 months ago

rolfbjarne commented 10 months ago

Enabling generated strongly typed code for resources breaks XliffTasks with:

error MSB3573: The language for a strongly typed resource class was specified, but more than one source file was passed in. Please pass in only one source file at a time if you want to generate strongly typed resource classes.

Test project: xlifftest-e9b5f83.zip To repro: download & unzip & dotnet build

For reference, this is the EmbeddedResource:

<EmbeddedResource Update="Resources.resx">
  <Generator>ResXFileCodeGenerator</Generator>
  <LastGenOutput>Resources.Designer.cs</LastGenOutput>
  <CustomToolNamespace>Resources</CustomToolNamespace>
  <ManifestResourceName>Resources</ManifestResourceName>
  <StronglyTypedFileName>Resources.designer.cs</StronglyTypedFileName>
  <StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
  <StronglyTypedNamespace>Resources</StronglyTypedNamespace>
  <StronglyTypedClassName>Resources</StronglyTypedClassName>
  <GenerateResource>true</GenerateResource>
  <PublicClass>true</PublicClass>
</EmbeddedResource>

Binlog: msbuild.binlog.zip

rolfbjarne commented 10 months ago

This seems to fix it:

<Target Name="FixEmbeddedResources" AfterTargets="TranslateSourceFromXlf">
  <ItemGroup>
    <EmbeddedResource Update="@(EmbeddedResource)" Condition="'%(EmbeddedResource.XlfSource)' != ''">
      <StronglyTypedClassName />
      <StronglyTypedLanguage />
      <StronglyTypedNamespace />
      <StronglyTypedFileName />
    </EmbeddedResource>
  </ItemGroup>
</Target>
missymessa commented 8 months ago

@rolfbjarne please feel free to submit a PR to the repo with this fix. Thanks! :)

markusroessler commented 7 months ago

For me the following works in a Dotnet MAUI Project (with both dotnet build and dotnet publish):

<Target Name="GenerateResXClass" AfterTargets="GatherXlf">
  <ItemGroup>
      <EmbeddedResource Update="Resources\Localization\AppResources.resx">
              <Generator>ResXFileCodeGenerator</Generator>
              <LastGenOutput>Resources/Localization/AppResources.Designer.cs</LastGenOutput>
              <StronglyTypedFileName>Resources/Localization/AppResources.Designer.cs</StronglyTypedFileName>
              <StronglyTypedLanguage>CSharp</StronglyTypedLanguage>
              <StronglyTypedNamespace>XXX.Maui.Resources.Localization</StronglyTypedNamespace>
              <StronglyTypedClassName>AppResources</StronglyTypedClassName>
      </EmbeddedResource>
  </ItemGroup>
</Target>

After some research I think the problems is that all properties of the EmbeddedResource (e.g. StronglyTypedClassName) are copied to the new TaskItem using the Copy-Constructor: https://github.com/dotnet/arcade/blob/main/src/Microsoft.DotNet.XliffTasks/Tasks/GatherXlf.cs#L39