59 Commits

Author SHA1 Message Date
Mike McQuaid
e7159d733c
Revert "cmd/shellenv: set XDG_DATA_DIRS on Linux" 2024-10-30 18:27:54 -07:00
Conlan Cesar
f7b8681bc9
Add zsh site-functions to fpath
I considered doing a fallthough in the case-statement, but that doesn't work on bash leq 4 (notably, those versions shipped with MacOS by default).

I chose to prepend the value to the array to mirror the homebrew completion [instructions page](https://docs.brew.sh/Shell-Completion).

I also chose to leave off the `-d`irectory check -- zsh seems to tolerate invalid paths, so there isn't much harm in adding it anyway.

I'm flexible on any of these choices should someone feel strongly. However, I imagine this is the best combination given the trade-offs we have.
2024-10-15 03:20:50 +08:00
notfirefox
12982a2d97 cmd/shellenv: set XDG_DATA_DIRS on Linux
Some programs like `vapigen` might not work correctly, when `XDG_DATA_DIRS`
does not include `$HOMEBREW_PREFIX/share`. Instead of requiring the user
to manually adjust the shell environment, we can set `XDG_DATA_DIRS` as
part of `cmd/shellenv`, so that it is sufficient to run `brew shellenv`
on any particular system.

Fixes #18317
2024-09-17 13:54:53 +02:00
Carlo Cabrera
623e1766dd
cmd/shellenv: use long flags for fish
This will make the output more readable. We tend to prefer long flags
elsewhere anyway.
2024-09-12 16:49:23 +08:00
WinkelCode
d6043f1554 shellenv.sh: Move fish PATHs to front if they exist 2024-09-10 22:46:17 +02:00
Carlo Cabrera
4f03226a10
shellenv: silence errors from writing to HOMEBREW_PATHS_FILE
Fixes #18301.
2024-09-11 03:26:54 +08:00
Carlo Cabrera
9d60472090
shellenv: retain user PATH
This is filtered in `brew`, so we need to manually set this to
`HOMEBREW_PATH`.
2024-09-02 21:12:36 +08:00
Carlo Cabrera
c9ca7e3257
cmd/shellenv: output the result of path_helper instead
This will save the indirection of `eval`.
2024-09-02 17:36:17 +08:00
Carlo Cabrera
43d10940b9
Revert "Revert "cmd/shellenv: improve for newer macOS using /usr/libexec/path_helper"" 2024-09-02 17:32:05 +08:00
Carlo Cabrera
121624e0c2
Revert "cmd/shellenv: improve for newer macOS using /usr/libexec/path_helper" 2024-09-02 17:02:08 +08:00
Carlo Cabrera
6cd74705cb
shellenv: silence errors from writing to etc/paths file
Users don't need to see this error. We can just fall back silently
instead.
2024-08-29 19:12:23 +08:00
Carlo Cabrera
04088b3e3c
shellenv: extract paths file to a variable
Let's follow the Rule of Three here.
2024-08-29 17:36:50 +08:00
Carlo Cabrera
6dc90cbf83
cmd/shellenv: improve for newer macOS using /usr/libexec/path_helper
On macOS 14 and newer, `/usr/libexec/path_helper` supports setting a
`PATH_HELPER_ROOT` environment variable.

With this set, `path_helper` checks `$PATH_HELPER_ROOT/etc/paths` and
`$PATH_HELPER_ROOT/etc/paths.d` in the same way it checks `/etc/paths`
and `/etc/paths.d`.

We can use this to simplify management of the user's `PATH` variable
when they do `brew shellenv`. In particular, if their system supports
it, we delegate setting the `PATH` environment variable to `path_helper`
instead of our own code. We also write a default `etc/paths` file if one
is not already present.

This is nicer because it simplifies management of the user's `PATH`
variable. For example, if a user wants a keg-only formula to be in their
`PATH`, they can simply add the necessary path to `/etc/paths` or
`/etc/paths.d` without having to do something like `brew link --force`
or adding to `PATH` themselves.

When `/usr/libexec/path_helper` is not available, we just fall back to
the existing code.
2024-08-29 17:00:43 +08:00
Scott Searcy
17855638c2 Update Fish shell environment script
Fish didn't support `$(...)` substitution until v3.4.0, but the command
didn't work with just parenthesis, so rewrite the test to prepend an
empty string if MANPATH is non-empty (to trigger a leading colon).
Only manipulate INFOPATH in Fish if Homebrew not in path already.
2024-08-10 14:34:59 -07:00
Carlo Cabrera
3fec77e72a
shellenv: fix MANPATH and INFOPATH handling for [t]csh
It turns out that control flow in csh is a pain. But we can rely on
`test`, which is thankfully POSIX.

Closes #17949.
2024-08-04 02:52:14 +08:00
Mike McQuaid
5987c5c1d0
Add stub Ruby commands for all Bash commands.
This gets us pretty similar (but easier to manage) manpage output but
much nicer completions etc. for all these commands.
2024-07-14 23:12:53 -04:00
Mike McQuaid
d187f2d6cc
cmd/shellenv.sh: handle undefined MANPATH and set -eu.
See https://github.com/Homebrew/brew/pull/17633#issuecomment-2218657931
2024-07-11 08:17:11 +01:00
Adrian Ho
4ff36552f6 shellenv: prepend colon to MANPATH if set
The current appended colon means system man pages always shadow
Homebrew's. There's also no point adding Homebrew's man dir, nor
filling out an empty MANPATH, since `man` and friends will add the
necessary dirs according to PATH.

Closes https://github.com/Homebrew/homebrew-core/issues/176037.

Also fixed a syntax error in the `*csh` INFOPATH setting.
2024-07-06 01:46:22 +08:00
Paulo Phagula
8ae063acb1
Remove unnecessary pre-setting of PATH 2024-02-04 13:51:45 +01:00
Paulo Phagula
cf4876ac41
Use -P flag to Manipulate $PATH directly.
Based on https://github.com/Homebrew/brew/pull/7215
2024-02-04 13:51:45 +01:00
Paulo Phagula
dc4c827a51
fix: use recommended way of updating Fish shell $PATH
Akin to https://github.com/Homebrew/brew/pull/11349
2024-02-04 13:51:45 +01:00
Xuehai Pan
6f0d87aace docs: prefer placing shellenv command in rcfile rather than shell profile on Linux 2023-10-07 00:14:26 +08:00
James Morris
793831cb93 fix eval (brew shellenv) from exiting with status 1 in fish
fish's `set` doesn't modify the exit status when assigning a variable.
This means that `set -q VAR; or set VAR ...` will return an exit status
of 1 even if the variable is suceessfully set. By switching the commands
to `! set -q VAR; and set VAR ...`, this prevents the 1 from propogating
when running `eval (brew shellenv)`.
2023-09-30 17:39:39 -04:00
Austin Ziegler
74b50ddc89
Fix small errors in brew shellenv
These were introduced by typos and an incorrect assumption on the last
update of #15358. The last commit had been tested with a parameter, but
had not been re-tested *without* parameter.

Apologies for the error.
2023-05-08 11:06:30 -04:00
Austin Ziegler
8719946036
Allow brew shellenv to accept a shell name param
Resolves #15358

With this change, `brew shellenv` will accept a shell name parameter to
override its default output (based on `/bin/ps`) for advanced use cases
such as with dotfile templating and caching with
[twpayne/chezmoi](https://github.com/twpayne/chezmoi/discussions/2971).
When running under `fish`, this is the output:

```console
$ brew shellenv pwsh
[System.Environment]::SetEnvironmentVariable('HOMEBREW_PREFIX','/opt/homebrew',[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('HOMEBREW_CELLAR','/opt/homebrew/Cellar',[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('HOMEBREW_REPOSITORY','/opt/homebrew',[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('PATH',$('/opt/homebrew/bin:/opt/homebrew/sbin:'+$ENV:PATH),[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('MANPATH',$('/opt/homebrew/share/man'+$(if(${ENV:MANPATH}){':'+${ENV:MANPATH}})+':'),[System.EnvironmentVariableTarget]::Process)
[System.Environment]::SetEnvironmentVariable('INFOPATH',$('/opt/homebrew/share/info'+$(if(${ENV:INFOPATH}){':'+${ENV:INFOPATH}})),[System.EnvironmentVariableTarget]::Process)
$ brew shellenv
set -gx HOMEBREW_PREFIX "/opt/homebrew";
set -gx HOMEBREW_CELLAR "/opt/homebrew/Cellar";
set -gx HOMEBREW_REPOSITORY "/opt/homebrew";
set -q PATH; or set PATH ''; set -gx PATH "/opt/homebrew/bin" "/opt/homebrew/sbin" $PATH;
set -q MANPATH; or set MANPATH ''; set -gx MANPATH "/opt/homebrew/share/man" $MANPATH;
set -q INFOPATH; or set INFOPATH ''; set -gx INFOPATH "/opt/homebrew/share/info" $INFOPATH;
```

The specific case presented in the mentioned discussion could be
mitigated by an additional level of indirection (`{{ output "fish" "-c"
"'brew shellenv'" | trim }}`), that requires that the running system
have the target shells installed, when they are not strictly necessary
with `brew shellenv`.
2023-05-05 11:50:48 -04:00
Adrian Ho
75c5769e75 shellenv: add explanatory MANPATH comment
Resolves #13867 and (hopefully) future attempts to correct the intended functionality.
2022-09-15 23:00:34 +08:00
Nanda H Krishna
b950ac3f6e
cmd/shellenv.sh: fix typo
Co-authored-by: Greg Myers <myersg86@gmail.com>
2022-01-15 17:27:49 -05:00
Carlo Cabrera
00137dda8e
Check that ${HOMEBREW_PREFIX}/bin is first in PATH 2021-12-09 20:50:47 +08:00
Carlo Cabrera
a62e88304b
shellenv: check that Homebrew's prefix precedes /usr/bin
Some setups (e.g. a default `tmux` session) will result in a user
starting multiple nested login shells. This is a problem for the way we
currently handle `shellenv` because a login shell invokes
`/usr/libexec/path_helper` which will prepend the contents of
`/etc/paths` and `/etc/paths.d` to `PATH`. In particular, the second
login shell will place `brew` further down the `PATH` than desired in a
non-`/usr/local` prefix, since `brew shellenv` will no longer produce
output the second time around.

Let's fix this by replacing the check that `brew` is in `PATH` with a
check that `${HOMEBREW_PREFIX}/bin` precedes `/usr/bin` in `PATH`.

Fixes #11851, #11883, Homebrew/discussions#2547.

See also:
- Homebrew/discussions#2237
- https://superuser.com/questions/544989/does-tmux-sort-the-path-variable
2021-12-09 20:50:22 +08:00
Julian Pawlowski
29f08603bf
Add support for PowerShell-preview package 2021-12-01 09:55:33 +01:00
Julian Pawlowski
cec3f2190a
Add shellenv template for PowerShell 2021-11-30 10:47:56 +01:00
XuehaiPan
bd8db0737d style: fix inconsistent code style for shell scripts 2021-09-16 01:06:19 +08:00
XuehaiPan
3f96d963f7 style: fix inconsistent code style for shell scripts 2021-09-15 14:59:01 +08:00
XuehaiPan
05c1c5895e Apply suggestions from code review 2021-08-30 22:37:07 +08:00
Xuehai Pan
27fb6506a0
Apply suggestions from code review
Co-authored-by: Carlo Cabrera <30379873+carlocab@users.noreply.github.com>
2021-08-27 18:22:01 +08:00
XuehaiPan
728efbdf2f Show warnings on brew shellenv if users modified env vars explicitly 2021-08-27 15:55:31 +08:00
XuehaiPan
1e737dbe2c cmd/shellenv.sh: apply suggestions from code review 2021-08-03 18:40:59 +08:00
XuehaiPan
6cd2192c6f cmd/shellenv.sh: make sure the current Homebrew is initialized correctly 2021-08-03 16:19:45 +08:00
XuehaiPan
7cc37fb7f8 cmd/shellenv.sh: support multi-instance installation 2021-08-02 16:57:59 +08:00
XuehaiPan
9e11fcf31e cmd/shellenv.sh: make brew shellenv idempotent 2021-07-30 21:00:38 +08:00
hyuraku
67c5006b0a cmd/shellenv.sh: repair style and set HOMEBREW_CASK_REPOSITORY 2021-04-28 20:47:39 +09:00
Z. Grace Moreau
a257d046ef remove spurious call to $fish_pid 2021-01-18 11:20:11 -07:00
Z. Grace Moreau
0dbd4157be query fish's PID correctly; also output executable names only 2021-01-17 14:25:09 -07:00
Tim Dorr
45368c8119 Add a default back to INFOPATH
It looks like #7738 removed the default unintentionally. This adds it back. 

For scripts that `set -u`, this will fail if $INFOPATH is not already set (`INFOPATH: unbound variable`). This provides an empty default, but one that will still satisfy the trailing colon requirement.
2020-07-19 13:37:52 +10:00
Adrian Ho
c166667666 shellenv: Fix shell detection
It currently switches on `$SHELL`, which points to the user's login shell. However, `shellenv` may be used in contexts where the running shell isn't `$SHELL` (e.g. csh-based `cron` scripts), and Linux desktop environments may set up user sessions in ways that trip up the current algorithm (see #7965 for an example). To work correctly in all circumstances and environments, its output should be determined by the context (calling shell) instead.

Fixes #7965.
2020-07-11 22:28:27 +08:00
Dan Kessler
30b04daa8a append colon (:) to end of brew's part of INFOPATH in brew shellenv
The behavior of some programs (e.g., emacs) differs depending on
whether INFOPATH is terminated in a colon or not. For example, the
info viewer built into emacs has a default list of places from which
it will draw info files, and it will only check this if either (a)
INFOPATH is unset, or (b) INFOPATH ends in a colon.

Currently brew shellenv prepends to the existing $INFOPATH and will
separate with a colon if $INFOPATH already exists, but if it does not
it will not terminate in a colon. As a result, info
pages (like emacs and elisp) are missing from the info viewer.

The existing brew shellenv logic used a parameter expansion trick to
include a colon only if it was prepending to an extant INFOPATH, but
because we want brew's contribution to the INFOPATH to always end in a
colon (whether there's an existing INFOPATH or not), we don't have to
use the parameter expansion trick

This commit only changes behavior for "other" shells (i.e., not fish,
csh, or tcsh) since I don't know how to append a colon to variables in
these shells.
2020-06-12 20:47:43 -04:00
April Arcus
0b4febceaf fix undefined variable interpolation in tcsh 2020-05-25 10:46:10 -07:00
Shaun Jackman
133ee7966d shell: Use .profile unless .bash_profile exists 2020-05-13 09:30:02 -07:00
Mike McQuaid
c05b3a755f
shellenv: recommend .bash_profile
Fixes #7516
2020-05-07 10:34:59 +01:00
Niklas Herder
2f4d4b8684
[fix] Improve shell detection in shellenv
$SHELL is set to only the binary name in some environments (e.g. fish, not /usr/bin/fish), causing detection of fish/csh/tsch to fail without this change.
2020-04-29 08:20:06 +02:00