question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dash `-` in `sourceName` gets changed to underscore `_` inside files but not in file names

See original GitHub issue

From @petrroll on July 28, 2017 9:41

Steps to reproduce

  1. dotnet new -i "Peachpie.Templates::*" (template’s config on GitHub)
  2. dotnet new peachpie-web -n tst-tst // specifies a sourceName that gets replaced
  3. dotnet 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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
seancpeterscommented, Jul 29, 2017

Hi @petrroll, There is a way to slightly modify the template source to prevent this problem:

  • In template.json, change the value of sourceName from “MyWebsite1” to “MyWebsite.1”.
  • In Program.cs, change the namespace from “MyWebsite1.Server” to “MyWebsite._1.Server”.
  • Change all other occurrences of “MyWebsite1” to “MyWebsite.1”, both in the file names and the file contents. (I believe the only file content changes are in MyWebste1.sln and MyWebsite1.Server.csproj)

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:

  • sourceName = MyWebsite.1
  • (input) name: tst-tst

The transformation for namespace results in:

  • sourceName (variant) = MyWebsite._1
  • name (variant) = tst_tst

And the transformation that could be used for class name (if you wanted that type of replacement) is:

  • sourceName (variant) = MyWebsite__1
  • name (variant) = tst_tst

These variants (and the original) are used to setup replacements from the template source to the output of:

  • MyWebsite.1 -> tst-tst
  • MyWebsite._1 -> tst_tst
  • MyWebsite__1 -> tst_tst So anywhere in the template file contents where the value on the left appears, it gets replaced with the corresponding value on the right.

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.

0reactions
cwensleycommented, Feb 9, 2018

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:

		"namespace-for-old-sdk": {
			"type": "generated",
			"generator": "coalesce",
			"parameters": {
				"sourceVariableName": "safe_namespace",
				"fallbackVariableName": "safe_name"
			},
			"replaces": "MyName._1"
		}

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

to underscores (_) on Win10 for many files: Windows ...
e.g. a search string name:(foo bar) does NOT seem to find a file named foo-bar.pdf However it DOES indeed find the same file...
Read more >
Replace all dashes with underscores in filenames
I need to replace all occurrences of "-" with "_" in every file name in every sub folder of a directory. This script...
Read more >
Custom Naming Files (defaulting to underscore instead of ...
The Filename Editor may default to a hyphen, but you can instead use the underscore typed in the Preset editor.
Read more >
How to rename file names - replacing underscores/dashes ...
found the answer on the microsoft forum, here it is: Open that folder in Windows File Explorer, please be sure you are in...
Read more >
Rename all files and replace hyphen - with underscore _ ...
So I have a bunch of files in a directory, pdf, docx etc and I am trying to rename them and replace dash...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found