Eptagone / Vite.AspNetCore

Small library to integrate Vite into ASP.NET projects
MIT License
264 stars 35 forks source link

Vite kill shell script is not launched when running app from terminal #75

Open Baz00k opened 11 months ago

Baz00k commented 11 months ago

Expected behaviour: When I run MVC app with dotnet run or dotnet watch with app.UseViteDevMiddleware(); a new shell script is created to kill vite after the app shuts down. The script self deletes after closing the app.

Current behaviour: When I use app.UseViteDevMiddleware(); in my app, a new shell script with guid name is added to project root. Unfortunately, the script is not launched and persists after shutting down the app. This is a problem, because this file can't be git ignored (random name and root directory) so I have to manually delete it after each app shutdown. The problem does not exist if the app is launched through vscode debugger.

System: MacOs Dotnet version: 8.0.100

I can provide futher information and help if needed.

Eptagone commented 11 months ago

Hi, unfortunately I don't have a Mac to reproduce the bug you describe. The error doesn't seem to appear on Windows or Linux as i can see. If I manage to get a Mac, then I'll take a look. If you or someone else find a clue, please let me know here or make a pull request with the changes.

jasonmueller commented 11 months ago

I haven't had time to troubleshoot it, but am seeing the same behavior. Note that if I kill the process that the script is waiting on, it kills the child process and cleans itself up. This leads me to believe that when the dotnet process is stopped, it's automatically killing child processes, including the cleanup process (before it gets a chance to delete itself).

If the native behavior of dotnet on MacOS is to kill the child processes automatically, perhaps the script isn't needed at all? If I get time I can do a custom build and set some more debugging / do more testing.

Baz00k commented 11 months ago

I was investigating if the Vite process persists when killing the dotnet process and it appears that is does not. The only thing that is weird to me is that the script is executed when running the app through vscode debugger (even when killing the process externally). Unfortunately, I'm not quite sure why

matteocontrini commented 10 months ago

While this gets investigated, would it be possible to make sure the shell script is created in a hidden subdirectory? Like .vite, so that it can at least be gitignored.

Unfortunately the current behaviour is polluting the root directory with a new randomly-named filed every time the dev server is started.

Baz00k commented 10 months ago

@matteocontrini I support your idea, in the meantime you can ignore the files with /[a-zA-Z0-9]*.sh in your .gitignore

asteiger commented 9 months ago

Workaround:

WebApplicationExtensions.cs

using System.Text.RegularExpressions;

namespace Extensions;

public static class WebApplicationExtensions
{
    public static void UseViteCleanup(this WebApplication app)
    {
        var logger = app.Services.GetRequiredService<ILogger<WebApplication>>();
        var lifetime = app.Services.GetRequiredService<IHostApplicationLifetime>();

        lifetime.ApplicationStopping.Register(() =>
        {
            try
            {
                var pattern = @"^[a-zA-Z0-9]*\.sh$";
                var regex = new Regex(pattern);

                var allFiles = Directory.EnumerateFiles(app.Environment.ContentRootPath, "*", SearchOption.TopDirectoryOnly);
                foreach (var file in allFiles)
                {
                    var filename = Path.GetFileName(file);
                    if (!regex.IsMatch(filename))
                    {
                        continue;
                    }

                    File.Delete(file);
                    logger.LogInformation("Deleted file {FileName}", filename);
                }
            }
            catch (Exception e)
            {
                logger.LogError(e, "Error deleting files");
            }
        });
    }
}

Program.cs

if (app.Environment.IsDevelopment())
{
    app.UseViteCleanup();
    app.UseViteDevelopmentServer(true);
}