question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Adding package references via post action doesn't work

See original GitHub issue

I’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:closed
  • Created a year ago
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
vlada-shubinacommented, Aug 15, 2022

We may consider servicing the following for 6.0.4xx:

I’ll keep posted on decision made.

1reaction
GangWang01commented, Aug 11, 2022

Update more detail on the investigation above.

For dotnet 6.0.301 and above, it needs ./ prefix with relative path of targetFiles to get adding package reference post action work. But the command like dotnet 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 using targetFiles.

Commit https://github.com/dotnet/templating/commit/b7dc29745861400991ca519f85bfd8a9eee43fd4 in main branch resolved the problem. targetFiles can know the file name change when instantiating template.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nuget-package not adding package reference
Somehow there is no reference added to the project. Things I've tried: Update-Package -reinstall (does not help); packages.config contains the ...
Read more >
The project does not support adding package references ...
Help me with fixing : The project does not support adding package references through the add package command : r/dotnet.
Read more >
The project does not support adding package references ...
The project does not support adding package references through the add package command.
Read more >
Nuget package changes not reflected in project ...
Right click on “References” and choose to add a (non-Nuget) reference. Add a new reference (like System. Data) Observe that now the References...
Read more >
Migrate from packages.config to PackageReference
Workaround. Perform any one of the following NuGet actions: Open the Package Manager UI - Right-click on References and select Manage NuGet  ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found