Skip to content

[BUG] file.recurse with clean: True doesn't process requirements correctly #61042

Closed

Description

@dkacar-oradian

Description
Under some circumstances file.recurse can throw an exception or fail to act in an expected way. More details below.

Setup
We want to use two file.recurse states to copy files from two directories on the Salt master into the same directory on the minion. We also want to use clean: True in the last file.recurse to ensure that the minion directory has no other files.

In the master file root set up the files to be copied:

mkdir aaa bbb
cd aaa
touch a1 a2 a3
cd ../bbb
touch b1 b2

This is test.sls file which works fine:

destdir:
  file.directory:
    - name: /tmp/destabab

aaa:
  file.recurse:
    - name: /tmp/destabab
    - source: salt://aaa
    - require:
      - destdir

bbb:
  file.recurse:
    - name: /tmp/destabab
    - source: salt://bbb
    - clean: True
    - require:
      - file: aaa

This is the result if I run it:

state.apply output
# salt vdave-repo-02.xdc state.apply test
vdave-repo-02.xdc:
----------
          ID: destdir
    Function: file.directory
        Name: /tmp/destabab
      Result: True
     Comment: 
     Started: 16:33:12.174130
    Duration: 21.743 ms
     Changes:   
              ----------
              /tmp/destabab:
                  ----------
                  directory:
                      new
----------
          ID: aaa
    Function: file.recurse
        Name: /tmp/destabab
      Result: True
     Comment: Recursively updated /tmp/destabab
     Started: 16:33:12.196896
    Duration: 510.601 ms
     Changes:   
              ----------
              /tmp/destabab/a1:
                  ----------
                  diff:
                      New file
                  mode:
                      0644
              /tmp/destabab/a2:
                  ----------
                  diff:
                      New file
                  mode:
                      0644
              /tmp/destabab/a3:
                  ----------
                  diff:
                      New file
                  mode:
                      0644
----------
          ID: bbb
    Function: file.recurse
        Name: /tmp/destabab
      Result: True
     Comment: Recursively updated /tmp/destabab
     Started: 16:33:12.711482
    Duration: 493.767 ms
     Changes:   
              ----------
              /tmp/destabab/b1:
                  ----------
                  diff:
                      New file
                  mode:
                      0644
              /tmp/destabab/b2:
                  ----------
                  diff:
                      New file
                  mode:
                      0644

Summary for vdave-repo-02.xdc
------------
Succeeded: 3 (changed=3)
Failed:    0
------------
Total states run:     3
Total run time:   1.026 s

Steps to Reproduce the behavior

There are a few things which don't work.

  1. Change id aaa to file_aaa for the first file.recurse and also add it in the require for the second file.recurse as:
- require:
  - file_aaa

I'm getting an exception:

----------
          ID: bbb
    Function: file.recurse
        Name: /tmp/destabab
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python3.6/site-packages/salt/state.py", line 2172, in call
                  *cdata["args"], **cdata["kwargs"]
                File "/usr/lib/python3.6/site-packages/salt/loader.py", line 1241, in __call__
                  return self.loader.run(run_func, *args, **kwargs)
                File "/usr/lib/python3.6/site-packages/salt/loader.py", line 2274, in run
                  return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
                File "/usr/lib/python3.6/site-packages/contextvars/__init__.py", line 38, in run
                  return callable(*args, **kwargs)
                File "/usr/lib/python3.6/site-packages/salt/loader.py", line 2289, in _run_as
                  return _func_or_method(*args, **kwargs)
                File "/usr/lib/python3.6/site-packages/salt/loader.py", line 2322, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python3.6/site-packages/salt/states/file.py", line 4303, in recurse
                  keep.update(_gen_keep_files(name, require))
                File "/usr/lib/python3.6/site-packages/salt/states/file.py", line 606, in _gen_keep_files
                  if low["name"] == comp["file"] or low["__id__"] == comp["file"]:
              TypeError: string indices must be integers
     Started: 16:43:15.032890
    Duration: 262.888 ms
     Changes:   
  1. Change the ID back to aaa, but specify the require directive as:
- require:
  - aaa

This will execute, but it won't work as intended because it will delete files created by the first file.recurse. Apparently, files from another state cannot be retained if only an ID is supplied. That, I think, is a bug also.

state.apply output
# salt vdave-repo-02.xdc state.apply test
vdave-repo-02.xdc:
----------
          ID: destdir
    Function: file.directory
        Name: /tmp/destabab
      Result: True
     Comment: The directory /tmp/destabab is in the correct state
     Started: 16:45:26.360640
    Duration: 21.201 ms
     Changes:   
----------
          ID: aaa
    Function: file.recurse
        Name: /tmp/destabab
      Result: True
     Comment: The directory /tmp/destabab is in the correct state
     Started: 16:45:26.382810
    Duration: 336.091 ms
     Changes:   
----------
          ID: bbb
    Function: file.recurse
        Name: /tmp/destabab
      Result: True
     Comment: Recursively updated /tmp/destabab
     Started: 16:45:26.719873
    Duration: 463.596 ms
     Changes:   
              ----------
              removed:
                  - /tmp/destabab/a2
                  - /tmp/destabab/a3
                  - /tmp/destabab/a1

Summary for vdave-repo-02.xdc
------------
Succeeded: 3 (changed=1)
Failed:    0
------------
Total states run:     3
Total run time: 820.888 ms

Expected behavior

  1. Exception should not be raised if required id contains substring file.
  2. Files from other states should be retained even if the require directive contains just state id (not prefixed with file:)
  3. Retaining files from other states is kind of documented in the description for the clean parameter: "Make sure that only files that are set up by salt and required by this function are kept. If this option is set then everything in this directory will be deleted unless it is required." The wording should be improved because it's very easy to miss this feature. I would also advise adding an example with two file.recurse states.

Screenshots
If applicable, add screenshots to help explain your problem.

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)
> salt-minion --versions-report
Salt Version:
          Salt: 3003.3
 
Dependency Versions:
          cffi: Not Installed
      cherrypy: Not Installed
      dateutil: Not Installed
     docker-py: Not Installed
         gitdb: Not Installed
     gitpython: Not Installed
        Jinja2: 2.11.1
       libgit2: Not Installed
      M2Crypto: 0.35.2
          Mako: Not Installed
       msgpack: 0.6.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: Not Installed
      pycrypto: 2.6.1
  pycryptodome: Not Installed
        pygit2: Not Installed
        Python: 3.6.8 (default, Nov 16 2020, 16:55:22)
  python-gnupg: Not Installed
        PyYAML: 3.13
         PyZMQ: 17.0.0
         smmap: Not Installed
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.1.4
 
System Versions:
          dist: centos 7 Core
        locale: ISO-8859-2
       machine: x86_64
       release: 4.4.169-1.el7.elrepo.x86_64
        system: Linux
       version: CentOS Linux 7 Core

Additional context
The trouble seems to be in _gen_keep_files function in states/file.py. It has:

    keep = set()
    if isinstance(require, list):
        required_files = [comp for comp in require if "file" in comp]

That's responsible for the exception and the code below that is responsible for not honoring plain ID.

Activity

  1. added
    bug
    on Oct 13, 2021
  2. waynew commented on Oct 13, 2021

    @waynew
    Contributor

    @dkacar-oradian thanks for the report! I'm OOO the rest of the week, but I'll take a look into this when I get back, looks like it should be straightforward to reproduce with your report 👍

  3. added
    Confirmed
    and removed on Oct 18, 2021
  4. added this to the Approved milestone on Oct 18, 2021
  5. waynew commented on Oct 18, 2021

    @waynew
    Contributor

    Thanks again! I was able to confirm this, with ease due to your complete MCVE.

    That additional context looks to be the interesting bit, and it does look like the problem is specifically related to looking for file in comp. aaa does in fact fail. Changing the state ID to aaa_file will also error out. I'm guessing that function should be checking the type as well in if "file" in comp. There may be some other better way to approach that I didn't see at a glance, but ultimately that piece does need to be written a little better 👍

  6. removed their assignment
    on Oct 18, 2021
  7. added 2 commits that reference this issue on Jul 10, 2026
    02d61e5
    2dcef28
  8. added a commit that references this issue on Aug 4, 2026
    2d8d831
  9. added a commit that references this issue on Oct 27, 2026
    be340cc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Confirmedbug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions