Skip to content

Commit fad40cf

Browse files
authored
fix: improve Fish shell support (and Cshell) (#1429)
I faced the same issue mentioned in #1299, this PR fixes the issue by taking in account specific `set` and `unset` syntax for Fish shell. The post install instruction (i.e : `./emsdk activate latest`) provides wrong instructions in Fish shell and Cshell cases. This should be fixed by this PR too, although the code is a bit redundant and it could be more cleaner.
1 parent cc7ef52 commit fad40cf

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

emsdk.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,12 @@ def exit_with_error(msg):
119119
CSH = bool(os.getenv('EMSDK_CSH'))
120120
CMD = bool(os.getenv('EMSDK_CMD'))
121121
BASH = bool(os.getenv('EMSDK_BASH'))
122+
FISH = bool(os.getenv('EMSDK_FISH'))
123+
122124
if WINDOWS and BASH:
123125
MSYS = True
124126

125-
if not CSH and not POWERSHELL and not BASH and not CMD:
127+
if not CSH and not POWERSHELL and not BASH and not CMD and not FISH:
126128
# Fall back to default of `cmd` on windows and `bash` otherwise
127129
if WINDOWS and not MSYS:
128130
CMD = True
@@ -1503,6 +1505,22 @@ def find_emscripten_root(active_tools):
15031505
return root
15041506

15051507

1508+
# returns a tuple (string,string) of config files paths that need to used
1509+
# to activate emsdk env depending on $SHELL, defaults to bash.
1510+
def get_emsdk_shell_env_configs():
1511+
default_emsdk_env = sdk_path('emsdk_env.sh')
1512+
default_shell_config_file = '$HOME/.bash_profile'
1513+
shell = os.getenv('SHELL', '')
1514+
if 'zsh' in shell:
1515+
return (default_emsdk_env, '$HOME/.zprofile')
1516+
elif 'csh' in shell:
1517+
return (sdk_path('emsdk_env.csh'), '$HOME/.cshrc')
1518+
elif 'fish' in shell:
1519+
return (sdk_path('emsdk_env.fish'), '$HOME/.config/fish/config.fish')
1520+
else:
1521+
return (default_emsdk_env, default_shell_config_file)
1522+
1523+
15061524
def generate_em_config(active_tools, permanently_activate, system):
15071525
cfg = 'import os\n'
15081526
cfg += "emsdk_path = os.path.dirname(os.getenv('EM_CONFIG')).replace('\\\\', '/')\n"
@@ -1556,22 +1574,16 @@ def generate_em_config(active_tools, permanently_activate, system):
15561574
print('- Consider running `emsdk activate` with --permanent or --system')
15571575
print(' to have emsdk settings available on startup.')
15581576
else:
1559-
emsdk_env = sdk_path('emsdk_env.sh')
15601577
print('Next steps:')
15611578
print('- To conveniently access emsdk tools from the command line,')
15621579
print(' consider adding the following directories to your PATH:')
15631580
for p in path_add:
15641581
print(' ' + p)
15651582
print('- This can be done for the current shell by running:')
1583+
emsdk_env, shell_config_file = get_emsdk_shell_env_configs()
15661584
print(' source "%s"' % emsdk_env)
15671585
print('- Configure emsdk in your shell startup scripts by running:')
1568-
shell = os.getenv('SHELL', '')
1569-
if 'zsh' in shell:
1570-
print(' echo \'source "%s"\' >> $HOME/.zprofile' % emsdk_env)
1571-
elif 'csh' in shell:
1572-
print(' echo \'source "%s"\' >> $HOME/.cshrc' % emsdk_env)
1573-
else:
1574-
print(' echo \'source "%s"\' >> $HOME/.bash_profile' % emsdk_env)
1586+
print(' echo \'source "%s"\' >> %s' % (emsdk_env, shell_config_file))
15751587

15761588

15771589
def find_msbuild_dir():
@@ -2578,6 +2590,8 @@ def unset_env(key):
25782590
return 'set %s=\n' % key
25792591
if CSH:
25802592
return 'unsetenv %s;\n' % key
2593+
if FISH:
2594+
return 'set -e %s;\n' % key
25812595
if BASH:
25822596
return 'unset %s;\n' % key
25832597
assert False
@@ -2599,6 +2613,8 @@ def construct_env_with_vars(env_vars_to_add):
25992613
env_string += 'SET ' + key + '=' + value + '\n'
26002614
elif CSH:
26012615
env_string += 'setenv ' + key + ' "' + value + '";\n'
2616+
elif FISH:
2617+
env_string += 'set -gx ' + key + ' "' + value + '";\n'
26022618
elif BASH:
26032619
env_string += 'export ' + key + '="' + value + '";\n'
26042620
else:
@@ -2616,7 +2632,7 @@ def construct_env_with_vars(env_vars_to_add):
26162632
# of the SDK but we want to remove that from the current environment
26172633
# if no such tool is active.
26182634
# Ignore certain keys that are inputs to emsdk itself.
2619-
ignore_keys = set(['EMSDK_POWERSHELL', 'EMSDK_CSH', 'EMSDK_CMD', 'EMSDK_BASH',
2635+
ignore_keys = set(['EMSDK_POWERSHELL', 'EMSDK_CSH', 'EMSDK_CMD', 'EMSDK_BASH', 'EMSDK_FISH'
26202636
'EMSDK_NUM_CORES', 'EMSDK_NOTTY', 'EMSDK_KEEP_DOWNLOADS'])
26212637
env_keys_to_add = set(pair[0] for pair in env_vars_to_add)
26222638
for key in os.environ:

emsdk_env.fish

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#Now, when you want to use the SDK, run this alias first to set up
44
#your environment.
55

6+
set -gx EMSDK_FISH 1
7+
68
set -l script (status -f)
79
set -l dir (dirname $script)
810

0 commit comments

Comments
 (0)