Skip to content

Commit

Permalink
Merge pull request #4861 from dra27/env-fix
Browse files Browse the repository at this point in the history
Fix reverting additions to PATH-like variables
  • Loading branch information
rjbou authored Oct 15, 2021
2 parents b941f86 + 36c9ce3 commit f9c2bbf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ users)
## State
* Handle empty environment variable updates - missed cherry-pick from 2.0 [#4840 @dra27]
* Repository state: stop scanning directory once opam file is found [#4847 @rgrinberg]
* Fix reverting environment additions to PATH-like variables when several dirs added at once [#4861 @dra27]

## Opam file format
*
Expand Down
30 changes: 23 additions & 7 deletions src/state/opamEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,30 @@ let join_var l =
(* To allow in-place updates, we store intermediate values of path-like as a
pair of list [(rl1, l2)] such that the value is [List.rev_append rl1 l2] and
the place where the new value should be inserted is in front of [l2] *)
let unzip_to elt =
let rec aux acc = function
| [] -> None
| x::r ->
if x = elt then Some (acc, r)
else aux (x::acc) r


let unzip_to elt current =
(* If [r = l @ rs] then [remove_prefix l r] is [Some rs], otherwise [None] *)
let rec remove_prefix l r =
match l, r with
| (l::ls, r::rs) when l = r ->
remove_prefix ls rs
| ([], rs) -> Some rs
| _ -> None
in
aux []
match split_var elt with
| [] -> invalid_arg "OpamEnv.unzip_to"
| hd::tl ->
let rec aux acc = function
| [] -> None
| x::r ->
if x = hd then
match remove_prefix tl r with
| Some r -> Some (acc, r)
| None -> aux (x::acc) r
else aux (x::acc) r
in
aux [] current

let rezip ?insert (l1, l2) =
List.rev_append l1 (match insert with None -> l2 | Some i -> i::l2)
Expand Down
17 changes: 17 additions & 0 deletions tests/reftests/dune.inc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@
%{targets}
(run ./run.exe %{bin:opam} %{dep:dot-install.test} %{read-lines:testing-env}))))

(rule
(alias reftest-env)
(action
(diff env.test env.out)))

(alias
(name reftest)
(deps (alias reftest-env)))

(rule
(targets env.out)
(deps root-N0REP0)
(action
(with-stdout-to
%{targets}
(run ./run.exe %{bin:opam} %{dep:env.test} %{read-lines:testing-env}))))

(rule
(alias reftest-init)
(action
Expand Down
27 changes: 27 additions & 0 deletions tests/reftests/env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
N0REP0
### <pkg:nv.1>
opam-version: "2.0"
setenv: [ NV_VARS += "%{_:doc}%:%{_:share}%" ]
flags: compiler
### opam update

<><> Updating package repositories ><><><><><><><><><><><><><><><><><><><><><><>
[default] synchronised from file://${BASEDIR}/REPO
Now run 'opam upgrade' to apply any package updates.
### opam switch create setenv nv

<><> Installing new switch packages <><><><><><><><><><><><><><><><><><><><><><>
Switch invariant: ["nv"]

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-> installed nv.1
Done.
### opam env | grep "NV_VARS" | '[:;]' -> '-' | '[:;]' -> '-' | '[:;]' -> '-'
NV_VARS='${BASEDIR}/OPAM/setenv/doc/nv-${OPAMTMP}/OPAM/setenv/share/nv'- export NV_VARS-
### opam exec -- opam env --revert | grep "NV_VARS" | '[:;]' -> '-' | '[:;]' -> '-' | '[:;]' -> '-'
NV_VARS=''- export NV_VARS-
### NV_VARS=/another/path
### opam env | grep "NV_VARS" | '[:;]' -> '-' | '[:;]' -> '-' | '[:;]' -> '-'
NV_VARS='${BASEDIR}/OPAM/setenv/doc/nv-${OPAMTMP}/OPAM/setenv/share/nv-/another/path'- export NV_VARS-
### opam exec -- opam env --revert | grep "NV_VARS" | '[:;]' -> '-'
NV_VARS='/another/path'- export NV_VARS-

0 comments on commit f9c2bbf

Please sign in to comment.