Skip to content

Commit 6ee18e1

Browse files
dario-piotrowiczask-bonk[bot]edmundhung
authored
Fix autoconfig for Astro 6+ projects to skip wrangler config generation (#12856)
Co-authored-by: ask-bonk[bot] <249159057+ask-bonk[bot]@users.noreply.github.com> Co-authored-by: Edmund Hung <edmund@cloudflare.com>
1 parent de6e6b1 commit 6ee18e1

File tree

6 files changed

+70
-25
lines changed

6 files changed

+70
-25
lines changed

.changeset/real-plants-slide.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Fix autoconfig for Astro v6 projects to skip wrangler config generation
6+
7+
Astro 6+ generates its own wrangler configuration on build, so autoconfig now detects the Astro version and skips creating a `wrangler.jsonc` file for projects using Astro 6 or later. This prevents conflicts between the autoconfig-generated config and Astro's built-in config generation.

packages/create-cloudflare/e2e/tests/frameworks/test-config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,6 @@ function getExperimentalFrameworkTestConfig(
723723
{
724724
name: "astro:workers",
725725
argv: ["--platform", "workers"],
726-
quarantine: true,
727726
testCommitMessage: true,
728727
unsupportedOSs: ["win32"],
729728
verifyDeploy: {
Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import assert from "node:assert";
12
import { writeFileSync } from "node:fs";
23
import { brandColor, dim } from "@cloudflare/cli/colors";
4+
import semiver from "semiver";
35
import { runCommand } from "../c3-vendor/command";
6+
import { getInstalledPackageVersion } from "./utils/packages";
47
import { Framework } from ".";
58
import type { ConfigurationOptions, ConfigurationResults } from ".";
69

@@ -9,7 +12,10 @@ export class Astro extends Framework {
912
outputDir,
1013
dryRun,
1114
packageManager,
15+
projectPath,
1216
}: ConfigurationOptions): Promise<ConfigurationResults> {
17+
const astroVersion = getAstroVersion(projectPath);
18+
1319
const { npx } = packageManager;
1420
if (!dryRun) {
1521
await runCommand([npx, "astro", "add", "cloudflare", "-y"], {
@@ -21,18 +27,43 @@ export class Astro extends Framework {
2127
});
2228
writeFileSync("public/.assetsignore", "_worker.js\n_routes.json");
2329
}
24-
return {
25-
wranglerConfig: {
26-
main: `${outputDir}/_worker.js/index.js`,
27-
compatibility_flags: ["global_fetch_strictly_public"],
28-
assets: {
29-
binding: "ASSETS",
30-
directory: outputDir,
30+
31+
if (semiver(astroVersion, "6.0.0") < 0) {
32+
// Before version 6 Astro required a wrangler config file
33+
return {
34+
wranglerConfig: {
35+
main: `${outputDir}/_worker.js/index.js`,
36+
compatibility_flags: ["global_fetch_strictly_public"],
37+
assets: {
38+
binding: "ASSETS",
39+
directory: outputDir,
40+
},
3141
},
32-
},
42+
};
43+
}
44+
45+
// From version 6 Astro doesn't need a wrangler config file but generates a redirected config on build
46+
return {
47+
wranglerConfig: null,
3348
};
3449
}
3550

3651
configurationDescription =
3752
'Configuring project for Astro with "astro add cloudflare"';
3853
}
54+
55+
/**
56+
* Gets the installed version of the "astro" package
57+
* @param projectPath The path of the project
58+
*/
59+
function getAstroVersion(projectPath: string): string {
60+
const packageName = "astro";
61+
const astroVersion = getInstalledPackageVersion(packageName, projectPath);
62+
63+
assert(
64+
astroVersion,
65+
`Unable to discern the version of the \`${packageName}\` package`
66+
);
67+
68+
return astroVersion;
69+
}

packages/wrangler/src/autoconfig/frameworks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export type PackageJsonScriptsOverrides = {
1818

1919
export type ConfigurationResults = {
2020
/** The wrangler configuration that the framework's `configure()` hook should generate. `null` if autoconfig should not create the wrangler file (in case an external tool already does that) */
21-
wranglerConfig: RawConfig;
21+
wranglerConfig: RawConfig | null;
2222
// Scripts to override in the package.json. Most frameworks should not need to do this, as their default detected build command will be sufficient
2323
packageJsonScriptsOverrides?: PackageJsonScriptsOverrides;
2424
// Build command to override the standard one (`npm run build` or framework's build command)

packages/wrangler/src/autoconfig/run.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,12 @@ export async function runAutoConfig(
116116

117117
autoConfigSummary = await buildOperationsSummary(
118118
{ ...autoConfigDetails, outputDir: autoConfigDetails.outputDir },
119-
ensureNodejsCompatIsInConfig({
120-
...wranglerConfig,
121-
...dryRunConfigurationResults.wranglerConfig,
122-
}),
119+
dryRunConfigurationResults.wranglerConfig === null
120+
? null
121+
: ensureNodejsCompatIsInConfig({
122+
...wranglerConfig,
123+
...dryRunConfigurationResults.wranglerConfig,
124+
}),
123125
{
124126
build:
125127
dryRunConfigurationResults.buildCommandOverride ??
@@ -199,13 +201,15 @@ export async function runAutoConfig(
199201
);
200202
}
201203

202-
await saveWranglerJsonc(
203-
autoConfigDetails.projectPath,
204-
ensureNodejsCompatIsInConfig({
205-
...wranglerConfig,
206-
...configurationResults.wranglerConfig,
207-
})
208-
);
204+
if (configurationResults.wranglerConfig !== null) {
205+
await saveWranglerJsonc(
206+
autoConfigDetails.projectPath,
207+
ensureNodejsCompatIsInConfig({
208+
...wranglerConfig,
209+
...configurationResults.wranglerConfig,
210+
})
211+
);
212+
}
209213

210214
addWranglerToGitIgnore(autoConfigDetails.projectPath);
211215

@@ -316,7 +320,7 @@ export async function buildOperationsSummary(
316320
autoConfigDetails: AutoConfigDetailsForNonConfiguredProject & {
317321
outputDir: NonNullable<AutoConfigDetails["outputDir"]>;
318322
},
319-
wranglerConfigToWrite: RawConfig,
323+
wranglerConfigToWrite: RawConfig | null,
320324
projectCommands: {
321325
build?: string;
322326
deploy: string;
@@ -329,7 +333,11 @@ export async function buildOperationsSummary(
329333
const summary: AutoConfigSummary = {
330334
wranglerInstall: false,
331335
scripts: {},
332-
wranglerConfig: wranglerConfigToWrite,
336+
...(wranglerConfigToWrite !== null
337+
? {
338+
wranglerConfig: wranglerConfigToWrite,
339+
}
340+
: {}),
333341
outputDir: autoConfigDetails.outputDir,
334342
frameworkId: autoConfigDetails.framework.id,
335343
buildCommand: projectCommands.build,
@@ -360,7 +368,7 @@ export async function buildOperationsSummary(
360368

361369
const containsServerSideCode =
362370
// If there is an entrypoint then we know that there is server side code
363-
!!wranglerConfigToWrite.main;
371+
!!wranglerConfigToWrite?.main;
364372

365373
if (
366374
// If there is no server side code, then there is no need to add the cf-typegen script

packages/wrangler/src/autoconfig/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export type AutoConfigOptions = {
6363
export type AutoConfigSummary = {
6464
scripts: Record<string, string>;
6565
wranglerInstall: boolean;
66-
wranglerConfig: RawConfig;
66+
wranglerConfig?: RawConfig;
6767
frameworkConfiguration?: string;
6868
outputDir: string;
6969
frameworkId?: string;

0 commit comments

Comments
 (0)