Repository navigation
Closed
Labels
bugbroken, incorrect, or confusing behaviorseverity-low4th level, cosemtic problems, work around exists
Milestone
Description
Description of Issue/Question
Using file.append state on a file with an encoding that's incompatible with the system encoding fails.
For example, I have a file containing ISO-8859-1 encoded data. The system encoding is ASCII. If I try to use file.append, it fails with the following error:
An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/state.py", line 1944, in call
**cdata['kwargs'])
File "/usr/lib/python3/dist-packages/salt/loader.py", line 1959, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/salt/states/file.py", line 5669, in append
slines = slines.decode(__salt_system_encoding__)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 4: ordinal not in range(128)
If the system encoding were UTF-8, it would similarly fail because 0xed is not a valid UTF-8 byte.
In the simplest case, it would be helpful if the state.file functions that work with existing files could take an encoding, much as managed files do.
Setup
See attached zip file with Docker config that reproduces the bug
Steps to Reproduce Issue
- Build attached config:
docker build salt-bug -t salt-bug:latest - Run configuration:
docker run -it --rm salt-bug:latest
Output:
[ERROR ] An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/state.py", line 1944, in call
**cdata['kwargs'])
File "/usr/lib/python3/dist-packages/salt/loader.py", line 1959, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/salt/states/file.py", line 5669, in append
slines = slines.decode(__salt_system_encoding__)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 4: ordinal not in range(128)
local:
----------
ID: /bugfile
Function: file.append
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/salt/state.py", line 1944, in call
**cdata['kwargs'])
File "/usr/lib/python3/dist-packages/salt/loader.py", line 1959, in wrapper
return f(*args, **kwargs)
File "/usr/lib/python3/dist-packages/salt/states/file.py", line 5669, in append
slines = slines.decode(__salt_system_encoding__)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 4: ordinal not in range(128)
Started: 21:35:25.452802
Duration: 39.99999999996362 ms
Changes:
Summary for local
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 40.000 ms
Versions Report
Salt Version:
Salt: 2018.3.0-n/a-501caa9
Dependency Versions:
cffi: Not Installed
cherrypy: Not Installed
dateutil: Not Installed
docker-py: Not Installed
gitdb: Not Installed
gitpython: Not Installed
Jinja2: 2.9.4
libgit2: Not Installed
M2Crypto: Not Installed
Mako: Not Installed
msgpack-pure: Not Installed
msgpack-python: 0.4.8
mysql-python: Not Installed
pycparser: Not Installed
pycrypto: 2.6.1
pycryptodome: Not Installed
pygit2: Not Installed
Python: 3.5.3 (default, Sep 27 2018, 17:25:39)
python-gnupg: Not Installed
PyYAML: 3.12
PyZMQ: 16.0.2
smmap: Not Installed
timelib: Not Installed
Tornado: 4.4.3
ZMQ: 4.2.1
System Versions:
dist: debian 9.6
locale: ANSI_X3.4-1968
machine: x86_64
release: 4.9.125-linuxkit
system: Linux
version: debian 9.6
Reacted by Joonho Syn and xu0o0
Metadata
Metadata
Assignees
Labels
bugbroken, incorrect, or confusing behaviorseverity-low4th level, cosemtic problems, work around exists
Activity
dwoz commentedon Dec 19, 2018
I've been wondering if this issue would come up. I think we need to add an encoding parameter to most of the file states.
stale commentedon Jan 9, 2020
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.
calmzhu commentedon Jan 10, 2020
The same problem occured in me when run state module file.replace.

Currently fix this with a walkaround, diy salt builtin utils.files.py like this
Is there any other way to do this without need to change salt builtin modules.
stale commentedon Jan 10, 2020
Thank you for updating this issue. It is no longer marked as stale.
LHozzan commentedon Sep 2, 2020
Problem is still persistant for me in our development environment:
Error:
State:
Rendered data:
Version info:
File info:
Aditional info: (maybe hotfix)
salt-callcommand directly on machinesalt '...'commandIf you need aditional information, please ask.
Add encoding and encoding_errors params to file comment/append/prepend
Fix file state UnicodeDecodeError on encoding mismatch
Add encoding and encoding_errors params to file comment/append/prepend
Fix file state UnicodeDecodeError on encoding mismatch
Add encoding and encoding_errors params to file comment/append/prepend
Fix file state UnicodeDecodeError on encoding mismatch
Add encoding and encoding_errors params to file comment/append/prepend
states/file: _read_file_text honor encoding_errors on BOM + fallback …