dotnet / templating

This repo contains the Template Engine which is used by dotnet new
https://docs.microsoft.com/dotnet/
MIT License
1.61k stars 367 forks source link

Replacement breaks conditional code #8045

Open marcselman opened 3 months ago

marcselman commented 3 months ago

Product

dotnet CLI (dotnet new)

Describe The Bug

Note, I've stripped most of the code to show the bare minimum for the bug

I have a .bicep file that contains the following code:

//#if(includeDatabaseContainer)
var cosmosDatabaseContainerName = '__apiNameLower__'
//#endif

In my template.json I have the following symbols:

"symbols": {
    "includeDatabaseContainer": {
        "type": "parameter",
        "datatype": "bool",
        "description": "Whether to include a database container",
        "isRequired": false,
        "defaultValue": "false"
    },
    "apiNameLower": {
        "type": "generated",
        "generator": "casing",
        "parameters": {
            "source": "apiName",
            "toLower": true
        },
        "replaces": "__apiNameLower__",
        "fileRename": "__apiNameLower__",
        "displayName": "Api Name Lower"
    }
}

The __apiNameLower__ replacement works fine throughout all my template files. But when I pass includeDatabaseContainer true as a parameter the output is not as expected.

For example:

dotnet new MyTemplate --apiName MyApi --includeDatabaseContainer true

I would expect:

var cosmosDatabaseContainerName = 'myapi'

But what I get:

myapi'
...
//#endif

To Reproduce

See the sample code in the bug description.

dotnet Info

output .NET SDK: Version: 8.0.204 Commit: c338c7548c Workload version: 8.0.200-manifests.00402117 Runtime Environment: OS Name: Windows OS Version: 10.0.22631 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.204\ .NET workloads installed: There are no installed workloads to display. Host: Version: 8.0.6 Architecture: x64 Commit: 3b8b000a0e .NET SDKs installed: 3.0.103 [C:\Program Files\dotnet\sdk] 6.0.321 [C:\Program Files\dotnet\sdk] 7.0.317 [C:\Program Files\dotnet\sdk] 8.0.106 [C:\Program Files\dotnet\sdk] 8.0.204 [C:\Program Files\dotnet\sdk] .NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.29 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.29 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.26 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.29 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation] Environment variables: Not set global.json file: Not found Learn more: https://aka.ms/dotnet/info Download .NET: https://aka.ms/dotnet/download

Visual Studio Version

No response

Additional context

No response

marcselman commented 3 months ago

I've figured out the root of the issue. It happens when //#endif is at the bottom/end of the .bicep file. Then it does not get parsed, I think. When I add an extra line of code below it, everything works fine.

Trokkin commented 1 month ago

I've stumbled on this issue in my work and was able to find a similar workaround, but it took me a day to figure it out on my own. It's tough when errors are silent.