← Back to Pull requests

Write Dockerfiles and build-context files as UTF-8 #23922

Closed
unknown closed this pull request without merging on Nov 15, 2026 16:04 UTC
View on GitHub
unknown unknown commented on Nov 15, 2026 14:20 UTC
Author

What this does

ImageBuilder wrote the build context and the Dockerfile with the platform default encoding: Path.write_text(text) in write_text(), and open("w") in build() and generate_default_dockerfile(). On a host whose locale is not UTF-8 (cp936/cp1252 on Windows) a context file containing a character the locale cannot encode raises UnicodeEncodeError, and the hashed file is left in the context empty, so the Dockerfile generated afterwards references a truncated file.

This passes encoding="utf-8" to those three writes. The bytes no longer depend on the host locale, which is what the Dockerfile format expects and what Linux CI produces.

Reproduction (before)

Windows 11, locale.getpreferredencoding(False) == "cp936":

import tempfile
from pathlib import Path

from prefect.utilities.dockerutils import ImageBuilder

builder = ImageBuilder(base_image="python:3.11", context=Path(tempfile.mkdtemp()))
builder.write_text("print('emoji 🔥')\n", "app.py")

UnicodeEncodeError: 'gbk' codec can't encode character '\U0001f525' in position 13: illegal multibyte sequence

and the context directory keeps the empty .<sha256> file.

After the change the same call writes the file and the test reads it back as UTF-8.

Tests

tests/utilities/test_dockerutils.py now covers the three writes this PR changes: ImageBuilder.write_text(), ImageBuilder.build() and generate_default_dockerfile().

The tests run in-process. A fixture wraps pathlib.Path.write_text and Path.open so a call that omits encoding writes as ASCII, reproducing the cp936/cp1252 failure on any machine, while an explicit encoding is passed through untouched. Each test writes non-ASCII content (the emoji, and a non-ASCII context directory for the default Dockerfile) and asserts the file on disk is UTF-8.

Mutation check: removing the three encoding="utf-8" arguments makes all three tests fail with UnicodeEncodeError: 'ascii' codec can't encode ...; restoring them passes the file (19 passed).

The earlier child-process test under LC_ALL=C was removed rather than patched again. It did pass on CI here, so the reported child import failure was not reproduced; it was dropped because it could only work while the child process stayed importable and ASCII-clean, which is exactly the environment dependency this test should not have.

Fixes #23921

Diff: 304 lines (+228 / -76) across 3 files Reviewer: simin-yuan Review duration: 103m
All Vouch review confidence checks passed
Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 304 lines diff across 3 files
0.40
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.342
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #23922"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 40.1% (MEDIUM) · Higher = larger blast radius
40.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 15.9% · Safe to merge
15.9%