Skip to content

Update PATH in place in env hook - #6859

Merged
kit-ty-kate merged 1 commit into
ocaml:masterfrom
gridbugs:env-hook-inplace-path
Jul 13, 2026
Merged

Update PATH in place in env hook#6859
kit-ty-kate merged 1 commit into
ocaml:masterfrom
gridbugs:env-hook-inplace-path

Conversation

@gridbugs

Copy link
Copy Markdown
Contributor

Prior to this change, the default behaviour of opam init was to register a shell hook which ran before each command, and added/moved the bin dir from the current opam switch to the beginning of the PATH variable. If a user has a custom bin directory at the beginning of their PATH containing an executable with the same name as an executable in their current opam switch, the executable in the opam switch will take precedence.

This commit changes the behaviour of the shell hook to update the existing opam switch in PATH in place rather than prepending it, so the precedence of the opam switch relative to other dirs in PATH is preserved.

This change doesn't affect the behaviour of running eval $(opam env).

Fixes #6815

@gridbugs
gridbugs force-pushed the env-hook-inplace-path branch 2 times, most recently from 6872608 to b896271 Compare February 16, 2026 10:47
@rjbou

rjbou commented Feb 18, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR! We'll discuss it in the next opam dev meeting.

@rjbou
rjbou requested review from kit-ty-kate and rjbou and removed request for rjbou February 18, 2026 17:10

@kit-ty-kate kit-ty-kate left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea, thanks for bringing options that i didn't know about.

After testing it live, i think the documentation of the option is somewhat misleading as to its behaviour when adding another switch directory to the PATH manually. The documentation says that it "replaces any pre-existing opam path" but it rather "replaces any previously-modified PATH component by opam".

As noted by @rjbou during our meeting, our testsuite is missing test for this option and it would be nice to have some before the full release (this can be done separately from this PR at a later date).

Once merged, we should remember to display a prominent note so that people know about this change. In particular, if their .bashrc/.zshrc file contain further additions to PATH after the source opam-init.sh line, they will now be place before opam in the PATH. This can be a breaking change for some people but this is also a "bug fix" in some regard given that it normalises the shape of the PATH in interactive shell mode vs. not interactive mode if someone adds things to their PATH after sourcing opam.

Prior to this change, the default behaviour of `opam init` was to
register a shell hook which ran before each command, and added/moved the
bin dir from the current opam switch to the beginning of the PATH
variable. If a user has a custom bin directory at the beginning of their
PATH containing an executable with the same name as an executable in
their current opam switch, the executable in the opam switch will take
precedence.

This commit changes the behaviour of the shell hook to update the
existing opam switch in PATH in place rather than prepending it, so the
precedence of the opam switch relative to other dirs in PATH is
preserved.

This change doesn't affect the behaviour of running `eval $(opam env)`.

Fixes ocaml#6815

Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
@rjbou
rjbou force-pushed the env-hook-inplace-path branch from b896271 to 1488dc7 Compare July 13, 2026 15:54
@kit-ty-kate

Copy link
Copy Markdown
Member

Ignoring the cygwin failure (#7030).

Thanks a lot!

@kit-ty-kate
kit-ty-kate merged commit a1a4774 into ocaml:master Jul 13, 2026
40 of 41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

opam env_hook is too agressive, and makes the default install configuration of opam behave invasively in the shell environment

3 participants