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

Don't copy intermediate translated files #9159

Merged
merged 2 commits into from
Jul 13, 2023

Conversation

tmeschter
Copy link
Contributor

@tmeschter tmeschter commented Jul 12, 2023

Related to AB#1849781.

By default we translate all XamlPropertyRule, XamlPropertyRuleNoCodeBehind, and XamlPropertyProjectItemsSchema items via xliff-tasks. The XlfOutputItem metadata on the items specifies what kind of MSBuild item xliff-tasks produces for each translated file. Currently we specify EmbeddedResource because we want to embed the translated files in our resource DLLs. When xliff-tasks creates the items it also copies over all the metadata from the original item--including the value of CopyToOutputDirectory. We'll get back to that in a moment.

The items created by xliff-tasks represent intermediate files that still need some adjustment. For example, if the input file is AnalyzerReference.xaml, the Spanish translation will be AnalyzerReference.es.xaml. When we ship the file we want it to have the original name but live under a locale-specific folder, like es\AnalyzerReference.xaml. The CopyTranslatedXamlRulesToOutputDirectory target takes care of this for us.

However, the intermediate items are still EmbeddedResources with CopyToOutputDirectory set to PreserveNewest. So the standard targets that handle copying to the output directory find these intermediate EmbeddedResource items and, indeed, copy them. One effect of this is that they get copied into the NPM package we produce for C# Dev Kit even though they will never get used from that location (even if they had the right name and directory structure). While these files are functionally harmless they make the NPM package much larger and will also impose maintenance costs--future devs will have to waste time trying to understand why they are present and what they do.

The fix here is to have xliff-tasks put these intermediate files into an intermediate item, _TranslatedXamlRule. We then have a target, CreateEmbeddedResourceForTranslatedXamlRules, that handles creating the EmbeddedResource items but resets the CopyToOutputDirectory metadata. Since we have now put all the translated XAML rules into their own convenient item group we also update CopyTranslatedXamlRulesToOutputDirectory to use that instead of digging through all the EmbeddedResources to find the ones it cares about.

This reduces the NPM package to the expected files: a couple of assemblies and some package metadata files.

Aside: Whether or not it is desirable to embed all these files as resources and whether or not we are doing it correctly such that CPS can find them at runtime are open questions that I do not attempt to address here.

Microsoft Reviewers: Open in CodeFlow

By default we translate all `XamlPropertyRule`, `XamlPropertyRuleNoCodeBehind`, and `XamlPropertyProjectItemsSchema` items via xliff-tasks. The `XlfOutputItem` metadata on the items specifies what kind of MSBuild item xliff-tasks produces for each translated file. Currently we specify `EmbeddedResource` because we want to embed the translated files in our resource DLLs. When xliff-tasks creates the items it also copies over all the metadata from the original item--including the value of `CopyToOutputDirectory`. We'll get back to that in a moment.

The items created by xliff-tasks represent intermediate files that still need some adjustment. For example, if the input file is AnalyzerReference.xaml, the Spanish translation will be AnalyzerReference.es.xaml. When we ship the file we want it to have the original name but live under a locale-specific folder, like es\AnalyzerReference.xaml. The `CopyTranslatedXamlRulesToOutputDirectory` target takes care of this for us.

However, the intermediate items are still `EmbeddedResources` with `CopyToOutputDirectory` set to `PreserveNewest`. So the standard targets that handle copying to the output directory find these intermediate `EmbeddedResource` items and, indeed, copy them. One effect of this is that they get copied into the NPM package we produce for C# Dev Kit even though they will never get used from that location (even if they had the right name and directory structure). While these files are functionally harmless they make the NPM package much larger and will also impose maintenance costs--future devs will have to waste time trying to understand why they are present and what they do.

The fix here is to have xliff-tasks put these intermediate files into an intermediate item, `_TranslatedXamlRule`. We then have a target, `CreateEmbeddedResourceForTranslatedXamlRules`, that handles creating the `EmbeddedResource` items but resets the `CopyToOutputDirectory` metadata. Since we have now put all the translated XAML rules into their own convenient item group we also update `CopyTranslatedXamlRulesToOutputDirectory` to use that instead of digging through all the `EmbeddedResource`s to find the ones it cares about.

This reduces the NPM package to the expected files: a couple of assemblies and some package metadata files.

> Aside: Whether or not it is desirable to embed all these files as resources and whether or not we are doing it correctly such that CPS can find them at runtime are open questions that I do not attempt to address here.
@tmeschter tmeschter requested a review from a team as a code owner July 12, 2023 19:18
Add a test to verify the contents of the NPM package produced for C# Dev Kit.
Comment on lines +11 to +15
[UsesVerify]
public sealed class PackageContentTests
{
[Fact]
public Task NpmPackage()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@tmeschter tmeschter merged commit fc1c97f into dotnet:main Jul 13, 2023
5 checks passed
@ghost ghost added this to the 17.7 milestone Jul 13, 2023
@tmeschter tmeschter deleted the 230712-IncludeResourceAssemblies branch July 13, 2023 17:09
@tmeschter tmeschter modified the milestones: 17.7, 17.8 Jul 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants