diff --git a/.changeset/lucky-points-remain.md b/.changeset/lucky-points-remain.md new file mode 100644 index 000000000000..ddd0cb9c3ec3 --- /dev/null +++ b/.changeset/lucky-points-remain.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +fix: Show a clearer user error when trying to use a python worker without the `python_workers` compatibility flag specified diff --git a/packages/wrangler/src/__tests__/configuration.test.ts b/packages/wrangler/src/__tests__/configuration.test.ts index 1e852e0ee260..16e5c81c4ade 100644 --- a/packages/wrangler/src/__tests__/configuration.test.ts +++ b/packages/wrangler/src/__tests__/configuration.test.ts @@ -1,6 +1,9 @@ import path from "node:path"; +import { readConfig } from "../config"; import { normalizeAndValidateConfig } from "../config/validation"; import { normalizeSlashes } from "./helpers/mock-console"; +import { runInTempDir } from "./helpers/run-in-tmp"; +import { writeWranglerToml } from "./helpers/write-wrangler-toml"; import type { ConfigFields, RawConfig, @@ -8,6 +11,40 @@ import type { RawEnvironment, } from "../config"; +describe("readConfig()", () => { + runInTempDir(); + it("should not error if a python entrypoint is used with the right compatibility_flag", () => { + writeWranglerToml({ + main: "index.py", + compatibility_flags: ["python_workers"], + }); + const config = readConfig("wrangler.toml", {}); + expect(config.rules).toMatchInlineSnapshot(` + Array [ + Object { + "globs": Array [ + "**/*.py", + ], + "type": "PythonModule", + }, + ] + `); + }); + it("should error if a python entrypoint is used without the right compatibility_flag", () => { + writeWranglerToml({ + main: "index.py", + }); + try { + readConfig("wrangler.toml", {}); + expect.fail(); + } catch (e) { + expect(e).toMatchInlineSnapshot( + `[Error: The \`python_workers\` compatibility flag is required to use Python.]` + ); + } + }); +}); + describe("normalizeAndValidateConfig()", () => { it("should use defaults for empty configuration", () => { const { config, diagnostics } = normalizeAndValidateConfig({}, undefined, { diff --git a/packages/wrangler/src/__tests__/deploy.test.ts b/packages/wrangler/src/__tests__/deploy.test.ts index 626591916caa..43000900e2a0 100644 --- a/packages/wrangler/src/__tests__/deploy.test.ts +++ b/packages/wrangler/src/__tests__/deploy.test.ts @@ -10797,6 +10797,7 @@ export default{ it("should upload python module defined in wrangler.toml", async () => { writeWranglerToml({ main: "index.py", + compatibility_flags: ["python_workers"], }); await fs.promises.writeFile( "index.py", @@ -10835,7 +10836,9 @@ export default{ }); it("should upload python module specified in CLI args", async () => { - writeWranglerToml(); + writeWranglerToml({ + compatibility_flags: ["python_workers"], + }); await fs.promises.writeFile( "index.py", "from js import Response;\ndef fetch(request):\n return Response.new('hello')" diff --git a/packages/wrangler/src/config/index.ts b/packages/wrangler/src/config/index.ts index 3248ea08aa4f..6d1c400d5141 100644 --- a/packages/wrangler/src/config/index.ts +++ b/packages/wrangler/src/config/index.ts @@ -147,6 +147,15 @@ export function readConfig( } } + applyPythonConfig(config, args); + + return config; +} + +/** + * Modifies the provided config to support python workers, if the entrypoint is a .py file + */ +function applyPythonConfig(config: Config, args: ReadConfigCommandArgs) { const mainModule = "script" in args ? args.script : config.main; if (typeof mainModule === "string" && mainModule.endsWith(".py")) { // Workers with a python entrypoint should have bundling turned off, since all of Wrangler's bundling is JS/TS specific @@ -156,9 +165,12 @@ export function readConfig( if (!config.rules.some((rule) => rule.type === "PythonModule")) { config.rules.push({ type: "PythonModule", globs: ["**/*.py"] }); } + if (!config.compatibility_flags.includes("python_workers")) { + throw new UserError( + "The `python_workers` compatibility flag is required to use Python." + ); + } } - - return config; } /**