Skip to content

Commit

Permalink
refactor(aliases): source foreign shell funcs without interactive mode (
Browse files Browse the repository at this point in the history
#5344)

Co-authored-by: Noortheen Raja <jnoortheen@gmail.com>
  • Loading branch information
gforsyth and jnoortheen committed Apr 18, 2024
1 parent 97b6e1b commit fde07cd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
25 changes: 25 additions & 0 deletions news/source_foreign_notinteractive.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**Added:**

* <news item>

**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:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
1 change: 1 addition & 0 deletions tests/aliases/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ def test_source_foreign_fn_parser(alias, xession):
"suppress_skip_message",
"show",
"dryrun",
"interactive",
]
24 changes: 19 additions & 5 deletions xonsh/aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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"
)

Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit fde07cd

Please sign in to comment.