Skip to content

Commit

Permalink
Show a clearer user error when trying to use a python worker without …
Browse files Browse the repository at this point in the history
…the `python_workers` compatibility flag specified (#6572)
  • Loading branch information
penalosa authored Aug 27, 2024
1 parent 1ce82ea commit 0d83428
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-points-remain.md
Original file line number Diff line number Diff line change
@@ -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
37 changes: 37 additions & 0 deletions packages/wrangler/src/__tests__/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
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,
RawDevConfig,
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, {
Expand Down
5 changes: 4 additions & 1 deletion packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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')"
Expand Down
16 changes: 14 additions & 2 deletions packages/wrangler/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit 0d83428

Please sign in to comment.