Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Baz00k opened this issue Dec 22, 2023 · 6 comments
Open

Comments

@Baz00k
Copy link

Baz00k commented Dec 22, 2023

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
Copy link
Owner

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
Copy link

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
Copy link
Author

Baz00k commented Dec 28, 2023

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
Copy link

matteocontrini commented Jan 18, 2024

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
Copy link
Author

Baz00k commented Jan 18, 2024

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

@asteiger
Copy link

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);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants