templating icon indicating copy to clipboard operation
templating copied to clipboard

Conditions Don't Work With Multi Choices

Open chobo2 opened this issue 5 months ago • 5 comments

Product

Visual Studio

Describe The Bug

Hi

I am trying to do conditions on my multi select choices.

I found this example: https://github.com/dotnet/templating/wiki/Conditions

I copied it as is into my template

 "symbols": {
    "Platform": {
      "type": "parameter",
      "description": "The target platform for the project.",
      "datatype": "choice",
      "allowMultipleValues": true,
      "enableQuotelessLiterals": true,
      "choices": [
        {
          "choice": "Windows",
          "description": "Windows Desktop"
        },
        {
          "choice": "WindowsPhone",
          "description": "Windows Phone"
        },
        {
          "choice": "MacOS",
          "description": "Macintosh computers"
        },
        {
          "choice": "iOS",
          "description": "iOS mobile"
        },
        {
          "choice": "android",
          "description": "android mobile"
        },
        {
          "choice": "nix",
          "description": "Linux distributions"
        }
      ],
      "defaultValue": "MacOS|iOS"
    }
}

When I do this

#if (Platform == MacOS) // MacOS choice flag specified here #endif

Does not matter if I choose it or not it always seems to be fall.

I am using NET 8, Blazor 8 and Visual Studio 2022 if that makes any difference

To Reproduce

Follow steps in the link

dotnet Info

output

.NET SDK: version: 8.0.100

Visual Studio Version

2022

Additional context

No response

chobo2 avatar Jan 11 '24 21:01 chobo2

I seem to only be getting this issue when I try to use my template in VS 2022, when I use the cli it seems to work proper.

chobo2 avatar Jan 11 '24 23:01 chobo2

Adding @phenning since you mentioned this is specific to VS2022.

sayedihashimi avatar Jan 12 '24 00:01 sayedihashimi

Adding @phenning since you mentioned this is specific to VS2022.

It could be other versions of VS but I don't have anything older but yes it just seems to be a problem going through VS and not through command line

chobo2 avatar Jan 12 '24 03:01 chobo2

Okay I got a bit of time and uploaded the template to my github:

https://github.com/chobo2/VSTemplateExample

I installed the template by going to the folder and doing "dotnet new install ."

I created a project by going

dotnet new mynewcustomtemplate -o "test" --Platform MacOS 

 dotnet new mynewcustomtemplate -o "test"

The first command I expect to be able to go to Program.Cs and see this line

// MacOS choice flag specified here

second command I do not expect to see this (I removed MacOS from the default value)

I got the expected results in both of them.

I moved on to VS 2022 and did the same thing. I created a new project and checked MacOS and then I created another project without MacOS checked.

In both instances I did not see // MacOS choice flag specified here in Program.cs

Other things to note that I noticed

  1. When I create a new project based on my template and keep "place soultion and project in same directory" UNCHECKED

I get this folder structure of \Project1\Project1\Project1 It seems to create an extra "Project1" folder. When I do it on command line, I get \Project1\Project1 which seems correct to me as that is what other VS template do.

  1. With default Value, if I completely remove it in my template.json and go to the VS template, the first choice is still always checked.
  "choices": [
            {
              "choice": "Windows",
              "description": "Windows Desktop"
            },
            {
              "choice": "WindowsPhone",
              "description": "Windows Phone"
            },
            {
              "choice": "MacOS",
              "description": "Macintosh computers"
            },
            {
              "choice": "iOS",
              "description": "iOS mobile"
            },
            {
              "choice": "android",
              "description": "android mobile"
            },
            {
              "choice": "nix",
              "description": "Linux distributions"
            }
          ],
          "defaultValue": "iOS" // if I remove this line of just put an empty string in, in the VS 2022  project creation  "Windows" will become always checked.
        }

chobo2 avatar Jan 12 '24 15:01 chobo2

Adding @phenning since you mentioned this is specific to VS2022.

Anyone have any updates on this if I am doing it wrong or if this is a bug?

chobo2 avatar Feb 06 '24 17:02 chobo2