Skip to content

Commit b4dcbc1

Browse files
dom96nikitassharma
authored andcommitted
Read Python packages from cf-requirements.txt. (cloudflare#9886)
1 parent 346be56 commit b4dcbc1

File tree

7 files changed

+31
-17
lines changed

7 files changed

+31
-17
lines changed

.changeset/cute-eagles-exist.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"workers-playground": minor
3+
"wrangler": minor
4+
---
5+
6+
Python packages are now read from cf-requirements.txt instead of requirements.txt by default

packages/workers-playground/generate-default-hashes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ const pythonWorker = async () => {
3737
);
3838

3939
worker.set(
40-
"requirements.txt",
41-
new Blob([await readFile("./welcome/requirements.txt", "utf8")], {
40+
"cf-requirements.txt",
41+
new Blob([await readFile("./welcome/cf-requirements.txt", "utf8")], {
4242
type: "text/plain",
4343
}),
44-
"requirements.txt"
44+
"cf-requirements.txt"
4545
);
4646
return worker;
4747
};

packages/workers-playground/src/QuickEditor/defaultHashes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default {
33
contentType:
44
"multipart/form-data; boundary=----formdata-88e2b909-318c-42df-af0d-9077f33c7988",
55
worker:
6-
'------formdata-88e2b909-318c-42df-af0d-9077f33c7988\r\nContent-Disposition: form-data; name="metadata"\r\n\r\n{"main_module":"index.py","compatibility_date":"$REPLACE_COMPAT_DATE","compatibility_flags":["python_workers"]}\r\n------formdata-88e2b909-318c-42df-af0d-9077f33c7988\r\nContent-Disposition: form-data; name="index.py"; filename="index.py"\r\nContent-Type: text/x-python\r\n\r\nfrom js import Response\nimport numpy as np\n\ndef on_fetch(request):\n print("Hi there!")\n arr = np.array([1, 2, 3])\n return Response.new(str(arr))\n\r\n------formdata-88e2b909-318c-42df-af0d-9077f33c7988\r\nContent-Disposition: form-data; name="requirements.txt"; filename="requirements.txt"\r\nContent-Type: text/plain\r\n\r\nnumpy\r\n------formdata-88e2b909-318c-42df-af0d-9077f33c7988--',
6+
'------formdata-88e2b909-318c-42df-af0d-9077f33c7988\r\nContent-Disposition: form-data; name="metadata"\r\n\r\n{"main_module":"index.py","compatibility_date":"$REPLACE_COMPAT_DATE","compatibility_flags":["python_workers"]}\r\n------formdata-88e2b909-318c-42df-af0d-9077f33c7988\r\nContent-Disposition: form-data; name="index.py"; filename="index.py"\r\nContent-Type: text/x-python\r\n\r\nfrom js import Response\nimport numpy as np\n\ndef on_fetch(request):\n print("Hi there!")\n arr = np.array([1, 2, 3])\n return Response.new(str(arr))\n\r\n------formdata-88e2b909-318c-42df-af0d-9077f33c7988\r\nContent-Disposition: form-data; name="cf-requirements.txt"; filename="cf-requirements.txt"\r\nContent-Type: text/plain\r\n\r\nnumpy\r\n------formdata-88e2b909-318c-42df-af0d-9077f33c7988--',
77
},
88
"/": {
99
contentType:

packages/workers-playground/src/QuickEditor/useDraftWorker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,15 @@ export function serialiseWorker(service: PartialWorker): FormData {
7575
const entrypointModule = typedModules.find(
7676
(m) => m.name === service.entrypoint
7777
);
78-
// Try to find a requirements.txt file
78+
// Try to find a cf-requirements.txt file
7979
const isPythonEntrypoint = entrypointModule?.type === "python";
8080

8181
if (isPythonEntrypoint) {
8282
try {
83-
const pythonRequirements = service.modules["requirements.txt"];
83+
const pythonRequirements = service.modules["cf-requirements.txt"];
8484
if (pythonRequirements) {
8585
const textContent = decoder.decode(pythonRequirements.contents);
86-
// This is incredibly naive. However, it supports common syntax for requirements.txt
86+
// This is incredibly naive. However, it supports common syntax for cf-requirements.txt
8787
for (const requirement of textContent.split("\n")) {
8888
const packageName = requirement.match(/^[^\d\W]\w*/);
8989
if (typeof packageName?.[0] === "string") {
@@ -98,10 +98,10 @@ export function serialiseWorker(service: PartialWorker): FormData {
9898
}
9999
}
100100
}
101-
// We don't care if a requirements.txt isn't found
101+
// We don't care if a cf-requirements.txt isn't found
102102
} catch (e) {
103103
console.debug(
104-
"Python entrypoint detected, but no requirements.txt file found."
104+
"Python entrypoint detected, but no cf-requirements.txt file found."
105105
);
106106
}
107107
}

packages/wrangler/src/deployment-bundle/find-additional-modules.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { existsSync } from "node:fs";
12
import { mkdir, readdir, readFile, writeFile } from "node:fs/promises";
23
import path from "node:path";
34
import chalk from "chalk";
@@ -93,21 +94,28 @@ export async function findAdditionalModules(
9394
name: m.name,
9495
}));
9596

96-
// Try to find a requirements.txt file
97+
// Try to find a cf-requirements.txt file
9798
const isPythonEntrypoint =
9899
getBundleType(entry.format, entry.file) === "python";
99100

100101
if (isPythonEntrypoint) {
101102
let pythonRequirements = "";
102103
try {
103104
pythonRequirements = await readFile(
104-
path.resolve(entry.projectRoot, "requirements.txt"),
105+
path.resolve(entry.projectRoot, "cf-requirements.txt"),
105106
"utf-8"
106107
);
107108
} catch (e) {
108-
// We don't care if a requirements.txt isn't found
109+
// We don't care if a cf-requirements.txt isn't found
109110
logger.debug(
110-
"Python entrypoint detected, but no requirements.txt file found."
111+
"Python entrypoint detected, but no cf-requirements.txt file found."
112+
);
113+
}
114+
115+
// If a `requirements.txt` file is found, show a warning instructing user to use `cf-requirements.txt` instead.
116+
if (existsSync(path.resolve(entry.projectRoot, "requirements.txt"))) {
117+
logger.warn(
118+
"Found a `requirements.txt` file. Python requirements should now be in a `cf-requirements.txt` file."
111119
);
112120
}
113121

@@ -117,7 +125,7 @@ export async function findAdditionalModules(
117125
}
118126
if (!isValidPythonPackageName(requirement)) {
119127
throw new UserError(
120-
`Invalid Python package name "${requirement}" found in requirements.txt. Note that requirements.txt should contain package names only, not version specifiers.`
128+
`Invalid Python package name "${requirement}" found in cf-requirements.txt. Note that cf-requirements.txt should contain package names only, not version specifiers.`
121129
);
122130
}
123131

packages/wrangler/src/deployment-bundle/guess-worker-format.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default async function guessWorkerFormat(
2424
`The entrypoint ${path.relative(
2525
process.cwd(),
2626
entryFile
27-
)} defines a Python worker, support for Python workers is currently experimental. Python workers with a requirements.txt file can only be run locally and cannot be deployed.`
27+
)} defines a Python worker, support for Python workers is currently experimental. Python workers with a cf-requirements.txt file can only be run locally and cannot be deployed.`
2828
);
2929
return { format: "modules", exports: [] };
3030
}

packages/wrangler/src/dev/use-esbuild.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ export function runBuild(
184184
// trigger "builds" when any change
185185
if (noBundle) {
186186
const watching = [path.resolve(entry.moduleRoot)];
187-
// Check whether we need to watch a Python requirements.txt file.
187+
// Check whether we need to watch a Python cf-requirements.txt file.
188188
const watchPythonRequirements =
189189
getBundleType(entry.format, entry.file) === "python"
190-
? path.resolve(entry.projectRoot, "requirements.txt")
190+
? path.resolve(entry.projectRoot, "cf-requirements.txt")
191191
: undefined;
192192

193193
if (watchPythonRequirements) {

0 commit comments

Comments
 (0)