diff --git a/.changeset/selfish-mangos-drum.md b/.changeset/selfish-mangos-drum.md new file mode 100644 index 000000000000..8fa2e7e75da2 --- /dev/null +++ b/.changeset/selfish-mangos-drum.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes regression with config file restarts diff --git a/packages/astro/src/core/dev/index.ts b/packages/astro/src/core/dev/index.ts index 7c0ed10fdba6..04af0448624e 100644 --- a/packages/astro/src/core/dev/index.ts +++ b/packages/astro/src/core/dev/index.ts @@ -1,3 +1,3 @@ -export { createContainer, runInContainer, startContainer } from './container.js'; +export { createContainer, runInContainer, startContainer, isStarted } from './container.js'; export { default } from './dev.js'; export { createContainerWithAutomaticRestart } from './restart.js'; diff --git a/packages/astro/src/core/dev/restart.ts b/packages/astro/src/core/dev/restart.ts index edbc9af02d0d..9ded41325cbb 100644 --- a/packages/astro/src/core/dev/restart.ts +++ b/packages/astro/src/core/dev/restart.ts @@ -8,10 +8,10 @@ import { createContainer, isStarted, startContainer } from './container.js'; async function createRestartedContainer( container: Container, - settings: AstroSettings + settings: AstroSettings, + needsStart: boolean ): Promise { const { logging, fs, resolvedRoot, configFlag, configFlagPath } = container; - const needsStart = isStarted(container); const newContainer = await createContainer({ isRestart: true, logging, @@ -78,10 +78,10 @@ export async function restartContainer({ const { logging, close, resolvedRoot, settings: existingSettings } = container; container.restartInFlight = true; - //console.clear(); // TODO move this if (beforeRestart) { beforeRestart(); } + const needsStart = isStarted(container); try { const newConfig = await openConfig({ cwd: resolvedRoot, @@ -96,7 +96,7 @@ export async function restartContainer({ const settings = createSettings(astroConfig, resolvedRoot); await close(); return { - container: await createRestartedContainer(container, settings), + container: await createRestartedContainer(container, settings, needsStart), error: null, }; } catch (_err) { @@ -105,7 +105,7 @@ export async function restartContainer({ await close(); info(logging, 'astro', 'Continuing with previous valid configuration\n'); return { - container: await createRestartedContainer(container, existingSettings), + container: await createRestartedContainer(container, existingSettings, needsStart), error, }; } diff --git a/packages/astro/test/units/dev/restart.test.js b/packages/astro/test/units/dev/restart.test.js index a993019c4c72..2e78f789df6f 100644 --- a/packages/astro/test/units/dev/restart.test.js +++ b/packages/astro/test/units/dev/restart.test.js @@ -3,9 +3,10 @@ import * as cheerio from 'cheerio'; import { createContainerWithAutomaticRestart, - runInContainer, + isStarted, + startContainer, } from '../../../dist/core/dev/index.js'; -import { createFs, createRequestAndResponse } from '../test-utils.js'; +import { createFs, createRequestAndResponse, triggerFSEvent } from '../test-utils.js'; const root = new URL('../../fixtures/alias/', import.meta.url); @@ -74,4 +75,38 @@ describe('dev container restarts', () => { await restart.container.close(); } }); + + it('Restarts the container if previously started', async () => { + const fs = createFs( + { + '/src/pages/index.astro': ` + + Test + +

Test

+ + + `, + '/astro.config.mjs': ``, + }, + root + ); + + let restart = await createContainerWithAutomaticRestart({ + params: { fs, root }, + }); + await startContainer(restart.container); + expect(isStarted(restart.container)).to.equal(true); + + try { + // Trigger a change + let restartComplete = restart.restarted(); + triggerFSEvent(restart.container, fs, '/astro.config.mjs', 'change'); + await restartComplete; + + expect(isStarted(restart.container)).to.equal(true); + } finally { + await restart.container.close(); + } + }); });