pyinfra icon indicating copy to clipboard operation
pyinfra copied to clipboard

FileContents does not return None if file does not exist

Open NichtJens opened this issue 3 months ago • 3 comments

Describe the bug

The docs of FileContents say:

Returns None if the file doest not exist.

However, it actually errors and stops the script.

(Also, note the "doest" typo.)

To Reproduce

from pyinfra import host
from pyinfra.facts.files import FileContents

host.get_fact(FileContents, "doesnotexist")
pyinfra localhost test.py
--> Preparing operation files...
    Loading: test.py
    [localhost] cat: doesnotexist: No such file or directory
    [localhost] Error: could not load fact: files.FileContents path=doesnotexist

Expected behavior

The current behavior could be correct in the sense that asking for the contents of a file that does not exist may be an error. OTOH, returning None for non-existent files also makes sense. So:

  • either the documentation should be corrected, or
  • it should return None if the file does not exist.

The typo ("doest") should be fixed. Sha1File has the same typo.

Meta

  • Include output of pyinfra --support.
  • How was pyinfra installed (source/pip)?
  • Include pyinfra-debug.log (if one was created)
  • Consider including output with -vv and --debug.

NichtJens avatar Sep 20 '25 20:09 NichtJens

Fixed, should be ok

wowi42 avatar Sep 25 '25 13:09 wowi42

I left a comment in #1461.

NichtJens avatar Sep 25 '25 15:09 NichtJens

Fixed, I think you were right

wowi42 avatar Sep 26 '25 07:09 wowi42