Skip to content

Update PATH in place in env hook#6859

Open
gridbugs wants to merge 1 commit into
ocaml:masterfrom
gridbugs:env-hook-inplace-path
Open

Update PATH in place in env hook#6859
gridbugs wants to merge 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 from 98024e0 to 6872608 Compare February 13, 2026 05:16
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>
@gridbugs gridbugs force-pushed the env-hook-inplace-path branch from 6872608 to b896271 Compare February 16, 2026 10:47
@rjbou
Copy link
Copy Markdown
Collaborator

rjbou commented Feb 18, 2026

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
Copy link
Copy Markdown
Member

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

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.

@kit-ty-kate kit-ty-kate added this to the 2.6.0~alpha1 milestone Mar 9, 2026
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