Fix reading of /etc/paths on OS X

(and /etc/paths.d/*)

Do so by emulating the behavior of /usr/libexec/path_helper for login
shells, matching the behavior in /etc/profile.

Also add a path_helper command to reproduce the behavior of
/usr/libexec/path_helper for fish.

This also handles setting MANPATH if necessary.

Fixes issue #4336
This commit is contained in:
Frederick Akalin 2018-03-27 23:35:41 -07:00 committed by ridiculousfish
parent c116843611
commit adbaddfaf4

View file

@ -126,59 +126,7 @@ if test -d /usr/xpg4/bin
end
end
# OS X-ism: Load the path files out of /etc/paths and /etc/paths.d/*
set -g __fish_tmp_path $PATH
function __fish_load_path_helper_paths
# We want to rearrange the path to reflect this order. Delete that path component if it exists and then prepend it.
# Since we are prepending but want to preserve the order of the input file, we reverse the array, append, and then reverse it again
set __fish_tmp_path $__fish_tmp_path[-1..1]
while read -l new_path_comp
if test -d $new_path_comp
set -l where (contains -i -- $new_path_comp $__fish_tmp_path)
and set -e __fish_tmp_path[$where]
set __fish_tmp_path $new_path_comp $__fish_tmp_path
end
end
set __fish_tmp_path $__fish_tmp_path[-1..1]
end
test -r /etc/paths
and __fish_load_path_helper_paths </etc/paths
for pathfile in /etc/paths.d/*
__fish_load_path_helper_paths <$pathfile
end
set -xg PATH $__fish_tmp_path
set -e __fish_tmp_path
functions -e __fish_load_path_helper_paths
# OS X-ism: Load the manpath files out of /etc/manpaths and /etc/manpaths.d/*
if set -q MANPATH
set -g __fish_tmp_manpath $MANPATH
function __fish_load_manpath_helper_manpaths
# We want to rearrange the manpath to reflect this order. Delete that manpath component if it exists and then prepend it.
# Since we are prepending but want to preserve the order of the input file, we reverse the array, append, and then reverse it again
set __fish_tmp_manpath $__fish_tmp_manpath[-1..1]
while read -l new_manpath_comp
if test -d $new_manpath_comp
set -l where (contains -i -- $new_manpath_comp $__fish_tmp_manpath)
and set -e __fish_tmp_manpath[$where]
set __fish_tmp_manpath $new_manpath_comp $__fish_tmp_manpath
end
end
set __fish_tmp_manpath $__fish_tmp_manpath[-1..1]
end
test -r /etc/manpaths
and __fish_load_manpath_helper_manpaths </etc/manpaths
for manpathfile in /etc/manpaths.d/*
__fish_load_manpath_helper_manpaths <$manpathfile
end
set -xg MANPATH $__fish_tmp_manpath
set -e __fish_tmp_manpath
functions -e __fish_load_manpath_helper_manpaths
end
# Add a handler for when fish_user_path changes, so we can apply the same changes to PATH
# Invoke it immediately to apply the current value of fish_user_path
function __fish_reconstruct_path -d "Update PATH when fish_user_paths changes" --on-variable fish_user_paths
set -l local_path $PATH
@ -202,8 +150,6 @@ function __fish_reconstruct_path -d "Update PATH when fish_user_paths changes" -
set -xg PATH $local_path
end
__fish_reconstruct_path
#
# Launch debugger on SIGTRAP
#
@ -259,7 +205,40 @@ end
# This used to be in etc/config.fish - keep it here to keep the semantics
#
# Adapt construct_path from the macOS /usr/libexec/path_helper
# executable for fish; see
# https://opensource.apple.com/source/shell_cmds/shell_cmds-203/path_helper/path_helper.c.auto.html .
function __fish_macos_set_env -d "set an environment variable like path_helper does (macOS only)"
set -l result
for path_file in $argv[2] $argv[3]/*
if test -f $path_file
while read -la entry
if not contains $entry $result
set result $result $entry
end
end <$path_file
end
end
for entry in $$argv[1]
if not contains $entry $result
set result $result $entry
end
end
set -xg $argv[1] $result
end
if status --is-login
# macOS-ism: Emulate calling path_helper.
if command -sq /usr/libexec/path_helper
__fish_macos_set_env 'PATH' '/etc/paths' '/etc/paths.d'
if [ -n "$MANPATH" ]
__fish_macos_set_env 'MANPATH' '/etc/manpaths' '/etc/manpaths.d'
end
end
#
# Put linux consoles in unicode mode.
#
@ -271,3 +250,7 @@ if status --is-login
end
end
end
# Invoke this here to apply the current value of fish_user_path after
# PATH is possibly set above.
__fish_reconstruct_path