Adding package references via post action doesn't work
See original GitHub issueI’m trying to dynamically add a new package reference to a solution template based on a symbol set previously through parameters in the post actions of my template, like this (package references changed for privacy):
{
...
"symbols": {
"hosting": {
"type": "parameter",
"datatype": "choice",
"description": "Choose between HttpSys and Kestrel for ASP.NET Core hosting",
"choices": [
{
"choice": "httpsys",
"description": "HttpSys hosting"
},
{
"choice": "kestrel",
"description": "Kestrel hosting"
}
],
"defaultValue": "httpsys"
}
// other params go here...
},
"postActions": [
{
"condition": "(hosting == 'httpsys')",
"description": "Adding reference to HttpSys",
"manualInstructions": [
{ "text": "Manually add the reference to your project file" }
],
"actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
"continueOnError": true,
"args": {
"referenceType": "package",
"reference": "Microsoft.AspNetCore.Server.HttpSys",
"version": "2.2.6",
"targetFiles": "MyProject/MyProject.csproj"
}
},
{
"condition": "(hosting == 'kestrel')",
"description": "Adding reference to Kestrel",
"manualInstructions": [
{ "text": "Manually add the reference to your project file" }
],
"actionId": "B17581D1-C5C9-4489-8F0A-004BE667B814",
"continueOnError": true,
"args": {
"referenceType": "package",
"reference": "Microsoft.AspNetCore.Server.Kestrel",
"version": "2.2.0",
"targetFiles": "MyProject/MyProject.csproj"
}
}
]
}
For adding post actions, I’ve been following the docs for post action registries at https://github.com/dotnet/templating/wiki/Post-Action-Registry#add-a-reference-to-a-project-file.
Running dotnet new mytemplate -n test123
unfortunately results in a very short error message without an explanation as to why it fails:
PS C:\Users\user\repos> dotnet new mytemplate -n test123
The template "App template" was created successfully.
Processing post-creation actions...
Post action failed.
Manual instructions: Manually add the reference to your project file
I’ve verified that the package references are compatible by manually running dotnet add package Microsoft.AspNetCore.Server.HttpSys --version 2.2.6
, which adds a reference to the newly generated .csproj file just fine. My colleagues have tested the template as well, resulting in the same error message. I’ve already tried working around this using a powershell script, but it would be way easier to use post actions for this purpose.
dotnet --version: 6.0.301 Platform: Windows
Issue Analytics
- State:
- Created a year ago
- Comments:13 (5 by maintainers)
Top GitHub Comments
We may consider servicing the following for 6.0.4xx:
I’ll keep posted on decision made.
Update more detail on the investigation above.
For dotnet 6.0.301 and above, it needs
./
prefix with relative path oftargetFiles
to get adding package reference post action work. But the command likedotnet new mytemplate -n test123
doesn’t work because of-n test123
. It renames project file when instantiating template. While just as the note of https://github.com/dotnet/templating/wiki/Post-Action-Registry#add-a-reference-to-a-project-file the file name change is ignored when usingtargetFiles
.Commit https://github.com/dotnet/templating/commit/b7dc29745861400991ca519f85bfd8a9eee43fd4 in main branch resolved the problem.
targetFiles
can know the file name change when instantiating template.