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

"dotnet new" can generate invalid code if directory name is not a valid C# namespace identifier #381

Closed
TheRealPiotrP opened this issue Feb 27, 2017 · 2 comments

Comments

@TheRealPiotrP
Copy link

From @mikeharder on February 23, 2017 21:52

Steps to reproduce

docker run -it --rm microsoft/dotnet:1.0.3-sdk-msbuild-rc4
mkdir new
cd new
dotnet new console
dotnet restore
dotnet build

Expected behavior

Builds successfully.

Actual behavior

Program.cs(3,11): error CS1001: Identifier expected [/new/new.csproj]
Program.cs(3,11): error CS1514: { expected [/new/new.csproj]
Program.cs(3,11): error CS0116: A namespace cannot directly contain members such as fields or methods [/new/new.csproj]
Program.cs(4,1): error CS1022: Type or namespace definition, or end-of-file expected [/new/new.csproj]

The root cause is dotnet new uses the directory name as the namespace in the generated code, without first checking if the directory name is a valid namespace identifier.

using System;

namespace new
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Environment data

dotnet --info output:

.NET Command Line Tools (1.0.0-rc4-004883)

Product Information:
 Version:            1.0.0-rc4-004883
 Commit SHA-1 hash:  fa2c9e025c

Runtime Environment:
 OS Name:     debian
 OS Version:  8
 OS Platform: Linux
 RID:         debian.8-x64
 Base Path:   /usr/share/dotnet/sdk/1.0.0-rc4-004883

Copied from original issue: dotnet/cli#5825

@mlorbetske
Copy link
Contributor

As a workaround, you can specify the -n switch to give a name that isn't a C# keyword. We'll beef up the name cleansing logic.

@donJoseLuis
Copy link
Contributor

This issue was last touched some years ago. We are working on a new delivery road map. Please reopen if this is something we want & we'll properly assess its' priority compared to other work aimed at improving the overall templating UX.

@donJoseLuis donJoseLuis removed this from the Backlog milestone Mar 19, 2020
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

No branches or pull requests

3 participants