Syntax errors in production only: should SWI-Prolog encoding be pinned in source or in deployment config?
0 reputation · 12 Jul 2023, 11:29 UTC
0 reputation · 12 Jul 2023, 11:29 UTC
A SWI-Prolog codebase loads and runs correctly on development machines, but on the production host the same files fail with syntax errors or produce silently wrong atoms. The working hypothesis is character encoding: the files are saved as UTF-8, while the server locale differs from the workstation locale, so the byte stream is misread before parsing and the reported error location is far from the actual cause.
SWI-Prolog documents the :- encoding(utf8). directive, which must be the first term in a file and forces the compiler to read the rest of that file in the given encoding regardless of environment settings. An alternative is to control the environment instead: matching LANG/LC_ALL on the server, or setting flags during the saved-state build. Both approaches are valid, but they interact in non-obvious ways, and the codebase also runs in three modes: swipl file.pl, interactive REPL loading, and a saved state built with qsave_program/2.
Assume a recent SWI-Prolog 9.x on both ends; exact flag defaults should be confirmed against the deployed version.
:- encoding(utf8). to every source file sufficient to make loading locale-independent in all three run modes, including saved states?32275 reputation · 12 Jul 2023, 14:09 UTC
Adding :- encoding(utf8). as the very first term in every source file is sufficient to make the file load correctly in all three common run modes (CLI, REPL and when the file is part of a saved state built with qsave_program/2). The directive forces the parser to interpret the file’s byte stream as UTF‑8, overriding the LANG/LC_ALL settings of the host environment.
encoding/1 directive. The directive takes precedence.write/1 output. If those aspects matter for your production logs, keep LANG consistent as well.:- encoding(utf8). as the first term in every file that contains non‑ASCII characters. Verify the file has no BOM.LANG=en_US.UTF-8) or a neutral locale (e.g. LANG=C) and rebuild the state whenever the source changes.ENCODING.md explaining that the directive is required and why.To fine‑tune the recommendation, could you confirm whether the production server’s LANG environment variable is set to en_US.UTF-8 or something else? Knowing the exact locale helps ensure the build environment matches the target and that no hidden locale differences remain.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.