diff --git a/doc/usage.rst b/doc/usage.rst index deb0fa4cf4a0..b5ee0d0270b7 100644 --- a/doc/usage.rst +++ b/doc/usage.rst @@ -501,13 +501,15 @@ context or can be the description of an opam switch, as follows: are meant to be executed on the host machine, such as preprocessors. - ``(paths ( ) .. ( ))`` allows to set the value of any - ``PATH``-like variables in this context. If ``PATH`` itself is modified in - this way, its value will be used to resolve binaries in the workspace, - including finding the compiler and related tools. These variables will also be - passed as part of the environment to any program launched by ``dune``. For - each variable, the value is specified using the :ref:`ordered-set-language`. - Relative paths are interpreted with respect to the workspace root, see - :ref:`finding-root`. + ``PATH``-like variables in this context. These variables are passed as part of + the environment to any program launched by ``dune``. For each variable, the + value is specified using the :ref:`ordered-set-language`. Relative paths are + interpreted with respect to the workspace root, see :ref:`finding-root`. + + +If environment variables, such as ``PATH``, are modified in the `dune-workspace` +through `env-vars` or `paths`, its value will be used to resolve binaries in the +workspace, including finding the compiler and related tools. Both ``(default ...)`` and ``(opam ...)`` accept a ``targets`` field in order to setup cross compilation. See :ref:`advanced-cross-compilation` for more diff --git a/src/context.ml b/src/context.ml index 2a915fbfa0d3..f6ee5a4f7360 100644 --- a/src/context.ml +++ b/src/context.ml @@ -29,8 +29,8 @@ module Env_nodes = struct let extra_env ~profile env_nodes = Env.extend_env - (Dune_env.Stanza.find env_nodes.context ~profile).env_vars (Dune_env.Stanza.find env_nodes.workspace ~profile).env_vars + (Dune_env.Stanza.find env_nodes.context ~profile).env_vars end type t = @@ -407,7 +407,6 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets Option.value ~default:Env.empty (Option.map findlib_config ~f:Findlib.Config.env) ) - |> Env.extend_env (Env_nodes.extra_env ~profile env_nodes) in let stdlib_dir = Path.of_string (Ocaml_config.standard_library ocfg) in let natdynlink_supported = Ocaml_config.natdynlink_supported ocfg in @@ -646,9 +645,15 @@ let instantiate_context env (workspace : Workspace.t) ; workspace = workspace.env } in + let env = + Env.extend_env env + (Env_nodes.extra_env + ~profile:(Workspace.Context.profile context) env_nodes) + in + let env = extend_paths ~env (Workspace.Context.paths context) in match context with | Default { targets; name; host_context = _; profile; env = _ - ; toolchain ; paths; loc = _ } -> + ; toolchain ; paths = _; loc = _ } -> let merlin = workspace.merlin_context = Some (Workspace.Context.name context) in @@ -657,13 +662,11 @@ let instantiate_context env (workspace : Workspace.t) | Some _ -> toolchain | None -> Env.get env "OCAMLFIND_TOOLCHAIN" in - let env = extend_paths ~env paths in default ~env ~env_nodes ~profile ~targets ~name ~merlin ~host_context ~host_toolchain | Opam { base = { targets; name; host_context = _; profile; env = _ - ; toolchain; paths; loc = _ } + ; toolchain; paths = _; loc = _ } ; switch; root; merlin } -> - let env = extend_paths ~env paths in create_for_opam ~root ~env_nodes ~env ~profile ~switch ~name ~merlin ~targets ~host_context ~host_toolchain:toolchain diff --git a/src/stdune/env.mli b/src/stdune/env.mli index fb5eb9585c08..e0560f162f3e 100644 --- a/src/stdune/env.mli +++ b/src/stdune/env.mli @@ -23,6 +23,7 @@ val get : t -> Var.t -> string option val extend : t -> vars:string Map.t -> t val extend_env : t -> t -> t +(** [extend_env x y] gives priority to the values of y *) val add : t -> var:Var.t -> value:string -> t diff --git a/src/workspace.ml b/src/workspace.ml index 9886928f68e6..0e18fa815cd1 100644 --- a/src/workspace.ml +++ b/src/workspace.ml @@ -168,6 +168,14 @@ module Context = struct | Default x -> x.targets | Opam x -> x.base.targets + let profile = function + | Default x -> x.profile + | Opam x -> x.base.profile + + let paths = function + | Default x -> x.paths + | Opam x -> x.base.paths + let all_names t = let n = name t in n :: List.filter_map (targets t) ~f:(function diff --git a/src/workspace.mli b/src/workspace.mli index 430df6f4c00c..79916e955db0 100644 --- a/src/workspace.mli +++ b/src/workspace.mli @@ -42,6 +42,9 @@ module Context : sig val env : t -> Dune_env.Stanza.t + val profile : t -> string + val paths : t -> (string * Ordered_set_lang.t) list + val host_context : t -> string option end diff --git a/test/blackbox-tests/test-cases/env-variables/run.t b/test/blackbox-tests/test-cases/env-variables/run.t index 5dcc1b7b3bf6..49ad39049484 100644 --- a/test/blackbox-tests/test-cases/env-variables/run.t +++ b/test/blackbox-tests/test-cases/env-variables/run.t @@ -20,7 +20,7 @@ used. $ dune exec --root precedence ./printenv.exe VARIABLE_FROM_BOTH Entering directory 'precedence' Entering directory 'precedence' - VARIABLE_FROM_BOTH=from_workspace + VARIABLE_FROM_BOTH=from_context When a variable is repeated, an error message is displayed: