diff --git a/src/ansi_color.ml b/src/ansi_color.ml index 9c928a0a..65a3fb85 100644 --- a/src/ansi_color.ml +++ b/src/ansi_color.ml @@ -150,14 +150,18 @@ let strip_colors_for_stderr s = else strip s -(* We redirect the output of all commands, so by default the compiler will disable - colors. Since we support colors in the output of commands, we force it via OCAMLPARAM - if stderr supports colors. *) -let setup_env_for_opam_colors = lazy( +(* We redirect the output of all commands, so by default the various tools will disable + colors. Since we support colors in the output of commands, we force it via specific + environment variables if stderr supports colors. *) +let setup_env_for_colors = lazy( if Lazy.force stderr_supports_colors then begin - match Sys.getenv "OPAMCOLOR" with - | exception Not_found -> Unix.putenv "OPAMCOLOR" "always" - | _ -> () + let set var value = + match Sys.getenv with + | exception Not_found -> Unix.putenv var value + | _ -> () + in + set "OPAMCOLOR" "always"; + set "OCAML_COLOR" "always"; end ) diff --git a/src/ansi_color.mli b/src/ansi_color.mli index 8a382134..59484e9a 100644 --- a/src/ansi_color.mli +++ b/src/ansi_color.mli @@ -2,7 +2,7 @@ val colorize : key:string -> string -> string val strip : string -> string val stderr_supports_colors : bool Lazy.t -val setup_env_for_opam_colors : unit Lazy.t +val setup_env_for_colors : unit Lazy.t (** Strip colors in [not (Lazy.force stderr_supports_colors)] *) val strip_colors_for_stderr : string -> string diff --git a/src/context.ml b/src/context.ml index e7030caa..d51f54de 100644 --- a/src/context.ml +++ b/src/context.ml @@ -264,11 +264,13 @@ let create ~(kind : Kind.t) ~path ~base_env ~env_extra ~name ~merlin ~use_findli let natdynlink_supported = Path.exists (Path.relative stdlib_dir "dynlink.cmxa") in let version = get "version" in let env,env_extra = - (* We redirect the output of all commands, so by default the compiler will disable - colors. Since we support colors in the output of commands, we force it via OCAMLPARAM - if stderr supports colors. *) - let ocaml_version = Scanf.sscanf version "%u.%u.%u" (fun a b c -> a, b, c) in - if Lazy.force Ansi_color.stderr_supports_colors && ocaml_version > (4, 02, 3) then + (* See comment in ansi_color.ml for setup_env_for_colors. For OCaml < 4.05, + OCAML_COLOR is not supported so we use OCAMLPARAM. OCaml 4.02 doesn't support + 'color' in OCAMLPARAM, so we just don't force colors with 4.02. *) + let ocaml_version = Scanf.sscanf version "%u.%u" (fun a b -> a, b) in + if Lazy.force Ansi_color.stderr_supports_colors + && ocaml_version > (4, 02) + && ocaml_version < (4, 05) then let value = match get_env env "OCAMLPARAM" with | None -> "color=always,_" @@ -369,7 +371,7 @@ let create ~(kind : Kind.t) ~path ~base_env ~env_extra ~name ~merlin ~use_findli let opam_config_var t var = opam_config_var ~env:t.env ~cache:t.opam_var_cache var let initial_env = lazy ( - Lazy.force Ansi_color.setup_env_for_opam_colors; + Lazy.force Ansi_color.setup_env_for_colors; Unix.environment ()) let default ?(merlin=true) ?(use_findlib=true) () = diff --git a/src/future.ml b/src/future.ml index 2e921fb3..cb2cc75e 100644 --- a/src/future.ml +++ b/src/future.ml @@ -620,7 +620,7 @@ module Scheduler = struct go_rec cwd log t let go ?(log=Log.no_log) t = - Lazy.force Ansi_color.setup_env_for_opam_colors; + Lazy.force Ansi_color.setup_env_for_colors; Log.info log ("Workspace root: " ^ !Clflags.workspace_root); let cwd = Sys.getcwd () in go_rec cwd log t