Repository: xonsh/xonsh

Issue:
fix(xpip): never add `sudo`, append `--user` instead
Fixes #4878 
Fixes #4369 

## For community
⬇️  **Please click the 👍 reaction instead of leaving a `+1` or 👍  comment**



Discussion:
# [Codecov](https://codecov.io/gh/xonsh/xonsh/pull/4879?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh) Report
> Merging [#4879](https://codecov.io/gh/xonsh/xonsh/pull/4879?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh) (ebb0558) into [main](https://codecov.io/gh/xonsh/xonsh/commit/c8c1fd1071136feb1c2eb750b9995c5c4c3eaad3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh) (c8c1fd1) will **increase** coverage by `4.04%`.
> The diff coverage is `0.00%`.

```diff
@@            Coverage Diff             @@
##             main    #4879      +/-   ##
==========================================
+ Coverage   62.68%   66.72%   +4.04%     
==========================================
  Files         123      129       +6     
  Lines       21778    23665    +1887     
  Branches     4259     4700     +441     
==========================================
+ Hits        13651    15791    +2140     
+ Misses       6978     6645     -333     
- Partials     1149     1229      +80     
```

| Flag | Coverage Δ | |
|---|---|---|
| macOS-latest | `63.69% <0.00%> (?)` | |
| ubuntu-latest | `64.28% <0.00%> (?)` | |
| windows-latest | `62.61% <0.00%> (-0.07%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/xonsh/xonsh/pull/4879?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh) | Coverage Δ | |
|---|---|---|
| [xonsh/aliases.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvYWxpYXNlcy5weQ==) | `64.11% <0.00%> (+2.63%)` | :arrow_up: |
| [xonsh/prompt/vc.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvcHJvbXB0L3ZjLnB5) | `72.56% <0.00%> (-0.61%)` | :arrow_down: |
| [xonsh/main.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvbWFpbi5weQ==) | `83.67% <0.00%> (ø)` | |
| [xonsh/\_\_main\_\_.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvX19tYWluX18ucHk=) | `100.00% <0.00%> (ø)` | |
| [xonsh/tools.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvdG9vbHMucHk=) | `71.54% <0.00%> (ø)` | |
| [xonsh/\_\_init\_\_.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvX19pbml0X18ucHk=) | `100.00% <0.00%> (ø)` | |
| [xonsh/completer.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvY29tcGxldGVyLnB5) | `79.27% <0.00%> (ø)` | |
| [xonsh/shell.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvc2hlbGwucHk=) | `72.52% <0.00%> (ø)` | |
| [xonsh/parsers/base.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvcGFyc2Vycy9iYXNlLnB5) | `85.20% <0.00%> (+0.12%)` | :arrow_up: |
| [xonsh/ptk\_shell/shell.py](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh#diff-eG9uc2gvcHRrX3NoZWxsL3NoZWxsLnB5) | `57.18% <0.00%> (+0.29%)` | :arrow_up: |
| ... and [44 more](https://codecov.io/gh/xonsh/xonsh/pull/4879/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh) | |

:mega: Codecov can now indicate which changes are the most critical in Pull Requests. [Learn more](https://about.codecov.io/product/feature/runtime-insights/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=xonsh)

Thanks @gforsyth 
Beware that `--user` is an argument to the `install` command. I don't know if this would break `xpip list` etc or not?
It probably does.  We'll have to tackle that in a followup

Resolving patch:
diff --git a/xonsh/aliases.py b/xonsh/aliases.py
--- a/xonsh/aliases.py
+++ b/xonsh/aliases.py
@@ -815,7 +815,10 @@ def detect_xpip_alias():
             # XXX: Does windows have an installation mode that requires UAC?
             return basecmd
         elif not os.access(os.path.dirname(sys.executable), os.W_OK):
-            return basecmd.extend(["--user"])
+            return (
+                sys.executable
+                + " -m pip @(['install', '--user'] + $args[1:] if $args and $args[0] == 'install' else $args)"
+            )
         else:
             return basecmd
     except Exception:


Test patch: