From fde07cda5f6ef113d8c845017ab86cd2eab08193 Mon Sep 17 00:00:00 2001 From: Gil Forsyth Date: Thu, 18 Apr 2024 10:21:39 -0400 Subject: [PATCH] refactor(aliases): source foreign shell funcs without interactive mode (#5344) Co-authored-by: Noortheen Raja --- news/source_foreign_notinteractive.rst | 25 +++++++++++++++++++++++++ tests/aliases/test_source.py | 1 + xonsh/aliases.py | 24 +++++++++++++++++++----- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 news/source_foreign_notinteractive.rst diff --git a/news/source_foreign_notinteractive.rst b/news/source_foreign_notinteractive.rst new file mode 100644 index 0000000000..291f72b116 --- /dev/null +++ b/news/source_foreign_notinteractive.rst @@ -0,0 +1,25 @@ +**Added:** + +* + +**Changed:** + +* ``source_foreign_fn`` now does not run subshells in interactive mode, so + associated RC files like ``zshrc`` and ``bashrc`` will not be auto-loaded on + sourcing. + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/tests/aliases/test_source.py b/tests/aliases/test_source.py index 8791aab728..07401b6c1f 100644 --- a/tests/aliases/test_source.py +++ b/tests/aliases/test_source.py @@ -78,4 +78,5 @@ def test_source_foreign_fn_parser(alias, xession): "suppress_skip_message", "show", "dryrun", + "interactive", ] diff --git a/xonsh/aliases.py b/xonsh/aliases.py index e75a5c79bb..e1b59c9f17 100644 --- a/xonsh/aliases.py +++ b/xonsh/aliases.py @@ -406,7 +406,7 @@ def xonsh_reset(args, stdin=None): def source_foreign_fn( shell: str, files_or_code: Annotated[list[str], Arg(nargs="+")], - interactive=True, + interactive=False, login=False, envcmd=None, aliascmd=None, @@ -433,7 +433,7 @@ def source_foreign_fn( Name or path to the foreign shell files_or_code file paths to source or code in the target language. - interactive : -n, --non-interactive + interactive : -i, --interactive whether the sourced shell should be interactive login : -l, --login whether the sourced shell should be login @@ -547,7 +547,21 @@ def source_foreign_fn( print(msg.format(k, shell), file=_stderr) -source_foreign = ArgParserAlias( +class SourceForeignAlias(ArgParserAlias): + def build(self): + parser = self.create_parser(**self.kwargs) + # for backwards compatibility + parser.add_argument( + "-n", + "--non-interactive", + action="store_false", + dest="interactive", + help="Deprecated: The default mode runs in non-interactive mode.", + ) + return parser + + +source_foreign = SourceForeignAlias( func=source_foreign_fn, has_args=True, prog="source-foreign" ) @@ -847,12 +861,12 @@ def make_default_aliases(): "exec": xexec, "xexec": xexec, "source": source_alias, - "source-zsh": ArgParserAlias( + "source-zsh": SourceForeignAlias( func=functools.partial(source_foreign_fn, "zsh", sourcer="source"), has_args=True, prog="source-zsh", ), - "source-bash": ArgParserAlias( + "source-bash": SourceForeignAlias( func=functools.partial(source_foreign_fn, "bash", sourcer="source"), has_args=True, prog="source-bash",