saltstack / saltstack/salt

[Bug]: Extra newline at end of output with Jinja template

Open
#69,821 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug needs-triage
Dominant language
Python
Stars
15.7k
Forks
5.6k
Avg merge
2d 44m
Merged PRs (30d)
80

Description

What happened?

Even with trim_blocks=True and lstrip_blocks=True, a template like this will result in not one, but two newlines at the end of the generated output:

{% for line in lines %}
{{ line }}
{% endfor %}

With lines = ['a', 'b', 'c'], this will generate:

a\n
b\n
c\n
\n

I'd expect the output to end with a single newline instead of two.

If there is content after {% endfor %}, no additional newline will be generated, so this template:

{% for line in lines %}
{{ line }}
{% endfor %}
x

will generate this output, which is what I'd expect:

a\n
b\n
c\n
x\n
Type of salt install

Official rpm

Major version

3007.x

What supported OS are you seeing the problem on? Can select multiple. (If bug appears on an unsupported OS, please open a GitHub Discussion instead)

rhel-10

salt --versions-report output
Salt Version:
          Salt: 3008.2
 
Python Version:
        Python: 3.14.6 (main, Jun 11 2026, 02:19:05) [GCC 11.2.0]
 
Dependency Versions:
          cffi: 2.0.0
      cherrypy: 18.10.0
  cryptography: 48.0.0
      dateutil: 2.9.0.post0
     docker-py: Not Installed
         gitdb: 4.0.12
     gitpython: 3.1.50
        Jinja2: 3.1.6
       libgit2: Not Installed
  looseversion: 1.3.0
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.1.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     packaging: 24.0
     pycparser: 3.00
      pycrypto: Not Installed
  pycryptodome: 3.23.0
        pygit2: Not Installed
  python-gnupg: 0.5.6
        PyYAML: 6.0.3
         PyZMQ: 27.1.0
        relenv: 0.22.14
         smmap: 5.0.2
       timelib: 0.3.0
       Tornado: 6.5.7
           ZMQ: 4.3.5
 
Salt Package Information:
  Package Type: onedir
 
System Versions:
          dist: rhel 10.2 Coughlan
        locale: utf-8
       machine: x86_64
       release: 6.12.0-211.30.1.el10_2.x86_64
        system: Linux
       version: Red Hat Enterprise Linux 10.2 Coughlan

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the reported Jinja template with trim_blocks=True and lstrip_blocks=True in Salt's rendering path. Trace how the template output is assembled, add a regression test for a single trailing newline, and verify that content after the loop remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.