Skip to content

Set environment variables needed for bash, zsh completion in opam env - #6812

Open
WardBrian wants to merge 2 commits into
ocaml:masterfrom
WardBrian:feat/6427-completion-environment-variables
Open

Set environment variables needed for bash, zsh completion in opam env#6812
WardBrian wants to merge 2 commits into
ocaml:masterfrom
WardBrian:feat/6427-completion-environment-variables

Conversation

@WardBrian

@WardBrian WardBrian commented Nov 24, 2025

Copy link
Copy Markdown
Contributor

Would close #6427

This updates opam env to set XDG_DATA_DIRS (from the XDG base directory specification) to include the current switch's share root, and FPATH to include %{share}%/zsh/site-functions when the shell is zsh

The former works with bash-completion, which looks in the XDG_DATA_DIRS entries for bash-completion/completions sub-directories, while the later sets up zsh's built-in completion mechanisms to look in this location.

Questions for reviewers --

  1. I currently only modify FPATH on zsh, but I think it would be safe to do it generically (the only other shell that seems to assign meaning to the FPATH variable is ksh, which opam doesn't support)
  2. Assuming we want to keep FPATH zsh-specific, is it worth threading through a shell variable all the way from the CLI to where we compute these updates, or is it fine to keep the guess_shell_compat call?

@WardBrian WardBrian changed the title Feat/6427 completion environment variables Set environment variables needed for bash, zsh completion in opam env Nov 24, 2025
@WardBrian
WardBrian force-pushed the feat/6427-completion-environment-variables branch from da44a84 to 4773962 Compare November 24, 2025 17:56
@kit-ty-kate kit-ty-kate added this to the 2.6.0~alpha1 milestone Nov 24, 2025
@WardBrian
WardBrian force-pushed the feat/6427-completion-environment-variables branch 2 times, most recently from 18a99a3 to 95d0887 Compare November 24, 2025 22:27
@WardBrian
WardBrian marked this pull request as ready for review November 30, 2025 21:02
@kit-ty-kate

Copy link
Copy Markdown
Member

cc @hannesm @reynir do you have any thoughts about the security considerations of this feature by any chance?

@hannesm

hannesm commented Dec 2, 2025

Copy link
Copy Markdown
Member

@kit-ty-kate thanks for asking, no I don't have an idea about this environment variable (and whether to prepend or append to it).

@reynir

reynir commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

I don't know much about it. For bash-completion I suspect it could add completion scripts from installed opam packages, and I think those can run arbitrary commands. Then again I guess packages can already install arbitrary binaries shadowing common binaries such as ls. I'm not sure what the security model is or how that fits into that.

@WardBrian

Copy link
Copy Markdown
Contributor Author

Even if you're sandboxing the build commands (so you can't be pwned by arbitrary badness hidden behind a make command), I still think it is currently the case that you can't install an opam package unless you trust it. It doesn't even have to shadow something like ls; presumably you're going to run the code from the package eventually, or else you wouldn't be installing it.

@dbuenzli

dbuenzli commented Dec 4, 2025

Copy link
Copy Markdown
Contributor

Then again I guess packages can already install arbitrary binaries shadowing common binaries such as ls

Not to mention that arbitrary ocaml packages can actually set or extend theses variables themselves…

@WardBrian

Copy link
Copy Markdown
Contributor Author

Not to mention that arbitrary ocaml packages can actually set or extend theses variables themselves…

Yes. If this is decided out of scope for the opam tool, possibly the next best option would be a conf-completion package in the opam repository which contains just these two variables as setenv fields. Then tools could depend on that to get their users environments correctly configured for autocomplete.

That feels like a bit of a hack compared to doing it in code here.

@WardBrian
WardBrian force-pushed the feat/6427-completion-environment-variables branch 2 times, most recently from 5e23463 to 6d4bea3 Compare July 24, 2026 16:59
@WardBrian

Copy link
Copy Markdown
Contributor Author

The failing windows reftests are fairly inscrutable to me, and I'm extra confused as to why only some configurations are failing in that matrix

@kit-ty-kate

Copy link
Copy Markdown
Member

The failing windows reftests are fairly inscrutable to me, and I'm extra confused as to why only some configurations are failing in that matrix

all Native configurations of windows fail. The only "windows" configurations that work fine are the ones using the cygwin backend of ocaml, which mains that it simply works like unix does.

The error is:

diff --git a/_build/default/tests/reftests/env-idempotent.test b/_build/default/tests/reftests/env-idempotent.out
index cb5a13d..5d23c8f 100644
--- a/_build/default/tests/reftests/env-idempotent.test
+++ b/_build/default/tests/reftests/env-idempotent.out
@@ -34,4 +34,3 @@ User configuration:
 ### rm -rf root/fake2
 ### bash bash-lc.sh "'$OPAMBIN' exec --root root --switch=fake -- env" | grep -v MANPATH | grep /root/fake2/ | '=.*' -> ''
 PATH
-XDG_DATA_DIRS
File "tests/reftests/env-idempotent.test", line 1, characters 0-0:
"C:\Program Files\Git\cmd\git.exe" --no-pager diff --no-index --color=always -u --ignore-cr-at-eol _build/default/tests/reftests/env-idempotent.test _build/default/tests/reftests/env-idempotent.out
Command exited with code 1.
diff --git a/_build/default/tests/reftests/env.win32.test b/_build/default/tests/reftests/env.win32.out
index 7dee3d0..511d6ed 100644
--- a/_build/default/tests/reftests/env.win32.test
+++ b/_build/default/tests/reftests/env.win32.out
@@ -811,7 +811,17 @@ Processing  2/3: [all-formulae: sh env | grep RAF_ENV | sort]
 [ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ("host" true | "target-quoted" true). Using default ';' 'target'.
 [ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ":" true | ";" true. Using default ';' 'target'.
 [ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ("host" true | "target-quoted" true). Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ":" true | ";" true. Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ("host" true | "target-quoted" true). Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ":" true | ";" true. Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ("host" true | "target-quoted" true). Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ":" true | ";" true. Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ("host" true | "target-quoted" true). Using default ';' 'target'.
 Done.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ":" true | ";" true. Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ("host" true | "target-quoted" true). Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ":" true | ";" true. Using default ';' 'target'.
+[ERROR] Formula can't be completely resolved : RAF_ENVSET_UNRES ("host" true | "target-quoted" true). Using default ';' 'target'.
 ### opam env | sort | grep "RAF_ENV"
 RAF_ENVSET_ATOM='\is\atom'; export RAF_ENVSET_ATOM;
 RAF_ENVSET_DBL='sec/ond;fir/st'; export RAF_ENVSET_DBL;

The change in env.win32.test is straightforward since you've done the same change in env.unix.test. Since you've only ran the testsuite on Unix, only the tests that work on Unix were ran and the ones that only run on Windows were skipped. So to fix that you can either run the testsuite on Windows again or simply trust the CI and apply that patch.

Now for env-idempotent.test, i'm not exactly sure, it depends what that test was supposed to show, but it could probably be fixed by adding another | grep -v XDG_DATA_DIRS to the command

@WardBrian

Copy link
Copy Markdown
Contributor Author

Now for env-idempotent.test, i'm not exactly sure, it depends what that test was supposed to show

I am not entirely sure myself, I tried digging through the git history but also came up empty handed. Since it's already filtering out manpath, an extra filter doesn't seem so bad

@WardBrian
WardBrian force-pushed the feat/6427-completion-environment-variables branch from 6d4bea3 to 61d7146 Compare July 27, 2026 18:46
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.

Should $(opam env) or .opam/opam-init/* setup paths for shell completion scripts ?

5 participants