Skip to content

Commit

Permalink
Fix failed server restart calling integration hooks twice (#7917)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <[email protected]>
  • Loading branch information
bluwy and ematipico authored Aug 3, 2023
1 parent 6806629 commit 1f0ee49
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
5 changes: 5 additions & 0 deletions .changeset/metal-chairs-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Prevent integration hooks from re-triggering if the server restarts on config change, but the config fails to load.
4 changes: 0 additions & 4 deletions packages/astro/src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,3 @@ export async function startContainer({

return devServerAddressInfo;
}

export function isStarted(container: Container): boolean {
return !!container.viteServer.httpServer?.listening;
}
2 changes: 1 addition & 1 deletion packages/astro/src/core/dev/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { createContainer, isStarted, startContainer } from './container.js';
export { createContainer, startContainer } from './container.js';
export { default } from './dev.js';
export { createContainerWithAutomaticRestart } from './restart.js';
41 changes: 17 additions & 24 deletions packages/astro/src/core/dev/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import { createSafeError } from '../errors/index.js';
import { info, error as logError } from '../logger/core.js';
import { formatErrorMessage } from '../messages.js';
import type { Container } from './container';
import { createContainer, isStarted, startContainer } from './container.js';
import { createContainer, startContainer } from './container.js';

async function createRestartedContainer(
container: Container,
settings: AstroSettings,
needsStart: boolean
settings: AstroSettings
): Promise<Container> {
const { logging, fs, inlineConfig } = container;
const newContainer = await createContainer({
Expand All @@ -26,9 +25,7 @@ async function createRestartedContainer(
fs,
});

if (needsStart) {
await startContainer(newContainer);
}
await startContainer(newContainer);

return newContainer;
}
Expand Down Expand Up @@ -62,21 +59,15 @@ export function shouldRestartContainer(
return shouldRestart;
}

export async function restartContainer(
container: Container
): Promise<{ container: Container; error: Error | null }> {
export async function restartContainer(container: Container): Promise<Container | Error> {
const { logging, close, settings: existingSettings } = container;
container.restartInFlight = true;

const needsStart = isStarted(container);
try {
const { astroConfig } = await resolveConfig(container.inlineConfig, 'dev', container.fs);
const settings = createSettings(astroConfig, fileURLToPath(existingSettings.config.root));
await close();
return {
container: await createRestartedContainer(container, settings, needsStart),
error: null,
};
return await createRestartedContainer(container, settings);
} catch (_err) {
const error = createSafeError(_err);
// Print all error messages except ZodErrors from AstroConfig as the pre-logged error is sufficient
Expand All @@ -91,12 +82,9 @@ export async function restartContainer(
stack: error.stack || '',
},
});
await close();
container.restartInFlight = false;
info(logging, 'astro', 'Continuing with previous valid configuration\n');
return {
container: await createRestartedContainer(container, existingSettings, needsStart),
error,
};
return error;
}
}

Expand Down Expand Up @@ -137,11 +125,16 @@ export async function createContainerWithAutomaticRestart({
async function handleServerRestart(logMsg: string) {
info(logging, 'astro', logMsg + '\n');
const container = restart.container;
const { container: newContainer, error } = await restartContainer(container);
restart.container = newContainer;
// Add new watches because this is a new container with a new Vite server
addWatches();
resolveRestart(error);
const result = await restartContainer(container);
if (result instanceof Error) {
// Failed to restart, use existing container
resolveRestart(result);
} else {
// Restart success. Add new watches because this is a new container with a new Vite server
restart.container = result;
addWatches();
resolveRestart(null);
}
restartComplete = new Promise<Error | null>((resolve) => {
resolveRestart = resolve;
});
Expand Down
5 changes: 4 additions & 1 deletion packages/astro/test/units/dev/restart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import { fileURLToPath } from 'node:url';

import {
createContainerWithAutomaticRestart,
isStarted,
startContainer,
} from '../../../dist/core/dev/index.js';
import { createFs, createRequestAndResponse, triggerFSEvent } from '../test-utils.js';

const root = new URL('../../fixtures/alias/', import.meta.url);

function isStarted(container) {
return !!container.viteServer.httpServer?.listening;
}

describe('dev container restarts', () => {
it('Surfaces config errors on restarts', async () => {
const fs = createFs(
Expand Down

0 comments on commit 1f0ee49

Please sign in to comment.