Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

salt-lint crash when piped and having non-beakable space in outputted line #74

Closed
sblaisot opened this issue Oct 23, 2019 · 5 comments · Fixed by #75
Closed

salt-lint crash when piped and having non-beakable space in outputted line #74

sblaisot opened this issue Oct 23, 2019 · 5 comments · Fixed by #75
Labels
Priority: High Type: Bug Something isn't working

Comments

@sblaisot
Copy link
Contributor

sblaisot commented Oct 23, 2019

I just encountered the following:

Traceback (most recent call last):
  File "/home/sblaisot/projets/salt/salt-2017.7.5-python2/bin/salt-lint", line 11, in <module>
    sys.exit(run())
  File "/home/sblaisot/projets/salt/salt-2017.7.5-python2/local/lib/python2.7/site-packages/saltlint/cli.py", line 102, in run
    print(formatter.format(match, config.colored))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 187: ordinal not in range(128)

Not sure now what triggers the problem. I'm inestigating.

It's probably due to a bad character in the sls file + salt-lint output piped to something else which disables pretty color. Not seen it with direct output without pipe.

Will add additionnal information here as soon as I can reproduce it consistently.

@sblaisot
Copy link
Contributor Author

Fun fact:

$ salt-lint file.sls  # no problem
$ salt-lint --nocolor file.sls  # no problem
$ salt-lint file.sls | cat  # problem

@roaldnefs roaldnefs added Type: Bug Something isn't working Priority: High labels Oct 23, 2019
@sblaisot
Copy link
Contributor Author

I reproduced the problem with this sample state:

{%- set environment = salt['pillar.get']("environment", "production") %}

config-file:
    file.managed:
      - name: /path/to/config.file
      - user: root
      - group: root
      - mode: "0644"
      - source: salt://formula/files/config-{{ environment }}

there should be a strange character like a non-breakable space in {{ environment }}. Maybe we can add a test on such strange characters?

$ salt-lint test.sls 
[206] Jinja variables should have spaces before and after: {{ var_name }}
test.sls:9
      - source: salt://formula/files/config-{{ environment }}

$ salt-lint test.sls | cat
Traceback (most recent call last):
  File "/home/sblaisot/projets/salt/salt-2017.7.5-python2/bin/salt-lint", line 11, in <module>
    sys.exit(run())
  File "/home/sblaisot/projets/salt/salt-2017.7.5-python2/local/lib/python2.7/site-packages/saltlint/cli.py", line 102, in run
    print(formatter.format(match, config.colored))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 129: ordinal not in range(128)

@sblaisot
Copy link
Contributor Author

Finally got it
image

it's a non-breakable space (C2A0)
image

@sblaisot sblaisot changed the title salt-lint crash salt-lint crash when pipe and having non-beakable space in outputted line Oct 23, 2019
@sblaisot sblaisot changed the title salt-lint crash when pipe and having non-beakable space in outputted line salt-lint crash when piped and having non-beakable space in outputted line Oct 23, 2019
@roaldnefs
Copy link
Member

Thanks @sblaisot for the detailed bug report!

Adding a rule for such characters is actually a great idea, it will reduces the time having to spend on searching for an invisible bug. In addition to adding a new rule we also have to make sure that salt-lint is able to output the line without raising a UnicodeEncodeError.

This was referenced Oct 23, 2019
roaldnefs added a commit that referenced this issue Oct 27, 2019
Fix encoding to use utf-8 by default in Python 2, the default encoding
in Python 3 is already utf-8. This fix will wrap `sys.stdout` in a
object that automatically encodes an unicode string into utf-8, in
Python 2 only.

An alternative to this would be to set the `PYTHONIOENCODING='utf-8'`
environment variable before running `salt-lint`, but this would require
an extra action on the user side.

The old solution using `str.encode('utf-8')` caused byte string (e.g.
`b''`) output in Python 3...

Fixes #74.

Signed-off-by: Roald Nefs <roald@warpnet.nl>
roaldnefs added a commit that referenced this issue Oct 27, 2019
Fix encoding to use utf-8 by default in Python 2, the default encoding
in Python 3 is already utf-8. This fix will wrap `sys.stdout` in a
object that automatically encodes an unicode string into utf-8, in
Python 2 only.

An alternative to this would be to set the `PYTHONIOENCODING='utf-8'`
environment variable before running `salt-lint`, but this would require
an extra action on the user side.

The old solution using `str.encode('utf-8')` caused byte string (e.g.
`b''`) output in Python 3...

Fixes #74.

Signed-off-by: Roald Nefs <roald@warpnet.nl>
roaldnefs added a commit that referenced this issue Oct 27, 2019
Fix encoding to use utf-8 by default in Python 2, the default encoding
in Python 3 is already utf-8. This fix will wrap `sys.stdout` in a
object that automatically encodes an unicode string into utf-8, in
Python 2 only.

An alternative to this would be to set the `PYTHONIOENCODING='utf-8'`
environment variable before running `salt-lint`, but this would require
an extra action on the user side.

The old solution using `str.encode('utf-8')` caused byte string (e.g.
`b''`) output in Python 3...

Fixes #74.

Signed-off-by: Roald Nefs <roald@warpnet.nl>
roaldnefs added a commit that referenced this issue Oct 27, 2019
Fix encoding to use utf-8 by default in Python 2, the default encoding
in Python 3 is already utf-8. This fix will wrap `sys.stdout` in a
object that automatically encodes an unicode string into utf-8, in
Python 2 only.

An alternative to this would be to set the `PYTHONIOENCODING='utf-8'`
environment variable before running `salt-lint`, but this would require
an extra action on the user side.

The old solution using `str.encode('utf-8')` caused byte string (e.g.
`b''`) output in Python 3...

Fixes #74.

Signed-off-by: Roald Nefs <roald@warpnet.nl>
@roaldnefs
Copy link
Member

@sblaisot, the solution I suggested seems to break the output in Python 3... The new solution in #84 will wrap sys.stdout in a object that automatically encodes an unicode string into utf-8, in Python 2 only.

For more information see: #84.

This was referenced Nov 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: High Type: Bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants