templating icon indicating copy to clipboard operation
templating copied to clipboard

PostAction Scripts don't run in Visual Studio

Open theLeftTenant opened this issue 1 year ago • 13 comments

Product

Visual Studio

Describe The Bug

When running dotnet new [template] on a template with a post action script, the user is prompted to respond with a Yes/No on whether they will allow the template to run a post action script. However, in Visual Studio, this prompt never appears and the postAction script never runs.

To Reproduce

Steps:

  1. Create or install a template with a postAction script
  2. Test that it runs with the CLI
  3. Attempt to create a project with the same template in Visual Studio
  4. Observe that the script did not run

dotnet Info

output .NET SDK: Version: 7.0.304 Commit: 7e794e2806

Runtime Environment: OS Name: Windows OS Version: 10.0.19044 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\7.0.304\

Host: Version: 7.0.7 Architecture: x64 Commit: 5b20af47d9

.NET SDKs installed: 3.1.426 [C:\Program Files\dotnet\sdk] 5.0.408 [C:\Program Files\dotnet\sdk] 5.0.411 [C:\Program Files\dotnet\sdk] 6.0.100 [C:\Program Files\dotnet\sdk] 6.0.102 [C:\Program Files\dotnet\sdk] 6.0.116 [C:\Program Files\dotnet\sdk] 6.0.202 [C:\Program Files\dotnet\sdk] 6.0.311 [C:\Program Files\dotnet\sdk] 7.0.304 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.28 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.18 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Learn more: https://aka.ms/dotnet/info

Download .NET: https://aka.ms/dotnet/download

Visual Studio Version

17.6.3

Additional context

No response

theLeftTenant avatar Jun 19 '23 20:06 theLeftTenant

If there is a known issue with no way to resolve, it'd be good to note that in the related wiki entry

theLeftTenant avatar Jun 19 '23 21:06 theLeftTenant

Hi @theLeftTenant ,

Thank you for reporting the issue.

@phenning, could you tell me if it belongs to your area?

YuliiaKovalova avatar Jun 20 '23 10:06 YuliiaKovalova

Currently, we don't run any post action scripts in Visual Studio or Visual Studio for Mac.

Please note that in the linked wiki entry, Visual Studio is not listed under the "Supported in:" section for that Post Action. This could probably be documented a bit more clearly in the wiki.

phenning avatar Jun 20 '23 16:06 phenning

@phenning thanks for the response. I assume packaging the template as a VSIX extension would make no difference?

The templates my team writes for developers throughout our company requires at least 3 postAction scripts to execute before the generated app will be fully deployable without manual intervention. Most of these developers use Visual Studio heavily, so it makes a lot of sense to let them directly generate the solutions within Visual Studio New Project GUI.

theLeftTenant avatar Jun 20 '23 16:06 theLeftTenant

It would be really nice to have the postActions run within the visual studio GUI.

Didza avatar Oct 16 '23 12:10 Didza

+1

JamieT-Shout avatar Oct 25 '23 14:10 JamieT-Shout

+1

POFerro avatar May 27 '24 11:05 POFerro

This is the one feature missing for me to completely switch to these new templates in my Visual Studio extension, looking forward to it. Do you have any idea when will this be available?

POFerro avatar May 27 '24 11:05 POFerro

Alternatively, I'd try this approach at the moment in the moment.

<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
        <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. Please install Node.js from https://nodejs.org/, then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
</Target>

prince272 avatar Aug 19 '24 15:08 prince272

Alternatively, I'd try this approach at the moment in the moment.

<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <!-- Ensure Node.js is installed -->
    <Exec Command="node --version" ContinueOnError="true">
        <Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. Please install Node.js from https://nodejs.org/, then restart your command prompt or IDE." />
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..." />
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
</Target>

Yep, this is similar to the workaround I came up with. We needed to ensure it ran once, so our script drops a dot file into the solution root after the first run and then short circuits on every build thereafter where it detects that file. This is hacky tho. It would be ideal to use the same PostAction in both the CLI and VS.

theLeftTenant avatar Aug 26 '24 17:08 theLeftTenant

We have done some investigations on the Visual Studio side, but I'm not sure if we will be enabling this in Visual Studio due to security concerns. Recently there has been a significant push to increase security so that makes me even more cautious about adding this support in Visual Studio. By adding support in VS requires that we have a good story for the security concerns. We may be able to add support in a safe manner, but I'm not sure what that would look like or how much time/investment that would take to implement. Currently we are working on some high priority work.

sayedihashimi avatar Aug 26 '24 19:08 sayedihashimi

@sayedihashimi, that's an understandable concern.

However, a simple dialog box asking the user if they wish to allow a script to run as a part of app generation, would not only constitute feature parity with the .NET CLI, but it also appears to be safer than the pre-build script mentioned above, since it would gather informed consent from the user.

This is more a matter of feature parity and code reuse than it is about requesting net-new capabilities.

theLeftTenant avatar Aug 26 '24 20:08 theLeftTenant

Visual Studio will never have full parity with what's available with dotnet new, but we do try to ensure that we support the most important/used features. We have to be picky on what we add support for because it takes away from other efforts and has to be maintained for a long time. Given the other work that we have on our plates, I don't see us getting to supporting this anytime soon. To support something like this in VS you could wrap your dotnet new template in a VSTemplate with a custom wizard that can execute code after the project is created, but that would take a good amount of effort.

sayedihashimi avatar Aug 27 '24 01:08 sayedihashimi