Eptagone / Vite.AspNetCore

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

Azure Pipelines Build Error #138

Open RacerDelux opened 9 hours ago

RacerDelux commented 9 hours ago

Describe the bug When running Azure Pipelines and building in Release mode, I get the following error: There was an error exporting the HTTPS developer certificate to a file. Please create the target directory before exporting. Choose permissions carefully when creating it.

To Reproduce Set up a build pipeline with a project using Vite.AspNetCore. In this case I used the example MVC project.

Expected behavior For the built to complete. Since I am targeting Release, it shouldn't even be asking for a HTTPS developer certificate.

Screenshots image

Device (please complete the following information):

Additional context This is my build task in Cake:


var configuration = Argument<string>("configuration", "Release");
Task("Build")
    .IsDependentOn("Clean")
    .IsDependentOn("Restore")
    .Does(() =>
    {
        var buildSettings = new DotNetBuildSettings();
        buildSettings.NoRestore = true;
        buildSettings.Configuration = configuration;
        buildSettings.Verbosity = DotNetVerbosity.Detailed;

        DotNetBuild(solutionPath, buildSettings);
    });
Eptagone commented 8 hours ago

Hi, are you validating the environment before run app.UseViteDevelopmentServer()? That method shouldn't be called in production.

RacerDelux commented 6 hours ago

Hi, are you validating the environment before run app.UseViteDevelopmentServer()? That method shouldn't be called in production.

This is what is in my program.cs:

if (app.Environment.IsDevelopment())
{
    app.UseWebSockets();
    // Use Vite Dev Server as middleware.
    app.UseViteDevelopmentServer(true);
}

And this is my Environment setting in the pipeline: image

It looks like the error comes right after these NPM scripts are run. image

Eptagone commented 4 hours ago

Ahh. I was thinking you were running the Vite dev server but it's not that. You're creating a HTTPS certificate in your vite.config.ts like the examples. That's okey while you are developing the app, but it's not even required for production and as I see your pipeline process doesn't allow to create the certificate anyway. So, the solution is editing your vite.config.ts to avoid generating the dotnet certificate in production mode and/or remove all of the SSL configuration for Vite.

RacerDelux commented 4 hours ago

Ahh. I was thinking you were running the Vite dev server but it's not that.

You're creating a HTTPS certificate in your vite.config.ts like the examples. That's okey while you are developing the app, but it's not even required for production and as I see your pipeline process doesn't allow to create the certificate anyway.

So, the solution is editing your vite.config.ts to avoid generating the dotnet certificate in production mode and/or remove all of the SSL configuration for Vite.

Don't I need that for development? If so is it possible to pass through the environment variable and skip making the cert?

Eptagone commented 4 hours ago

The certificate is only required if you want/need to use https for both ASP.NET and Vite during development and use the same dev certificate. If that's not the case, you can work without SSL.

And as you said, you can also use environment variables to skip the cert in your vite.config.ts by reading the environment variable via process.env.MY_VARIABLE.

RacerDelux commented 2 hours ago

And as you said, you can also use environment variables to skip the cert in your vite.config.ts by reading the environment variable via process.env.MY_VARIABLE.

This worked great! Do you think it would be good to update the example to have this logic already? Could be useful. This is the modified code at the beginning of the defineConfig function:

// Ensure the certificate and key exist
if (process.env.ASPNETCORE_ENVIRONMENT === 'Development' && (!fs.existsSync(certFilePath) || !fs.existsSync(keyFilePath))) {
      // Make the cert folder if missing
      if (!fs.existsSync(baseFolder))
          fs.mkdirSync(baseFolder);

Not sure if there is a downside, but I changed the cert path to:

const baseFolder =
    `${process.env.PWD}/.certs`;

This helps get around permission issues when making the directory if it doesn't exist (something that has to be manually fixed when running the original example on OSX)