thefuck icon indicating copy to clipboard operation
thefuck copied to clipboard

subprocess.run does not work inside side_effect

Open tnyeanderson opened this issue 9 months ago • 0 comments

For some reason, using subprocess.run() inside of a side_effect function doesn't seem to work. Take the following, which demonstrates the expected behavior:

$ cat /tmp/subprocess_raw.py
import subprocess
print(subprocess.__file__)
subprocess.run(["which", "free"])

$ python3 /tmp/subprocess_raw.py
/usr/lib/python3.11/subprocess.py
/usr/bin/free

When the same subprocess.run(["which", "free"]) call is made from inside a side_effect function, the free command is somehow executed instead of which free!

The output of thefuck --version (something like The Fuck 3.1 using Python 3.5.0 and Bash 4.4.12(1)-release):

The Fuck 3.32 using Python 3.11.4 and Bash 5.2.15(1)-release

Your system (Debian 7, ArchLinux, Windows, etc.):

Ubuntu 23.04

How to reproduce the bug:

Create ~/.config/thefuck/rules/subprocess_test.py, which is a very simple rule which basically tries to run which free as a side effect:

import subprocess
from thefuck import logs

priority = 0
requires_output = True
enabled_by_default = True

def match(command):
    return True

def get_new_command(command):
    return 'echo "--- running subprocess check --"'

def side_effect(command, fixed_command):
    logs.debug(subprocess.__file__)
    subprocess.run(["which", "free"])


Reload thefuck (open a new shell) and run fuck until the rule is tried and applied. You get the (erroneous) output seen below.

The output of The Fuck with THEFUCK_DEBUG=true exported (typically execute export THEFUCK_DEBUG=true in your shell before The Fuck):

...
DEBUG: Trying rule: subprocess_test; took: 0:00:00.000002
echo "--- running subprocess check --" (+side effect) [enter/↑/↓/ctrl+c]
DEBUG: /usr/lib/python3.11/subprocess.py
DEBUG: PYTHONIOENCODING: utf-8
DEBUG: Total took: 0:00:01.429646
               total        used        free      shared  buff/cache   available
Mem:        32771404    12371052     4576880      399764    16688000    20400352
Swap:        2097148           0     2097148
--- running subprocess check --

If the bug only appears with a specific application, the output of that application and its version:

N/A

Anything else you think is relevant:

N/A

tnyeanderson avatar Nov 02 '23 05:11 tnyeanderson