templating
templating copied to clipboard
Item templates should pick correct language from project
Is your feature request related to a problem? Please describe.
When using a item template inside a F# project, e.g.
dotnet new avalonia.templatedcontrol
I would expect that the option -lang=F#
would not be required but it is.
So it is using the C# version by default when the -lang=F#
was omitted/forgotten.
Describe the solution you'd like.
The solution should be that the templating engine should detect that the item template is added to a project of a known language, when the item template has multiple matching identities with different lang
tags the correct one should be picked.
Additional context
Somewhat related to: #5358
Also I am aware of #2684 but I don't think that is a solution when e.g. you mostly use C# but only sometimes F#.
Something like this should be enabled now that 'project context' and 'constraints' were implemented for .NET 7. Item Template authors should be able to define constraints on the different templates to match them to projects of a particular language, so this kind of automatic selection could just work automatically.
thanks for the very quick response, will try if I get that desired behavior to work with constraints.
@baronfel So I tried with the constraints here: (comparing C# to F# template)
But I get the error:
Failed to instatiate template 'Avalonia TemplatedControl', the following constraints are not met: Project capabiltities: The template needs project capability 'CSharp', and current project (C:\Code\AvaloniaFSharp\AvaloniaFSharp.fsproj) does not satisfy it.
So it seems to not work yet.
Picking up correct template language based on project context is not available yet. It might be a good extension to project context feature, @baronfel please let us know if you'd like to prioritize it.
In addition, I found even if explicitly specifying the language with --language F#
when instantiating F# item template with the constraint below
"constraints": {
"fsharp-only": {
"type": "project-capability",
"args": "FSharp"
}
}
it failed with the error:
Failed to instatiate template 'Repro 5844', the following constraints are not met:
Project capabiltities: The template needs project capability 'FSharp', and current project (F:\WS\Repro\5844\output\FSharp\FSharp.fsproj) does not satisfy it.
To run the template anyway, use '--force' option:
dotnet new repro5844 --language F# --force
For details on the exit code, refer to https://aka.ms/templating-exit-codes#100
After some investigation, I realized currently FSharp
is not an available project capability according to https://github.com/microsoft/VSProjectSystem/blob/master/doc/overview/project_capabilities.md.
In addition, I found even if explicitly specifying the language with
--language F#
when instantiating F# item template with the constraint below"constraints": { "fsharp-only": { "type": "project-capability", "args": "FSharp" } }
it failed with the error:
Failed to instatiate template 'Repro 5844', the following constraints are not met: Project capabiltities: The template needs project capability 'FSharp', and current project (F:\WS\Repro\5844\output\FSharp\FSharp.fsproj) does not satisfy it. To run the template anyway, use '--force' option: dotnet new repro5844 --language F# --force For details on the exit code, refer to https://aka.ms/templating-exit-codes#100
After some investigation, I realized currently
FSharp
is not an available project capability according to https://github.com/microsoft/VSProjectSystem/blob/master/doc/overview/project_capabilities.md.
let me investigate it. it looks as a blocker to any F# related item templates work.
Hi everyone,
I have contacted the author of the https://github.com/microsoft/VSProjectSystem/blob/master/doc/overview/project_capabilities.md - the document is outdated. F# is added to project capabilities.
The problem is on the templating side - the required information isn't retrieved during the evaluation phase [ProjectCapabilityConstraint.cs](https://github.com/dotnet/sdk/blob/3eea35e0d77f9a1bc418a364805ff89df818e371/src/Cli/dotnet/commands/dotnet-new/MSBuildEvaluation/ProjectCapabilityConstraint.cs#:~:text=static%20void-,AddProjectCapabilities,-(HashSet%3C )
We are working on fixing the issue.
Hi everyone,
I have contacted the author of the https://github.com/microsoft/VSProjectSystem/blob/master/doc/overview/project_capabilities.md - the document is outdated. F# is added to project capabilities.
The problem is on the templating side - the required information isn't retrieved during the evaluation phase [ProjectCapabilityConstraint.cs](https://github.com/dotnet/sdk/blob/3eea35e0d77f9a1bc418a364805ff89df818e371/src/Cli/dotnet/commands/dotnet-new/MSBuildEvaluation/ProjectCapabilityConstraint.cs#:~:text=static%20void-,AddProjectCapabilities,-(HashSet%3C)
We are working on fixing the issue.
Some more details:
-
MSBuild evaluation as done now doesn't return
FSharp
capability, contrary toCSharp
andVB
. - More investigation is required on why is that
- assumption: we might need to do design-time build instead of evaluation, however the performance is concern.
I did some digging into the installed SDK and I'm not sure the F# props and targets add a ProjectCapability
for FSharp
. This may be a miss on our part - assuming that such a thing existed. It should be a very simple request - the C# and VB targets include it in Microsoft.LANGUAGE.CurrentVersion.targets
- I assume the F# targets would need that added as well.
@baronfel,
You are right here - Microsoft.CSharp.CurrentVersion.targets and Microsoft.VisualBasic.CurrentVersion.targets specify it explicitly in the target files
But I can't find the same file for FSharp and I can see that ProjectCapability is specified when we run msbuild using DevCommandPrompt
-
I did some digging into the installed SDK and I'm not sure the F# props and targets add a
ProjectCapability
forFSharp
. This may be a miss on our part - assuming that such a thing existed. It should be a very simple request - the C# and VB targets include it inMicrosoft.LANGUAGE.CurrentVersion.targets
- I assume the F# targets would need that added as well.
The question is on which step of the build those are included. Now we are doing evaluation only. ProjectCapability
for FSharp
per the tests are not included on this stage now, it might be included in further targets which are not fine during evaluation. Including them in SDK sounds as a great idea to try.
Note from iteration "2023 January". Not actionable until F# team is ready, ~ May 2023.
After impediment is resolved, the work in template resolution still needs to be done. Picking up correct template language based on project context is not available yet, and template resolution logic should be adjusted to take constraints into account.
Relevant code managing constraints (https://github.com/dotnet/sdk/blob/11f4fee47293e8c9dae5fa902074eb26062d8f99/src/Cli/Microsoft.TemplateEngine.Cli/Commands/create/TemplateCommand.cs#L158-L168) happens only when single template is defined.
It should be moved earlier in the invocation cycle, potentially in this method: https://github.com/dotnet/sdk/blob/11f4fee47293e8c9dae5fa902074eb26062d8f99/src/Cli/Microsoft.TemplateEngine.Cli/Commands/create/InstantiateCommand.cs#L363-L385
If constraints are satisfied only for single template, it should be preferred. This should fix auto language pick-up.