Skip to content

procs: Add $XONSH_CAPTURE_ALWAYS variable for opt-in interactive capt… #4283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 21, 2021

Conversation

daniel-shimon
Copy link
Contributor

@daniel-shimon daniel-shimon commented May 15, 2021

…uring

Closes #4249
Closes #4304
Closes #4024
Closes #3672
Closes #3311
Closes #4318
Closes #3584
Closes #3672

Explanation from one of the issues:

All these issues are indeed because xonsh tries to capture the output of interactive processes.
Turning THREAD_SUBPROCS off kinda fixes it but it isn't good either, since it means $(...) doesn't always work.
We've decided to add a new XONSH_CAPTURE_INTERACTIVE variable that will give us better control and will be False by default

For community

⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment

@anki-code
Copy link
Member

anki-code commented May 15, 2021

Nice start. We have this on the current master:

$XONSH_TRACE_SUBPROC=True

echo 1
# TRACE SUBPROC: (['echo', '1'],), captured=hiddenobject

_ = $(echo 1)
# TRACE SUBPROC: (['echo', '1'],), captured=stdout

_ = !(echo 1)
# TRACE SUBPROC: (['echo', '1'],), captured=object

![echo 1]
# TRACE SUBPROC: (['echo', '1'],), captured=hiddenobject

$[echo 1]
# TRACE SUBPROC: (['echo', '1'],), captured=False

And we want to switch first case to captured=False without any effect on other cases.

By the proposed changes:

if captured == "hiddenobject" and not builtins.xonsh.env.get("XONSH_CAPTURE_ALWAYS"):

we will change also ![] behavior that we should not change.

@anki-code
Copy link
Member

Also I think this setting should be in xonfig to analysis the future issues around freezing the terminal in the captured mode.

@daniel-shimon
Copy link
Contributor Author

When running commands in xonsh they are actually transformed into ![]. It's the same thing

@anki-code
Copy link
Member

anki-code commented May 15, 2021

Yep, and I think we should have the way to switch this transformation to $[].

I suggest for example $XONSH_SUBPROC_NO_BRACES_CAPTURED variable. Default: True. But in the future it will be False.

@daniel-shimon
Copy link
Contributor Author

Why? That way you can't know the return value and other info.
As I see it, since ![] denotes an "uncaptured subprocesses", xonsh shouldn't capture its stdout unless explicitly told to via XONSH_CAPTURE_ALWAYS.
![] and $[] should behave identically (except their return values), a la the docs:

Uncaptured subprocesses are denoted with the $[] and ![] operators.
They are the same as $() captured subprocesses in almost every way.
The only difference is that the subprocess’s stdout passes directly through xonsh and to the screen.
The return value of $[] is always None.

@daniel-shimon
Copy link
Contributor Author

Another current issue is that when THREAD_SUBPROCS=False, !() fails to capture stdout while $() succeeds (without running the process in another thread).
As I see it, this doesn't make any sense and they should behave identically as well.

@anki-code
Copy link
Member

anki-code commented May 15, 2021

Why? That way you can't know the return value and other info.

Yep, maybe it should (have to) be ![]. I just have no success with this operator:

cd /tmp
# download interactive tool i.e. Miniconda from https://docs.conda.io/en/latest/miniconda.html
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
![./Miniconda3-latest-Linux-x86_64.sh ]
# press Enter and got freezed shell
$[./Miniconda3-latest-Linux-x86_64.sh ]
# working as expected

It looks like we should have another operator that can run uncaptured command but can catch return code etc.

As I understand we want to solve this case. We want to run just:

./Miniconda3-latest-Linux-x86_64.sh
# or the same case is #4243:
git log

and save the return code and timing to the history. For now these cases are failed because captured is the default.

@anki-code
Copy link
Member

Subproc has stdout/stderr and return code. Sometimes we want to catch both. Sometimes only return code. Sometimes nothing. Is the current operators/run_subproc functionality enough?

@daniel-shimon
Copy link
Contributor Author

It looks like we should have another operator that can run uncaptured command but can catch return code etc.

That's exactly what XONSH_CAPTURE_ALWAYS is for. No need for a different operator.
This whole 'printing and also capturing at the same time' mechanism is unexpected and causes most of these problems.

@anki-code
Copy link
Member

I've tested the branch and it works for me!

@@ -857,8 +857,15 @@ def run_subproc(cmds, captured=False, envs=None):
if builtins.__xonsh__.env.get("XONSH_TRACE_SUBPROC"):
print(f"TRACE SUBPROC: {cmds}, captured={captured}", file=sys.stderr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put the actual captured value here.

@anki-code
Copy link
Member

anki-code commented May 15, 2021

No need for a different operator.

We lose the use case like:

# current master
r = ![ls]  # the tool that produce some output
if 'file' in r.out:  # searching in the output
   print('found file')

In this case ![ls] returns colored output but r.out has the output without colors. The case looks useful.

@daniel-shimon
Copy link
Contributor Author

r = ![ls] # the tool that produce some output

So this is the case to turn on XONSH_CAPTURE_OUTPUT
But in reality if you want the output of a command you need to run $() or !()

In this case ![ls] returns colored output but r.out has the output without colors. The case looks useful.

I wonder why that is.. this is really strange! (just to be clear it also happens on main)

@daniel-shimon daniel-shimon force-pushed the optional-interractive-capture branch from 254d907 to 25f73a0 Compare June 18, 2021 14:32
@daniel-shimon daniel-shimon marked this pull request as ready for review June 18, 2021 14:34
@daniel-shimon daniel-shimon requested a review from anki-code June 18, 2021 14:38
@codecov-commenter
Copy link

codecov-commenter commented Jun 18, 2021

Codecov Report

Merging #4283 (606531a) into main (8e547d0) will decrease coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4283      +/-   ##
==========================================
- Coverage   57.18%   57.12%   -0.06%     
==========================================
  Files         138      138              
  Lines       22151    22152       +1     
  Branches     4059     4059              
==========================================
- Hits        12666    12655      -11     
- Misses       8438     8444       +6     
- Partials     1047     1053       +6     
Impacted Files Coverage Δ
xonsh/procs/specs.py 68.81% <ø> (-0.54%) ⬇️
xonsh/environ.py 75.41% <100.00%> (+0.03%) ⬆️
xonsh/procs/posix.py 64.46% <0.00%> (-2.84%) ⬇️
xonsh/commands_cache.py 71.02% <0.00%> (-0.71%) ⬇️
xonsh/procs/proxies.py 53.37% <0.00%> (-0.22%) ⬇️
xonsh/procs/pipelines.py 77.97% <0.00%> (-0.20%) ⬇️
xonsh/jobs.py 30.41% <0.00%> (+1.39%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8e547d0...606531a. Read the comment docs.

jeremyschlatter added a commit to jeremyschlatter/env that referenced this pull request Jun 20, 2021
jeremyschlatter added a commit to jeremyschlatter/env that referenced this pull request Jun 20, 2021
See this previous commit:

    90c0690 xonsh: workaround issue with git commit

Also see xonsh/xonsh#4283, which should make
this workaround unnecessary.
@daniel-shimon daniel-shimon requested a review from gforsyth June 20, 2021 09:56
@daniel-shimon
Copy link
Contributor Author

Thanks @anki-code !
@gforsyth I would also appreciate your review since this changes default behavior

Copy link
Collaborator

@gforsyth gforsyth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I think this is a good step forward and all the functionality is still available but with a slightly friendlier default.

Thanks @daniel-shimon for this! And thanks @anki-code for your input!

@gforsyth gforsyth merged commit f87be90 into xonsh:main Jun 21, 2021
jeremyschlatter added a commit to jeremyschlatter/env that referenced this pull request Jun 25, 2021
The latest master commit includes a merge of
xonsh/xonsh#4283, which means I can remove my
workarounds for that issue.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants