Skip to content

[Bug] Source name in templates should be safe for default form replacements #2176

@vlada-shubina

Description

@vlada-shubina

We have recent issue reported to our repo related to maui-blazor. Reported issue is: dotnet/templating#3666

I made some checks below:

  1. I checked the templates and it appears that sln is not part of templates anymore. Could you please confirm?

  2. Could you please check if sourceName you are using in templates is safe for default forms?
    https://github.com/dotnet/templating/wiki/Naming-and-default-value-forms
    Template engine applies 5 forms to the source name:

  • identity - no transformation
  • safe namespace
string workingValue = Regex.Replace(value, @"(^\s+|\s+$)", "");
workingValue = Regex.Replace(workingValue, @"(((?<=\.)|^)((?=\d)|\.)|[^\w\.])|(\.$)", "_");
  • safe class name
string workingValue = Regex.Replace(value, @"(^\s+|\s+$)", "");
workingValue = Regex.Replace(workingValue, @"(((?<=\.)|^)(?=\d)|\W)", "_");
  • lower safe namespace and lower safe class name (same as above + ToLowerInvariant)

The source name you chose in template.json should provide different results for the form above, similar to the name mentioned in https://github.com/dotnet/templating/wiki/Naming-and-default-value-forms:

Transform Input Output
Identity Template.1 Template.1
Namespace Template.1 Template._1
Class Name Template.1 Template__1
Lowercase Namespace Template.1 template._1
Lowercase Class Name Template.1 template__1

When sourceName is used in namespaces and class names, corresponding form should be used instead.
If the sourceName chosen is not safe, i.e. forms transformation results in same result, it's not guaranteed that correct form will be used when template engine replaces the values. This happened for the issue above:

  • MauiApp1 is same for identity, namespace, and class name forms
  • user entered Maui-Blazor-Dash, which results in Maui-Blazor-Dash, Maui_Blazor_Dash, Maui_Blazor_Dash respectively
  • template engine picked up one of latter 2 for all replacements: while it is correct for namespace, it is not correct for replacements made in solution files, where Maui-Blazor-Dash should have been used.

While some other templates have same issue in sourceName, usually it is ok to use it if sourceName is only used in namespace for replacement, however it is not that case for maui, as sourceName is not used only for namespaces.

Metadata

Metadata

Assignees

Labels

area-templatesProject templates, Item Templates for Blazor and MAUIfixed-in-6.0.101-preview.10Look for this fix in 6.0.101-preview.10!

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions