Skip to content

opam init advises misconfiguring shell startup scripts #4201

@Blaisorblade

Description

@Blaisorblade

opam outputs

[NOTE] Make sure that ~/.profile is well sourced in your ~/.bashrc.

But that's backwards. As also mentioned in this old opam issue, a typical setup is that:

  • non-login shells loads ~/.bashrc
  • login shells load ~/.profile — and usually that sources .bashrc (the opposite of what opam advises). For instance, on Debian
# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

The rationale is that environment variables are set by login shells loading ~/.profile and inherited by other shells — while other settings that are not inherited belong in ~/.bashrc, which ends up loaded by all shells.

The recommended setup would load ~/.profile multiple times, which is unexpected for lots of initialization code.

This bug is described in #3304 — the message comes from https://github.com/ocaml/opam/pull/3304/files#diff-234457b80298a00240e5fcd7591e5432R603.

Here's my proposed fix:

  • the message should suggest
[NOTE] Make sure that ~/.bashrc is well sourced in your ~/.profile.
  • the script should load ~/.opam/opam-init/variables.sh in ~/.profile
  • the script should load ~/.opam/opam-init/env_hook.sh in ~/.bashrc

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions