Don't source variables.sh if OPAM_SWITCH_PREFIX is already set#6729
Don't source variables.sh if OPAM_SWITCH_PREFIX is already set#6729rjbou merged 4 commits intoocaml:masterfrom
variables.sh if OPAM_SWITCH_PREFIX is already set#6729Conversation
|
While this is in the same area as #6603 (in particular, you can see that a normal |
There was a problem hiding this comment.
The change seems good overall, however last commit Show the "nuking the root" effect should be included in the second Add a test for variables.sh double-applying commit instead of being its own commit. Otherwise it is a bit confusing. Personally i had a hard time understanding what it was doing and then i understood once i read the second commit again.
Once done and the master_changes updated, lgtm
| export HOME="$PWD" | ||
| export SHELL="$(command -v sh)" |
There was a problem hiding this comment.
| export HOME="$PWD" | |
| export SHELL="$(command -v sh)" | |
| set -eu | |
| export HOME=$PWD | |
| export SHELL=$(command -v sh) |
There was a problem hiding this comment.
I get too used to the shellcheck brigade complaining about SC2086 🫣 I don't mind, but this three-line "script" doesn't need set -eu?
There was a problem hiding this comment.
the current script no but i'd rather have it to be more future-proof
src/state/opamEnv.ml
Outdated
|
|
||
| let abort_if_set var shell = | ||
| match shell with | ||
| | SH_zsh | SH_bash | SH_sh -> Printf.sprintf "test -z \"$%s\" || return\n" var |
There was a problem hiding this comment.
Fish also works with the same syntax
| | SH_zsh | SH_bash | SH_sh -> Printf.sprintf "test -z \"$%s\" || return\n" var | |
| | SH_fish | SH_zsh | SH_bash | SH_sh -> Printf.sprintf "test -z \"$%s\" || return\n" var |
There was a problem hiding this comment.
Aw, I was trying to be all fish-idiomatic, given that we use ; or true in init.fish
There was a problem hiding this comment.
both styles seem idiomatic https://fishshell.com/docs/current/language.html#combiners-and-or
but if you prefer using and/or statment, i'd then rather have the same logic as sh/etc. here:
test -z \"$%s\"; or return\n
As a side note, if we were to do the same thing with zsh and bash, the generated code would also look different, probably more like:
[[ $OPAM_SWITCH_PREFIX = "" ]] || return
There was a problem hiding this comment.
Oo, the set -eu made me check that again, and it wasn't quite what was meant. The sh version is now technically resistant to set -u as well. fish's variable expansion is different - as far as I can tell, fish doesn't have any equivalent to set -u, so I've not gone with adding the set -q OPAM_SWITCH_PREFIX to test for unset as it doesn't seem to be necessary.
There was a problem hiding this comment.
Yeah fish's equivalent to set -u is a long term feature request tracked by fish-shell/fish-shell#4163. Lgtm
|
Thanks, @kit-ty-kate - I don't understand the request about the last commit. It's not testing the same thing as the second commit, and the test doesn't change by being earlier in the commit series (because it's not fixed by this fix), so I'm not seeing what putting it earlier is helping? I can remove it entirely for now, of course (basically, I wrote the test, started working on the fix, and realised that the fix is more work than I wanted to have to do by next week...!) |
|
#5036 was also an approach for this - however, I think that this technique of simply backing off is actually better, given that it means that that subshells then behave correctly. i.e. |
| @@ -1,12 +1,23 @@ | |||
| N0REP0 | |||
| ### rm -rf $OPAMROOT | |||
| ### opam init -na --bypass-checks --bare REPO --root root | |||
There was a problem hiding this comment.
Thinking about it a bit more, i think it might be nice to duplicate this test to another file to make sure we test both -na and -a
There was a problem hiding this comment.
Good idea - in fact, in splitting it, it should now run that test on Windows as well.
b6d16f6 to
3cbe335
Compare
aca4610 to
f298e9f
Compare
csh requires shell variables to be initialised when used with +=, etc. but this isn't necessary for unconditional assignment.
ce15291 to
3920c44
Compare
Environment variables registered in variables.sh remain even on opam env for a different switch.
3920c44 to
cc8e824
Compare
I believe the underlying cause of all the various reports we have had over the years of the environment being incorrect (most recently referred to in #6455 (comment)) are because the
variablesscript is not idempotent.You can see this on Linux easily by starting a subshell:
or:
What then leads to, for example, seeing multiple switch directories in
binis that any subsequent call toeval $(opam env)and friends will only revert one of those entries, not all of them.This can be concretely seen on macOS, where the default shell is
zsh- if opam has been allowed to update.zshrc, and from a terminal one launches VS Code, a terminal opened in VS Code correctly resources.zshrcand incorrectly ends up with a broken$PATH(you can't see the easy$MANPATHexample, because we don't set$MANPATHon macOS).I think the fix for this trivially simple - if the environment is already set, then the
variablesshould just stop. That means, for example, that if you start a subshell, then you correctly inherit the previous environment, which may even be a different switch. I set it up using thisDockerfile, which runs usingbashas the default shell, but for example executes withfishas the shell withdocker run --rm -it <sha> fish:I've utilised a little trick to allow the init-scripts test on Unix actually to test this. I've also put a test for the behaviour of #6455 at the end, but I haven't fixed that here. I think this fix is worth having in its own right, and is more important than the "I nuked my opam root" fix. However, we should fix that too at some point, because "I nuked my opam root" (which is often a sledgehammer response to a problem) is also "I had multiple terminals open, and I've removed the switch which was activated" (which suffers the same problem -
eval $(opam env)won't revert the previous changes.Fixes #4649
Fixes #6633