templating icon indicating copy to clipboard operation
templating copied to clipboard

Issue: XML Conditional Processing Not Working in .slnx Files

Open StevenTCramer opened this issue 9 months ago • 4 comments

Is your feature request related to a problem? Please describe.

Issue: XML Conditional Processing Not Working in .slnx Files

Problem Description

I'm trying to use XML conditional processing format in a .slnx file within a .NET template, but it doesn't work as expected. Since .slnx files are XML-based, I expected the standard XML conditional processing syntax to work, but it seems the templating engine doesn't recognize .slnx files as XML for conditional processing purposes.

Steps to Reproduce

  1. Create a .NET template with a .slnx file
  2. Add conditional processing directives to the .slnx file using XML comment syntax:
    <!--#if (api)-->
    <Folder Name="/05-ContainerApps/Api/">
      <!-- Projects inside this folder -->
    </Folder>
    <!--#endif-->
    
  3. Generate a project from the template with the condition set to false (e.g., --api false)
  4. Observe that the conditional sections are not processed correctly - the comment directives are included in the output file, but the content between them is not excluded

Expected Behavior

When generating a project with --api false, the entire section (including the conditional directives and the content between them) should be excluded from the generated .slnx file.

Actual Behavior

The conditional directives (<!--#if (api)--> and <!--#endif-->) are included in the output file as literal text, and the content between them is also included regardless of the condition value.

Environment

  • .NET SDK version: 9.0.200
  • OS: Windows 11 Pro

Additional Context

The .slnx file format is relatively new and appears to be an XML-based solution file format. Since it's XML-based, I expected it to work with the standard XML conditional processing syntax as described in the documentation: https://github.com/dotnet/templating/wiki/Conditional-processing-and-comment-syntax#xml-based-formats

I believe this issue occurs because the .slnx extension is not mapped to XML for conditional processing purposes in the templating engine.

Possible Solution

The templating engine could be updated to recognize .slnx files as XML-based files for conditional processing purposes, similar to how it handles .csproj, .vbproj, and other XML-based files.

Alternatively, if there's a different recommended approach for handling conditional content in .slnx files, documentation on this would be helpful.

Describe the solution you'd like.

The templating engine could be updated to recognize .slnx files as XML-based files for conditional processing purposes, similar to how it handles .csproj, .vbproj, and other XML-based files.

Additional context

No response

StevenTCramer avatar Mar 02 '25 16:03 StevenTCramer

This seems entirely reasonable to me - would you be willing to make a contribution?

baronfel avatar Mar 02 '25 17:03 baronfel

This seems entirely reasonable to me - would you be willing to make a contribution?

Yes I am willing. Will give it a try on the weekend to see if I can find it. Then report back.

StevenTCramer avatar Mar 05 '25 07:03 StevenTCramer

A fix for this has been merged to main but for a work around until the fix is released you can use this SpecialCustomOperation in your template.

{
  "$schema": "http://json.schemastore.org/template",
  "SpecialCustomOperations": {
    "**.slnx": {
      "operations": [
        {
          "type": "conditional",
          "configuration": {
            "actionableIf": [ "<!--#if" ],
            "actionableElse": [ "#else", "<!--#else" ],
            "actionableElseif": [ "#elseif", "<!--#elseif", "#elif", "<!--#elif" ],
            "endif": [ "#endif", "<!--#endif" ],
            "trim" : "true",
            "wholeLine": "true",
            "evaluator": "C++"
          }
        },
        {
          "type": "balancednesting",
          "configuration": {
            "startToken": "<!--",
            "realEndToken": "-->",
            "pseudoEndToken": "-- >",
            "id": "fixPseudoNestedComments",
            "resetFlag": "_TestResetFlag_"
          }
        }
      ]
    }
  }
}

karl-sjogren avatar Apr 09 '25 05:04 karl-sjogren

Another workaround is to use the SLNX file extension as XML and define a rename operation in the template.json.

I've been using this way since the inception of SLNX.

egvijayanand avatar Oct 02 '25 19:10 egvijayanand