The cd
context manager and the prefix
context manager don't actually run commands when you invoke them, they just modify some local environment settings that affect any subsequent invocations of run()
and/or sudo()
.
So when your run('some_random_command')
gets executed, it gets executed, it runs in the context of (cd=/folder/one
, prefix=change_path_command
, cd=/folder/two
), and since the inner cd
takes precedence over the outer cd
, the end result is a single command executed with cd /folder/two && change_path_command && some_random_command
.
Take a look at the source code for cd
and prefix
to a get a better idea of how that works -- all they ultimately do is modify the dictionary fabric.state.env
when they enter and exit. These later get applied in the call to _prefix_commands()
, which gets called from run()
and sudo()
via the _run_command()
function.