xonsh/news
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
..
5477.rst Perform proper case-insensitive matching in command_cache on Windows (#5477) 2024-06-10 12:29:13 -04:00
appimage_py311.rst Downgrade AppImage to 3.11 (#5364) 2024-04-25 11:46:36 +02:00
env_completion.rst Use substring for env completion and better way to sort list (#5388) 2024-05-03 10:30:14 +02:00
env_detype_all.rst Added `env.detype_all()` to get all available variables that is possible to detype. (#5431) 2024-05-22 07:53:11 +05:30
fix_dircolors.rst Mitigate exception on start up if `dircolors` binary is broken (#5439) 2024-05-27 20:37:30 +02:00
fix_interactive_suspended_subproc.rst Read stop signals from the process and update the process state. (#5361) 2024-05-22 11:45:39 -04:00
fix_io.rst Fixed I/O operation on closed file and Bad file descriptor exceptions after running callable aliases multiple times (#5437) 2024-05-23 14:07:30 -04:00
fix_nix.rst Added support of NixOS core tools in `predict_threadable` (#5440) 2024-05-24 16:51:25 -04:00
fix_noaccess.rst Make xonsh tolerant to inaccessible paths: history backend, script cache (#5430) 2024-05-20 21:14:14 +02:00
fix_print_exception.rst Fixed showing exception message (#5394) 2024-05-06 19:48:43 +05:30
fix_process_traceback.rst Fixed empty stacktrace for CalledProcessError (#5391) 2024-05-04 11:50:32 +02:00
fix_redir.rst Revert "Revert #5423" (#5475) 2024-06-02 14:53:00 +02:00
funcalias.rst feat: add superhelp and additional context via new FuncAlias (#5366) 2024-05-13 09:11:58 -04:00
history_save_unload.rst History: Saving history in case of any type of exiting the shell. (#5418) 2024-05-16 11:01:38 +05:30
jobs_catch_no_process.rst jobs: catching ChildProcessError (#5365) 2024-04-26 10:34:31 +02:00
jobs_repr.rst jobs: default representation changed to dict (#5363) 2024-04-26 10:26:24 +02:00
jupytext.rst Add Jupytext to news (#5402) 2024-05-10 11:03:44 +02:00
last_cp.rst Now last executed CommandPipeline is available in `__xonsh__.last` (#5422) 2024-05-22 09:25:35 -04:00
main_d.rst Fixed `xonsh -DVAR=VAL` behavior: initiate env variables before shell initialization. (#5396) 2024-05-06 17:39:10 +05:30
mini_refactor.rst CommandPipeline: clean repr + a bit of code cleaning (#5369) 2024-05-03 00:09:31 +05:30
no_env.rst feat: add --no-env flag to avoid inheriting parent-shell env (#5370) 2024-04-29 12:29:22 -04:00
popen_name.rst Add thread details to the exception (#5360) 2024-04-24 11:15:40 +02:00
prompt_env_name_yel.rst prompt: yellow env_name (#5457) 2024-05-28 22:25:21 -04:00
prompt_superuser.rst Show `root and @#` in prompt if user is superuser. (#5409) 2024-05-22 09:27:03 -04:00
refresh_specs.rst Refreshing procs/specs.py (#5424) 2024-05-26 10:59:36 +02:00
shell_type.rst Shortcut for shell-type (#5367) 2024-04-25 20:10:18 +02:00
sig_exit_fix.rst fix(signals): fix processing exit signals and exit exception (#5399) 2024-05-13 09:31:55 -04:00
sigint_save_hist.rst Saving history on SIGINT (#5425) 2024-05-22 10:20:08 -04:00
subproc_output_format.rst feat: add subproc output format, autostrip singleline output (#5377) 2024-05-02 11:48:25 -04:00
TEMPLATE.rst single instance of string.Formatter 2018-10-20 12:22:59 -06:00
thr_unthr.rst Added SpecModifierAlias and xthread, xunthread aliases (#5443) 2024-05-28 11:03:45 -04:00
trace3.rst Added `$XONSH_TRACE_SUBPROC=3` (#5459) 2024-05-31 12:50:25 +02:00
trace_subproc.rst wip 2024-04-26 13:37:19 +05:30
tutorial_callias_capturing.rst Tutorial: Callable alias and capturing (#5472) 2024-06-10 09:42:00 -04:00
typerr.rst Fix 5379, 5429 (#5432) 2024-05-22 08:27:44 +05:30
unpin_ptk.rst Unpin prompt-toolkit version (#5438) 2024-05-23 14:08:54 -04:00
waitpid_returncode.rst Fixed populating the return code for interrupted process. (#5380) 2024-05-02 22:10:53 +02:00
xonfig_env.rst Xonfig: show sensitive env variables that could affect the shell behavior. (#5374) 2024-05-02 22:46:22 +05:30
xonshrc.rst Do not load ~/.xonshrc in not interactive mode (#5491) 2024-06-13 10:08:23 -04:00
zulip.rst Xonsh Zulip Community started (#5372) 2024-04-29 10:01:02 -04:00