Commit graph

2229 commits

Author SHA1 Message Date
a
238aeef005 Merge remote-tracking branch 'origin/main' into fix_callias_capturing 2024-06-25 12:29:44 +02:00
a
584a84d645 news 2024-06-25 00:46:32 +02:00
a
1faca101ab fix 2024-06-24 21:46:09 +02:00
a
eebfe98010 news 2024-06-22 21:45:48 +02:00
Gil Forsyth
5332d75505 Updated CHANGELOG for 0.17.0 2024-06-18 08:47:50 -04:00
Andy Kipp
4e12834e84
spec: added raise_subproc_error (#5494)
### Motivation

Add `spec.raise_subproc_error` to have an ability to use
`SpecModifierAlias` to manage the errors. Also this is the first step to
#4351.

Generally in scripts it's good to have RAISE_SUBPROC_ERROR=True to avoid
processing the error for every executed command. But in some cases (e.g.
`![]`) it's needed to avoid raising the error. To more elegant doing
this we can make an ability to create SpecModifier.

### After

```xsh
from xonsh.procs.specs import SpecModifierAlias
class SpecModifierNoErrAlias(SpecModifierAlias):
    def on_modifer_added(self, spec):
        spec.raise_subproc_error = False
aliases['noraise'] = SpecModifierNoErrAlias()

$RAISE_SUBPROC_ERROR = True

if ![noraise git pull]:
    git add --all
```

Cc #5443 

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-06-18 08:43:10 -04:00
Andy Kipp
09a89bf9b4
docs: install xonsh as a core shell (#5504)
### Motivation

I'm using [xonsh-install](https://github.com/anki-code/xonsh-install)
pretty long time and I see that it works pretty predictable, expected
and stable. I suggest to mention this approach in our docs to get more
attention and testing around this from users and some day move
xonsh-install to the xonsh project.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: Gil Forsyth <gforsyth@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-06-17 13:17:10 -04:00
Andy Kipp
f9d2842fb1
docs: move section "Use the Nix Package manager with Xonsh" from rc to platform tips and tricks (#5503)
<!---

Thanks for opening a PR on xonsh!

Please do this:

1. Include a news file with your PR
(https://xon.sh/devguide.html#changelog).
2. Add the documentation for your feature into `/docs`.
3. Add the example of usage or before-after behavior.
4. Mention the issue that this PR is addressing e.g. `#1234`.

-->

subj

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
2024-06-15 18:31:50 +02:00
Andy Kipp
f81d55d09b
Do not load ~/.xonshrc in not interactive mode (#5491)
### Motivation

In #5099 was introduced the logic where all RC files are loaded in
interactive and non-interactive modes. This logic is not working good
for home based `~/.xonshrc` file.

First of all `~/.xonshrc` is the buffer/accumulator of settings focused
on interactive mode. Most tools with integration with xonsh (e.g.
`conda`, `zoxide`, etc) offer to just do `echo "init_tool()" >>
~/.xonshrc` (`conda` has around 20 lines of init code) to start using
the tool and users are doing this without any doubts.

But because of after 5099 `~/.xonshrc` is executed in non-interactive
mode the adding code to it leads to unexpected and unintended side
effects:

* If you run a script with shebang (e.g. `#!/usr/bin/env xonsh` or
[xonsh-awesome-cli-app](https://github.com/anki-code/xonsh-awesome-cli-app))
or just from `xonsh script.xsh` the code will be unexpected and
unintended slower.

* If you're using xonsh-based tools (e.g. you install them using pip)
and run them in environment that has no packages that initiated in
`~/.xonshrc` you will see epic errors.

* Additional context: 
* Bash and Zsh do not load `~/.bashrc` and `~/.zshrc` in non-interactive
mode by the same reasons.
* We have welcome message `Create ~/.xonshrc file manually or use xonfig
to suppress the welcome message` and we don't want to make the process
of creating this file complex.

All of this leads to bad unexpected and unintended experience. This PR
is to fix this.

### Expectation

By doing this fix we assume that experienced user who wants to build
good repeatable run control files will use another ways to create config
files and this has [reflection in
docs](8860f2bd52/docs/xonshrc.rst).
In the nutshell if you want to create the RC files that affect every run
of code you should use one or many of these ways:

* Cross-desktop group (XDG) compliant `~/.config/xonsh/rc.xsh` control
file.
* The system-wide control file `/etc/xonsh/xonshrc` for Linux and OSX
and in `%ALLUSERSPROFILE%\xonsh\xonshrc` on Windows. It controls options
that are applied to all users of Xonsh on a given system.
* The home-based directory `~/.config/xonsh/rc.d/` and system
`/etc/xonsh/rc.d/` can contain .xsh files. They will be executed at
startup in order. This allows for drop-in configuration where your
configuration can be split across scripts and common and local
configurations more easily separated.

In your configs you need to check `$XONSH_INTERACTIVE` and
`$XONSH_LOGIN` explicitly.

### Before

`~/.xonshrc` is used in non-interactive mode.

```xsh
echo "echo RC" >> ~/.xonshrc
cd /tmp
echo "echo Script" > script.xsh
xonsh script.xsh
# RC
# Script
```
```xsh
cd /tmp
echo 'echo RC' >> ~/.xonshrc
echo '#!/usr/bin/env xonsh' > myscript
chmod +x myscript

./myscript
# RC
```

### After

`~/.xonshrc` is not used in non-interactive mode. Use `-i` if you need
it.

```xsh
echo "echo RC" >> ~/.xonshrc
cd /tmp
echo "echo Script" > script.xsh
xonsh script.xsh
# Script

xonsh -i script.xsh
# RC
# Script
```
```xsh
cd /tmp
echo 'echo RC' >> ~/.xonshrc
echo '#!/usr/bin/env xonsh' > myscript
chmod +x myscript

./myscript
```
Closes #5488 #4096 #5496

### Fun

I want to leave here some nice representation of how it works in
bash/sh/zsh from [twitter
post](https://twitter.com/paxx39/status/1742768007154479109):



![image](https://github.com/xonsh/xonsh/assets/1708680/cd7b3803-483f-4d5d-bf9d-baa61c794f68)


## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-06-13 10:08:23 -04:00
Jason R. Coombs
4aec6d1d12
Perform proper case-insensitive matching in command_cache on Windows (#5477)
Closes #5476
Closes #5469
2024-06-10 12:29:13 -04:00
Andy Kipp
63da8e3492
Tutorial: Callable alias and capturing (#5472)
<!---

Thanks for opening a PR on xonsh!

Please do this:

1. Include a news file with your PR
(https://xon.sh/devguide.html#changelog).
2. Add the documentation for your feature into `/docs`.
3. Add the example of usage or before-after behavior.
4. Mention the issue that this PR is addressing e.g. `#1234`.

-->

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
2024-06-10 09:42:00 -04:00
Andy Kipp
850bd8f34b
Revert "Revert #5423" (#5475)
Reverts xonsh/xonsh#5468

Closes #5466

Related #5423

---------

Co-authored-by: a <1@1.1>
2024-06-02 14:53:00 +02:00
Andy Kipp
c9046ab3f6
Revert #5423 (#5468)
I'm going to revert #5423 because it was regress -
https://github.com/xonsh/xonsh/issues/5466 - and we need to fix parser
instead of resolver.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
2024-05-31 22:59:59 +02:00
Andy Kipp
aaf3c99a3b
Added `$XONSH_TRACE_SUBPROC=3` (#5459)
Show std if `$XONSH_TRACE_SUBPROC=3`.

### Motivation

* It's very helpful if you want to understand how subproc is working. 
* It's helpful to trace `SpecModifierAlias`.
* It's helpful to trace cases like #2618

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-31 12:50:25 +02:00
Andy Kipp
1feab91935
prompt: yellow env_name (#5457)
### Motivation

When interact with default prompt I noticed that`env_name` has no color
and when you type command or see the output it mix with `env_name`
visually. Prompt is not highlighted when `env_name` has default color.

### Before

Output and env_name are visually mixed:

<img width="900" alt="image"
src="https://github.com/xonsh/xonsh/assets/1708680/c843388e-6e72-4fba-ab0f-6cd3fb023cd2">


### After

Output and env_name are visually distinct:

<img width="918" alt="image"
src="https://github.com/xonsh/xonsh/assets/1708680/09f45cd8-63d1-479a-bfab-c8063cf03aeb">



## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-28 22:25:21 -04:00
Andy Kipp
730fe45627
Added SpecModifierAlias and xthread, xunthread aliases (#5443)
### Motivation

* We have no recommended way to force subprocess command be
(un)threadable
  * #4214
  * #2119
  * #5003
* It's interesting opportunity to have a way to modify specs and CP
using `SpecModifierAlias`.

### Before

```xsh
!(ssh host -T "echo 1")
# output=''  # EXPECTED: 1

__xonsh__.commands_cache.threadable_predictors['ssh'] = lambda *a, **kw: True
!(ssh host -T "echo 1")
```


### After

```xsh
xthread
# Mark command as threadable.

!(xthread ssh host -T "echo 1")
# output='1'
```

Closes:
* Closes #4214
* Closes #2119
* Partially closes #5003

Implementation of `SpecModifierAlias` will help in:
* #2618

JFYI #5413 

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Gil Forsyth <gforsyth@users.noreply.github.com>
2024-05-28 11:03:45 -04:00
Andy Kipp
1f5a7a816b
Mitigate exception on start up if `dircolors` binary is broken (#5439)
### Motivation

If `dircolors` utility is wrong by any reason you can't start xonsh
shell. This should not be stopper.

### Before

```xsh
cd /tmp
echo '#!/bin/qwe' > dircolors && chmod +x dircolors
echo '$PATH.append("/tmp")' > rc
xonsh --no-env --rc /tmp/rc
# OSError: [Errno 8] Exec format error: 'dircolors'
# Xonsh encountered an issue during launch
```

### After

It's working.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-27 20:37:30 +02:00
Andy Kipp
2d966fb1b5
Refreshing procs/specs.py (#5424)
### Motivation

This PR just clean the code in `procs/specs.py` without changes the
logic: group the code into functions. This is needed to the next feature
PR #5443.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-26 10:59:36 +02:00
Andy Kipp
14342b2ea3
Fixed redirect with python substitution (#5423)
### Motivation

Closes #5420

### Before

```xsh
xonsh --no-rc
f = p'/tmp/file.tmp'
echo 'abc' > @(f)
# returns error: xonsh: ['/tmp/file.tmp']: unable to open file
```

### After

```xsh
xonsh --no-rc
f = p'/tmp/file.tmp'
echo 'abc' > @(f)
cat @(f)
# abc
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-26 10:57:35 +02:00
Andy Kipp
f582a33d61
Added support of NixOS core tools in `predict_threadable` (#5440)
### Motivation

Closes #5003

### The case

Core utils in Nix are symlinks to one binary file that contains all
utils:

```xsh
docker run --rm -it nixos/nix bash

which echo
# /nix/store/k6h0vjh342kqlkq69sxjj8i5y50l6jfr-coreutils-9.3/bin/echo

ls -la /nix/store/k6h0vjh342kqlkq69sxjj8i5y50l6jfr-coreutils-9.3/bin/
# b2sum -> coreutils
# base32 -> coreutils
# ...
# All tools are symlinks to one binary file - `coreutils`.
```

When
[`default_predictor_readbin`](61bda708c9/xonsh/commands_cache.py (L392))
read `coreutils` it catches `(b'isatty', b'tcgetattr', b'tcsetattr')`
and return `threadable=False` forever.

The list of Nix coreutils tools are exactly the same as in [brew
coreutils](https://formulae.brew.sh/formula/coreutils). So I can check
the real predicts on distinct binaries and see that only 2 tools among
106 are unthreadable and they already covered by
`default_threadable_predictors` (If it's wrong please add unthreadable
tools to the
[default_threadable_predictors](61bda708c9/xonsh/commands_cache.py (L518))):

```xsh
brew install coreutils

ls /opt/homebrew/opt/coreutils/libexec/gnubin/ | wc -l
# 106

for t in p`/opt/homebrew/opt/coreutils/libexec/gnubin/.*`:
    if not (th := __xonsh__.commands_cache.predict_threadable([t.name])):
        print($(which @(t.name)), th)
# /opt/homebrew/opt/coreutils/libexec/gnubin/cat False
# /opt/homebrew/opt/coreutils/libexec/gnubin/yes False

defaults = __import__('xonsh').commands_cache.default_threadable_predictors().keys()
defaults['cat']
# <function xonsh.commands_cache.predict_false>
defaults['yes']
# <function xonsh.commands_cache.predict_false>
```

So the rest of we need is to check the symlink and apply default
prediction if the tools is the symlink to coreutils. This implements
this PR. Test included.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-24 16:51:25 -04:00
Andy Kipp
61bda708c9
Unpin prompt-toolkit version (#5438)
Merge this after https://github.com/xonsh/xonsh/pull/5437

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
2024-05-23 14:08:54 -04:00
Andy Kipp
1847f8a379
Fixed I/O operation on closed file and Bad file descriptor exceptions after running callable aliases multiple times (#5437)
### Motivation

Closes #5435.

### After

After this fix I can't reproduce this:
* https://github.com/xonsh/xonsh/issues/5241#issuecomment-1961249511
  * We can revert https://github.com/xonsh/xonsh/pull/5288
* https://github.com/xonsh/xonsh/issues/5393 
  * We can revert https://github.com/xonsh/xonsh/pull/5403

### Next step

Here is the PR for unpin ptk - #5438

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-23 14:07:30 -04:00
Andy Kipp
0f25a5a348
Read stop signals from the process and update the process state. (#5361)
Reading stop signals from the process and update the process state.

### The issue

Technically. In a couple of places that critical for processing signals
we have `os.waitpid()`. The function behavior is pretty unobvious and
one of things is processing return code after catching the signal. We
had no good signal processing around this and this PR fixes this. See
also `proc_untraced_waitpid` function description.

From user perspective. For example we have process that is waiting for
user input from terminal e.g. `python -c "input()"` or `fzf`. If this
process will be in captured pipeline e.g. `!(echo 1 | fzf | head)` it
will be suspended by OS and the pipeline will be in the endless loop
with future crashing and corrupting std at the end. This PR fixes this.

### The solution

Technically. The key function is `proc_untraced_waitpid` - it catches
the stop signals and updates the process state.

From user perspective. First of all we expect that users will use
captured object `!()` only for capturable processes. Because of it our
goal here is to just make the behavior in this case stable.
In this PR we detect that process in the pipeline is suspended and we
need to finish the command pipeline carefully:
* Show the message about suspended process.
* Keep suspended process in `jobs`. The same behavior we can see in
bash. This is good because we don't know what process suspended and why.
May be experienced user will want to continue it manually.
* Finish the CommandPipeline with returncode=None and suspended=True.

### Before

```xsh
!(fzf) # or !(python -c "input()")
# Hanging / Exceptions / OSError / No way to end the command.
# After exception:
$(echo 1)
# OSError / IO error
```

### After

```xsh
!(fzf) # or `!(ls | fzf | head)` or `!(python -c "input()")`
# Process ['fzf'] with pid 60000 suspended with signal 22 SIGTTOU and stay in `jobs`.
# This happends when process start waiting for input but there is no terminal attached in captured mode.
# CommandPipeline(returncode=None, suspended=True, ...)

$(echo 1)
# Success.
```
Closes #4752 #4577

### Notes

* There is pretty edge case situation when the process was terminated so
fast that we can't catch pid alive and check signal
([src](67d672783d/xonsh/jobs.py (L71-L80))).
I leave it as is for now.

### Mentions

#2159

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Gil Forsyth <gforsyth@users.noreply.github.com>
2024-05-22 11:45:39 -04:00
Andy Kipp
635d7837c5
Saving history on SIGINT (#5425)
### Motivation

Development tools like PyCharm send SIGINT before SIGKILL in case of
reload app. Saving history on SIGINT is the last chance to save history
in this case.

I understand that history will be flushed on Ctrl+C (SIGINT) but it
looks like no bad consequences of this. In addition history saving will
be more stable.

### Before
```xsh
# Development in PyCharm
xonsh --no-rc
echo cmd
# Ctrl+RR of Ctrl+DD to restart.
# Command history lost.
```

### After
```xsh
# Development in PyCharm
xonsh --no-rc
echo cmd
# Ctrl+R Ctrl+R for restart
# Command history is here.
```

Closes #2657

### Env

Use `$XONSH_HISTORY_SIGINT_FLUSH=False` to switch it off.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-22 10:20:08 -04:00
Andy Kipp
c5f7b74ca8
Show `root and @#` in prompt if user is superuser. (#5409)
### Before

```xsh
sudo xonsh --no-rc
# <user>@host ~ #
```


### After

```xsh
sudo xonsh --no-rc
# root@host ~ @#
```

It's in sync with:

```xsh
docker run -it ubuntu bash
# root@ce01c2856819:/#
echo $USER
# 
# root@ce01c2856819:/#
```
## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-22 09:27:03 -04:00
Andy Kipp
aa69ce868a
Now last executed CommandPipeline is available in `__xonsh__.last` (#5422)
### Motivation

There is no way to access to the CommandPipeline of executed command in
uncaptured mode. This causes:
 * No chance to get return code in one universal way.
 * Barrier to trace and inspection.

### Before

```xsh
$(ls nofile)
# No way to access to the CommandPipeline of executed command.
```

### After
```xsh
$(ls nofile)
__xonsh__.last.rtn  # In interactive or not interactive.
# 2
```
```xsh
# Sugar sir:
last = type('LastCP', (object,), {'__getattr__':lambda self, name: getattr(__xonsh__.last, name) })()

ls nofile
last.rtn
# 2
```

JFYI #5342

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-22 09:25:35 -04:00
Andy Kipp
f50a9e577c
Fix 5379, 5429 (#5432)
Fix described in
https://github.com/xonsh/xonsh/issues/5358#issuecomment-2104322754
Closes #5379, #5429

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**
2024-05-22 08:27:44 +05:30
Andy Kipp
f17b72ca1f
Added `env.detype_all()` to get all available variables that is possible to detype. (#5431)
### Motivation

Closes #4636 

In fact`Env` manages two lists: explicitly set values and Xettings. When
we operate with env we have no detyping for Xettings. The `detype_all`
function doint detyping for all possible env variables.

### Before

```xsh
# No way to get all detyped variables e.g.
'PROMPT' in __xonsh__.env.detype()
# False
```

### After
```xsh
'PROMPT' in __xonsh__.env.detype_all()
# True
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-22 07:53:11 +05:30
Andy Kipp
9073a4c33c
Make xonsh tolerant to inaccessible paths: history backend, script cache (#5430)
Closes #5319

### Before

```xsh
mkdir -p /tmp/noaccess
chmod 000 /tmp/noaccess
echo 'print(1)' > /tmp/1.xsh

xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess'
# Json History error

xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess' /tmp/1.xsh
# Script cache error
```

### After

```xsh
mkdir -p /tmp/noaccess
chmod 000 /tmp/noaccess
echo 'print(1)' > /tmp/1.xsh

xonsh --no-rc --no-env -DXONSH_DATA_DIR='/tmp/noaccess'
# Error during load <class 'xonsh.history.json.JsonHistory'>: [Errno 13] Permission denied: '/tmp/noaccess/history_json'
# Set $XONSH_HISTORY_BACKEND='dummy' to disable history.
# History disabled.
# @

xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess /tmp/1.xsh
# xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
# update_cache: Cache file is not writable: /tmp/noaccess/xonsh_script_cache/private/tmp/1_.xsh.cpython-311
# Set $XONSH_CACHE_SCRIPTS=0, $XONSH_CACHE_EVERYTHING=0 to disable cache.
# 1
```
And no warnings when everything is switched off:
```xsh
xonsh % python -m xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess -DXONSH_HISTORY_BACKEND=dummy
# @

xonsh --no-rc --no-env -DXONSH_DATA_DIR=/tmp/noaccess -DXONSH_CACHE_SCRIPTS=0 -DXONSH_CACHE_EVERYTHING=0 /tmp/1.xsh
# 1
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-20 21:14:14 +02:00
Andy Kipp
a79d48c749
History: Saving history in case of any type of exiting the shell. (#5418)
### Before

```xsh
echo 1 
echo 2
# <<Crash>>
# No json history
```

### After

Closes #2657

```xsh
echo 1 
echo 2
# <<Crash>>
# In json history
# echo 1
# echo 2
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
2024-05-16 11:01:38 +05:30
Andy Kipp
fd5304fb87
fix(signals): fix processing exit signals and exit exception (#5399)
### Before

Case 1: Handler catches the exit signal and do not pass it forward. As
result xonsh could be suspended by OS or crash. Also exit code is wrong.
See repeatable examples with SIGHUP in
https://github.com/xonsh/xonsh/issues/5381#issuecomment-2097961804.

Case 2: From bash/zsh as login shell run xonsh. Then send quit signal to
xonsh. The terminal state will be broken: disabled SIGINT, mouse pointer
produces mouse state codes.

### After

Case 1: Xonsh exit normally with right exit code. Fixed #5381 #5304
#5371.

Case 2: After exiting with right exit code the state of the terminal is
good.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-13 09:31:55 -04:00
Andy Kipp
bb394a8e84
feat: add superhelp and additional context via new FuncAlias (#5366)
### Goals

* Make callable aliases transparent.
* Catch errors in callable aliases and show the name of the source.
* Show additional attributes: thredable, capturable.
* Closes #5266

## Exception

### Before

```xsh
aliases['cd']
# <function xonsh.dirstack.cd>

aliases['trace']
# <function xonsh.aliases.trace>

aliases['null'] = lambda: 1/0
null
# ZeroDivisionError: division by zero

@aliases.register('catch')
@aliases.register('me')
@aliases.register('if')
@aliases.register('you')
@aliases.register('can')
def _exc(args, stdin, stdout):
    for line in stdin.readlines():
        print(line.strip() + '!', file=stdout, flush=True)
    return 1/0 if 'i' in $__ALIAS_NAME else 0

echo hey | catch | me | if | you | can
# ZeroDivisionError: division by zero      <--- ???
# hey!!!!!
```

### After

```xsh 
aliases['cd']
# FuncAlias({'name': 'cd', 'func': 'cd'})

aliases['trace']
# FuncAlias({'name': 'trace', 'func': 'trace', '__xonsh_threadable__': False})

$XONSH_SHOW_TRACEBACK=False
$RAISE_SUBPROC_ERROR = False
aliases['null'] = lambda: 1/0
null
#Exception in thread {'cls': 'ProcProxyThread', 'name': 'Thread-15', 'func': FuncAlias({'name': 'null', 'func': '<lambda>'}), 'alias': 'null', 'pid': None}
#ZeroDivisionError: division by zero



@aliases.register('catch')
@aliases.register('me')
@aliases.register('if')
@aliases.register('you')
@aliases.register('can')
def _exc(args, stdin, stdout):
    for line in stdin.readlines():
        print(line.strip() + '!', file=stdout, flush=True)
    return 1/0 if 'i' in $__ALIAS_NAME else 0
echo hey | catch | me | if | you | can
# Exception in thread {'cls': 'ProcProxyThread', 'name': 'Thread-8', 'func': FuncAlias({'name': 'if', 'func': '_exc'}), 'alias': 'if', 'pid': None}
# ZeroDivisionError: division by zero
# hey!!!!!
```

## Superhelp

### Before
```xsh
@aliases.register("hello")
def _alias_hello():
    """Show world."""
    print('world')

hello?
# No manual entry for hello
```

### After
```xsh
@aliases.register("hello")
def _alias_hello():
    """Show world."""
    print('world')

hello?
# FuncAlias({'name': 'hello', 'func': '_alias_hello'}):
# Show world.
```



## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-13 09:11:58 -04:00
Andy Kipp
55b341d477
Add Jupytext to news (#5402)
https://github.com/xonsh/xonsh/discussions/5401

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
2024-05-10 11:03:44 +02:00
Andy Kipp
d2d13f028f
Downgrade prompt-toolkit to >=3.0.29,<3.0.40 (#5403)
This downgrade continues previous (#5288). Found issue in prompt-toolkit
3.0.40 (#5393).

JFYI #5241

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

Co-authored-by: a <1@1.1>
2024-05-09 09:13:03 -04:00
Andy Kipp
ac7bc67406
Fixed showing exception message (#5394)
subj

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-06 19:48:43 +05:30
Andy Kipp
b19a89b432
Fixed `xonsh -DVAR=VAL` behavior: initiate env variables before shell initialization. (#5396)
### Before

Case 1:
```xsh
xonsh --no-rc --no-env -DCOLOR_OUTPUT 0  # space between -DVAR and VALUE
```
```xsh
Traceback (most recent call last):
  File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/main.py", line 478, in main
    args = premain(argv)
           ^^^^^^^^^^^^^
  File "/Users/pc/.local/xonsh-env/lib/python3.12/site-packages/xonsh/main.py", line 420, in premain
    env.update([x.split("=", 1) for x in args.defines])
  File "<frozen _collections_abc>", line 987, in update
ValueError: not enough values to unpack (expected 2, got 1)
Xonsh encountered an issue during launch
```
Case 2:
```xsh
xonsh --no-rc --no-env -DXONSH_HISTORY_BACKEND=dummy
history info
# json  # EXPECTED: dummy
```

### After
Case 1:
```xsh
xonsh --no-rc --no-env -DCOLOR_OUTPUT 0
# Wrong format for -DCOLOR_OUTPUT argument. Use -DVAR=VAL form.
```

Case 2:
```xsh
xonsh --no-rc --no-env -DXONSH_HISTORY_BACKEND=dummy
history info
# dummy
```

Closes #5395

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-06 17:39:10 +05:30
Andy Kipp
cdc1d602a2
Fixed empty stacktrace for CalledProcessError (#5391)
Fixed #5387

### Before

```xsh
$XONSH_SHOW_TRACEBACK = False
$RAISE_SUBPROC_ERROR = True

ls nofile
# ls: nofile: No such file or directory
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
#
```

### After 

```xsh
$RAISE_SUBPROC_ERROR = False
$XONSH_SHOW_TRACEBACK = False
ls nofile
# ls: nofile: No such file or directory

$RAISE_SUBPROC_ERROR = True
$XONSH_SHOW_TRACEBACK = False
ls nofile
# ls: nofile: No such file or directory
# subprocess.CalledProcessError: Command '['ls', 'nofile']' returned non-zero exit status 1.

$RAISE_SUBPROC_ERROR = True
$XONSH_SHOW_TRACEBACK = True
ls nofile
# ls: nofile: No such file or directory
# Traceback (most recent call last):
# ...
# subprocess.CalledProcessError: Command '['ls', 'nofile']' returned non-zero exit status 1.

$RAISE_SUBPROC_ERROR = False
$XONSH_SHOW_TRACEBACK = True
ls nofile
# ls: nofile: No such file or directory
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-04 11:50:32 +02:00
Andy Kipp
a3e8b1a025
Use substring for env completion and better way to sort list (#5388)
### Motivation

It's very annoying to search env variable exactly by lprefix. The better
is to search by substring and sort results by the position of substring
and then alphabetically.

Closes #5386

### Before

```xsh
$TRA<Tab>
# nothing
```

### After
```xsh
$TRA<Tab>
# 'XONSH_TRACE_COMPLETIONS', 
# 'XONSH_TRACE_SUBPROC', 
# 'XONSH_TRACE_SUBPROC_FUNC', 
# 'XONSH_TRACEBACK_LOGFILE', 
# 'XONSH_SHOW_TRACEBACK', 
# 'VC_GIT_INCLUDE_UNTRACKED'
```

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-03 10:30:14 +02:00
Andy Kipp
21da30e753
Fixed populating the return code for interrupted process. (#5380)
### Motivation

There is annoying behavior when you run command in the loop and can't
interrupt e.g. [this
report](https://github.com/xonsh/xonsh/discussions/5371) and a bit
#5342. After diving into this I see the issue around return code.

### The essence

Basically ``p = subprocess.Popen()`` populates ``p.returncode`` after
``p.wait()``, ``p.poll()`` or ``p.communicate()``
([doc](https://docs.python.org/3/library/os.html#os.waitpid)). But if
you're using `os.waitpid()` BEFORE these functions you're capturing
return code from a signal subsystem and ``p.returncode`` will be ``0``
like success but it's not success. So after ``os.waitid`` call you need
to set return code manually ``p.returncode = -os.WTERMSIG(status)`` like
in Popen. Example:
```xsh
python  # python interactive

import os, signal, subprocess as sp

p = sp.Popen(['sleep', '100'])
p.pid
# 123
p.wait()
# Ctrl+C or `kill -SIGINT 123` from another terminal
p.returncode
# -2

# BUT:

p = sp.Popen(['sleep', '100'])
p.pid
# 123

pid, status = os.waitpid(p.pid, os.WUNTRACED)
# status=2

# From another terminal:
kill -SIGINT 123

p.wait()
# 0
p.returncode
# 0
```

```xsh
from xonsh.tools import describe_waitpid_status
describe_waitpid_status(2)
# WIFEXITED - False - Return True if the process returning status exited via the exit() system call.
# WEXITSTATUS - 0 - Return the process return code from status.
# WIFSIGNALED - True - Return True if the process returning status was terminated by a signal.
# WTERMSIG - 2 - Return the signal that terminated the process that provided the status value.
# WIFSTOPPED - False - Return True if the process returning status was stopped.
# WSTOPSIG - 0 - Return the signal that stopped the process that provided the status value.
```

See also: [Helpful things for
knight](https://github.com/xonsh/xonsh/pull/5361#issuecomment-2078826181).

### Before

```xsh
$RAISE_SUBPROC_ERROR = True

sleep 100
# Ctrl+C
_.rtn
# 0  # It's wrong and RAISE_SUBPROC_ERROR ignored.

for i in range(5):
    print(i)
    sleep 5
# 0
# Ctrl+C  # Can't interrupt
# 1
# 2 
```

### After
```xsh
sleep 100
# Ctrl+C
_.rtn
# -2  # Like in default Popen

$RAISE_SUBPROC_ERROR = True
for i in range(5):
    print(i)
    sleep 5
# 0
# Ctrl+C
# subprocess.CalledProcessError
```

### Notes

* We need to refactor `xonsh.jobs`. It's pretty uncomfortable work with
module.
* The logic is blurry between Specs, Pipelines and Jobs. We need to
bring more clear functions.
* The `captured` variable looks like "just the way of capturing (stdout,
object)" but in fact it affects all logic very much. We need to create
table where we can see the logic difference for every type of capturing.
E.g. in `captured=stdout` mode we use `xonsh.jobs` to monitor the
command but in `captured=object` we avoid this and some logic from
`xonsh.jobs` applied in `stdout` mode but missed in `object` mode. We
need clear map.

## For community
⬇️ **Please click the 👍 reaction instead of leaving a `+1` or 👍
comment**

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-02 22:10:53 +02:00
Andy Kipp
a5f0308a5a
CommandPipeline: clean repr + a bit of code cleaning (#5369)
* XONSH_TRACE_SUBPROC returns more useful details.

* update

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Name obj to thread because it's thread!

* update

* wip

* wip

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-03 00:09:31 +05:30
Andy Kipp
c4c7d1cbd7
Xonfig: show sensitive env variables that could affect the shell behavior. (#5374)
* wip

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Added XONSH_SUBPROC_OUTPUT_FORMAT

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-02 22:46:22 +05:30
Andy Kipp
c5cb7044b5
feat: add subproc output format, autostrip singleline output (#5377)
### Motivation

* To have an ability to manage the output format added ``$XONSH_SUBPROC_OUTPUT_FORMAT`` to switch the way to return the output lines. Default ``stream_lines`` to return text. Alternative ``list_lines`` to return the list of lines. Also supported custom lambda function.
* Additionally the [proposal to change default behavior](https://github.com/xonsh/xonsh/pull/5377#discussion_r1587627131) for a single line case.
* Closes #3924 as soft solution.

### Before

```xsh
mkdir -p /tmp/tst && cd /tmp/tst && touch 1 2 3

$(ls)
# '1\n2\n3\n'

id $(whoami)
# id: ‘pc\n’: no such user: Invalid argument

du $(ls)
# du: cannot access '1'$'\n''2'$'\n''3'$'\n': No such file or directory

ls $(fzf)
# ls: cannot access 'FUNDING.yml'$'\n': No such file or directory
```

### After

```xsh
mkdir -p /tmp/tst && cd /tmp/tst && touch 1 2 3

$XONSH_SUBPROC_OUTPUT_FORMAT = 'list_lines'

$(ls)
# ['1', '2', '3']

[f for f in $(ls)]
# ['1', '2', '3']

id $(whoami)
# uid=501(user) gid=20(staff)

du $(ls)
# 0 1
# 0 2
# 0 3

ls $(fzf)
# FUNDING.yml

# etc
mkdir -p /tmp/@($(whoami))/dir
cat /etc/passwd | grep $(whoami)
```

### Notes

* It will be good to improve parser for cases like `mkdir -p /tmp/$(whoami)/dir`. PR is welcome!
* I named the default mode as `stream_lines` (instead of just `stream` or `raw`) because in fact we transform raw output into stream of lines and possibly reduce the length of output ([replacing `\r\n` to `\n`](c3a12b2a9c/xonsh/procs/pipelines.py (L380-L383))). May be some day we need to add raw "stream" output format.
* Now anybody can implement bash `IFS` behavior in [bashisms](https://github.com/xonsh/xontrib-bashisms).

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-05-02 11:48:25 -04:00
Andy Kipp
c3a12b2a9c
feat: add --no-env flag to avoid inheriting parent-shell env (#5370)
### Motivation

Basically we use `xonsh --no-rc` to have pure xonsh. 
I noticed many times when parent environment can affect `xonsh --no-rc` e.g. `$XONSH_HISTORY_FILE`.
Current workaround is `env -i @$(which xonsh) --no-rc` but I think we need to have pure xonsh way.

Closes #4921

### Before

```xsh
xonsh --no-rc
$QWE = 1
xonsh --no-rc
$QWE
# 1
```

### After
```xsh
xonsh --no-rc
$QWE = 1
xonsh --no-rc --no-env
$QWE
# Unknown environment variable: $QWE

$XONSH_ENV_INHERITED  # Marker to have a way to catch this situation.
# False
```

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Gil Forsyth <gforsyth@users.noreply.github.com>
2024-04-29 12:29:22 -04:00
Andy Kipp
f71b5adde8
Xonsh Zulip Community started (#5372)
* wip

* wip

---------

Co-authored-by: a <1@1.1>
2024-04-29 10:01:02 -04:00
Andy Kipp
61751c2319
jobs: catching ChildProcessError (#5365)
* Update jobs.py

* Create jobs_catch_no_process.rst

* Update jobs.py

* Update jobs_catch_no_process.rst

* Update jobs.py
2024-04-26 10:34:31 +02:00
Andy Kipp
8ab1b9a0ee
jobs: default representation changed to dict (#5363)
* update

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: a <1@1.1>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-04-26 10:26:24 +02:00
a
a8a604d4c5 wip 2024-04-26 13:37:19 +05:30
a
24fdfece1e XONSH_TRACE_SUBPROC returns more useful details. 2024-04-26 13:37:19 +05:30
Andy Kipp
26c6e119e2
Shortcut for shell-type (#5367)
* Shortcut for shell-type

* Shortcut for shell-type

---------

Co-authored-by: a <1@1.1>
2024-04-25 20:10:18 +02:00
Andy Kipp
4dc17ca600
Downgrade AppImage to 3.11 (#5364)
* Update rever.xsh

* Create appimage_py311.rst
2024-04-25 11:46:36 +02:00