Dash `-` in `sourceName` gets changed to underscore `_` inside files but not in file names
See original GitHub issueFrom @petrroll on July 28, 2017 9:41
Steps to reproduce
dotnet new -i "Peachpie.Templates::*"
(template’s config on GitHub)dotnet new peachpie-web -n tst-tst
// specifies asourceName
that gets replaceddotnet restore
It should be reproducible with any template, didn’t have time to find any other that uses sourceName both in files and filenames, though.
Expected behavior
The original sourceName
gets replaced either with tst-tst
or tst_tst
everywhere, i.e. both in the source files (including the .sln
) and in file names (e.g. names of the .msbuildproj
or folders).
Actual behavior
The original sourceName
gets replaced with tst_tst
inside source files (e.g. in the .sln
file or the server’s Program.cs
) but with tst-tst
in file and folder names.
The mismatch between paths expected by the .sln
file (it expects the projects in tst_tst.Server
and tst_tst
folders while they actually are in tst-tst.Server
and tst-tst
folders) breaks the .sln
file and thus the template’s build.
Environment data
dotnet --info
output:
Tested on both .NET SDK 1.0.4 on Windows and on .NET SDK 2.0 preview-2 on Ubuntu via WSL.
.NET Command Line Tools (2.0.0-preview2-006497)
Product Information:
Version: 2.0.0-preview2-006497
Commit SHA-1 hash: 06a2093335
Runtime Environment:
OS Name: ubuntu
OS Version: 16.04
OS Platform: Linux
RID: ubuntu.16.04-x64
Base Path: /usr/share/dotnet/sdk/2.0.0-preview2-006497/
Microsoft .NET Core Shared Framework Host
Version : 2.0.0-preview2-25407-01
Build : 40c565230930ead58a50719c0ec799df77bddee9
.NET Command Line Tools (1.0.4)
Product Information:
Version: 1.0.4
Commit SHA-1 hash: af1e6684fd
Runtime Environment:
OS Name: Windows
OS Version: 10.0.15063
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\1.0.4
Copied from original issue: dotnet/cli#7307
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:9 (3 by maintainers)
Top GitHub Comments
Hi @petrroll, There is a way to slightly modify the template source to prevent this problem:
With those changes, your template should work as expected - and should also work for other versions of dotnet new.
USing the
-n tst-tst
input, all occurrences of “MyWebsite.1” will be replaced with “tst-tst”, and the namespace in Program.cs will be replaced with “tst_tst”.Please let me know if this solution works for you - if not, let me know the problem, and we’ll figure it out 😃
Partial explanation of what’s happening behind the scenes:
The reason this is necessary is because when the template engine creates an instance of a template, it makes variants on the input name that are meant to be safe to use in particular circumstances. For example it makes a variant that is a valid namespace, and another variant which is a valid class name. But the template engine doesn’t try to determine the semantic meaning of various symbols, so it doesn’t explicitly know when to use the namespace version. This is why we had to make the slightly different change in Program.cs.
The transformations used to make the variants are applied to both the sourceName value, and the user provided input value, and maps the results of the transformed sourceName to the transformed input name. The original values are also included in the mapping. In this case, with original values:
The transformation for namespace results in:
And the transformation that could be used for class name (if you wanted that type of replacement) is:
These variants (and the original) are used to setup replacements from the template source to the output of:
For the file names, we only replace the original sourceName value with the original user provided name value. We’re in the early stages of planning for more control of filename replacements, but don’t have anything concrete yet.
For others looking at this issue, I’ve had success using the following symbol to support both the new and old sdk with a single template:
This doesn’t mean you can use ‘.’ or ‘-’ in the name with the old sdk, but they will work with the new sdk.
Hope this helps someone!