From ecb4a17cc4c318b837aeaf66affd819c20707906 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 6 Nov 2026 12:51:59 +0100 Subject: [PATCH] feat(init): set PYTHONUTF8=1 for Python services On Windows a native Python process opens text files in the ANSI code page (cp1252) unless the code passes `encoding=`; in a container the same code reads UTF-8. `devstack init` now gives each Django, FastAPI and Flask service `PYTHONUTF8 = "1"` in its env, and the stack file reference and the devstack-config skill name the pitfall. Closes #464 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 10 ++++++++++ crates/devstack/src/init.rs | 20 +++++++++++++++++++- docs/stack-file.md | 4 ++++ skills/devstack-config/SKILL.md | 4 ++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e3d6a012..8264528e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,16 @@ and listed with their assets on the ## [Unreleased] +### Added + +- **`devstack init` sets `PYTHONUTF8 = "1"` for Python apps.** On Windows a + native Python process opens text files in the ANSI code page (cp1252) + unless the code passes `encoding=`, while the same code in a container + reads UTF-8: UTF-8 fixtures turned into mojibake only under devstack. The + draft now gives each Django, FastAPI and Flask service the variable in its + `env`, and the stack file reference and the `devstack-config` skill name + the pitfall (#464). + ### Changed - **devstack finds the stack file from any folder of the repository.** diff --git a/crates/devstack/src/init.rs b/crates/devstack/src/init.rs index 9df67d09..424daf9c 100644 --- a/crates/devstack/src/init.rs +++ b/crates/devstack/src/init.rs @@ -490,6 +490,13 @@ fn python(repo: &Path, dir: &Path, all: &[PathBuf]) -> Option { } else { return None; } + // On Windows, Python opens text files in the ANSI code page (cp1252) + // unless told otherwise; in a container the same code reads UTF-8 (#464). + let _ = writeln!(b, "\n[services.{name}.env]"); + let _ = writeln!( + b, + "PYTHONUTF8 = \"1\" # files as UTF-8, as on Linux (Windows reads them as cp1252 otherwise)" + ); Some(Svc { name, body: b }) } @@ -1002,7 +1009,18 @@ mod tests { "{}", d.text ); - devstack_config::from_str(&d.text, &r.join("devstack.toml")).expect("the draft loads"); + let stack = + devstack_config::from_str(&d.text, &r.join("devstack.toml")).expect("the draft loads"); + // Python reads files as UTF-8 on Windows too (#464); nothing else gets it. + for n in ["api", "admin"] { + assert_eq!( + stack.services[n].env.get("PYTHONUTF8").map(String::as_str), + Some("1"), + "{n}\n{}", + d.text + ); + } + assert!(!stack.services["orders"].env.contains_key("PYTHONUTF8")); } #[test] diff --git a/docs/stack-file.md b/docs/stack-file.md index 98d71d70..15fb2edb 100644 --- a/docs/stack-file.md +++ b/docs/stack-file.md @@ -449,6 +449,10 @@ Telemetry__Endpoint = "http://127.0.0.1:4317" telemetry. - On Windows, variable names that differ only in case are one variable: the later layer wins, and `${PATH}` finds `Path`. +- On Windows, Python opens text files in the ANSI code page (cp1252) unless + the code passes `encoding=`; in a container the same code reads UTF-8. Give + native Python services and commands `PYTHONUTF8 = "1"` in `env` (harmless + on Linux and macOS); `devstack init` writes it for the Python apps it finds. - `rewrite_hosts` needs `compose`; each entry is a service the service's own group starts, or `.` of another. `workers.env` names at least one variable. diff --git a/skills/devstack-config/SKILL.md b/skills/devstack-config/SKILL.md index 70e69b89..4b250b7a 100644 --- a/skills/devstack-config/SKILL.md +++ b/skills/devstack-config/SKILL.md @@ -49,6 +49,10 @@ without a stack file starts from `devstack init` (a draft with TODOs). instance, only one of them may run schedulers and queue consumers: put their switches into `[services..workers] env = { X = "{is_main}" }`, not into `env`. +- **Python on Windows reads files as cp1252.** A native Python service or + command opens text files in the ANSI code page unless the code passes + `encoding=`, so UTF-8 fixtures turn into mojibake only under devstack. Set + `PYTHONUTF8 = "1"` in its `env`. - **Lanes have their own containers.** A worktree runs as a lane with its ports moved (`[lanes] port_step`) and its own Compose project, so its database is its own and needs no `{lane_suffix}`. Every lane starts all of