Permalink
Browse files

Fix #32

Now the environment variables can't have more than one ~/.opam/... path
  • Loading branch information...
1 parent 3a0af3c commit 8a6502336d58fe852b2baa7b9a4288bdf5c98f2c @samoht samoht committed Jul 5, 2012
Showing with 14 additions and 7 deletions.
  1. +3 −2 src/client.ml
  2. +5 −5 src/run.ml
  3. +6 −0 src/utils.ml
View
@@ -701,10 +701,11 @@ type env = {
let expand_env t env =
List.map (fun (ident, symbol, string) ->
let string = substitute_string t string in
+ let clean_env () = try Utils.reset_env_value (Sys.getenv ident) with _ -> [] in
match symbol with
| "=" -> (ident, string)
- | "+=" -> (ident, try string ^ ":" ^ Sys.getenv ident with _ -> string)
- | "=+" -> (ident, try Sys.getenv ident ^ ":" ^ string with _ -> string)
+ | "+=" -> (ident, String.concat ":" (string :: clean_env ()))
+ | "=+" -> (ident, String.concat ":" (clean_env () @ [string]))
| _ -> failwith (Printf.sprintf "expand_env: %s is an unknown symbol" symbol)
) env
View
@@ -180,17 +180,17 @@ let real_path p =
else
dir / base
-let add_path bins =
+let replace_path bins =
let path = ref "<not set>" in
let env = Unix.environment () in
for i = 0 to Array.length env - 1 do
let k,v = match Utils.cut_at env.(i) '=' with
| Some (k,v) -> k,v
| None -> assert false in
if k = "PATH" then
- let new_path = match List.filter Sys.file_exists bins with
- | [] -> v
- | l -> String.concat ":" l ^ ":" ^ v in
+ let v = Utils.reset_env_value v in
+ let bins = List.filter Sys.file_exists bins in
+ let new_path = String.concat ":" (bins @ v) in
env.(i) <- "PATH=" ^ new_path;
path := new_path;
done;
@@ -201,7 +201,7 @@ type command = string list
let run_process ?(add_to_env=[]) ?(add_to_path=[]) = function
| [] -> invalid_arg "run_process"
| cmd :: args ->
- let env, path = add_path add_to_path in
+ let env, path = replace_path add_to_path in
let add_to_env = List.map (fun (k,v) -> k^"="^v) add_to_env in
let env = Array.concat [ env; Array.of_list add_to_env ] in
let name = log_file () in
View
@@ -77,4 +77,10 @@ let cut_at = cut_at_aux String.index
let rcut_at = cut_at_aux String.rindex
+let split s c =
+ Pcre.split (Pcre.regexp (String.make 1 c)) s
+(* Remove from a ':' separated list of string the one which are prefix of Globals.root_path *)
+let reset_env_value v =
+ let v = split v ':' in
+ List.filter (fun v -> not (starts_with ~prefix:!Globals.root_path v)) v

0 comments on commit 8a65023

Please sign in to comment.