Skip to content

Commit

Permalink
[bxl] delete all infer env vars in buck subprocess
Browse files Browse the repository at this point in the history
Summary: Buck2 sometimes executes actions locally. This means that Infer-set environment variables affect local actions, but not remote ones. In the case of the BXL integration this actually makes Infer capture fail when run locally. This diff explicitly unsets the Infer environment variables inside the buck process.

Reviewed By: martintrojer

Differential Revision: D50850658

fbshipit-source-id: 104efba0391789dda006cc1ee22d7a34b0774896
  • Loading branch information
ngorogiannis authored and facebook-github-bot committed Nov 1, 2023
1 parent 8a54760 commit 8aa2f14
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
6 changes: 4 additions & 2 deletions infer/src/base/CommandLineOption.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ let manpage_s_notes = "NOTES"

let is_env_var_set v = Option.value (Option.map (Sys.getenv v) ~f:(String.equal "1")) ~default:false

let infer_cwd_env_var = "INFER_CWD"

(** The working directory of the initial invocation of infer, to which paths passed as command line
options are relative. *)
let init_work_dir, is_originator =
match Sys.getenv "INFER_CWD" with
match Sys.getenv infer_cwd_env_var with
| Some dir ->
(dir, false)
| None ->
let real_cwd = Utils.realpath (Sys.getcwd ()) in
Unix.putenv ~key:"INFER_CWD" ~data:real_cwd ;
Unix.putenv ~key:infer_cwd_env_var ~data:real_cwd ;
(real_cwd, true)


Expand Down
3 changes: 3 additions & 0 deletions infer/src/base/CommandLineOption.mli
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ val mk_subcommand :
(otherwise the command is considered internal). [on_unknown_arg] is the action taken on unknown
anonymous arguments; it is `Reject by default. *)

val infer_cwd_env_var : string
(** environment variable pointing to the originator's process results directory *)

val args_env_var : string
(** environment variable use to pass arguments from parent to child processes *)

Expand Down
19 changes: 15 additions & 4 deletions infer/src/integration/Buck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ let store_args_in_file ~identifier args =
else args


let infer_vars_to_kill =
CommandLineOption.[infer_cwd_env_var; args_env_var; strict_mode_env_var; inferconfig_path_arg]


(** Wrap a call to buck while (i) logging standard error to our standard error in real time; (ii)
redirecting standard out to a file, the contents of which are returned; (iii) protect the child
process from [SIGQUIT].
Expand All @@ -44,7 +48,7 @@ let store_args_in_file ~identifier args =
To achieve this we need to do two things: (i) tell the JVM not to use signals, meaning it leaves
the default handler for [SIGQUIT] in place; (ii) uninstall the default handler for [SIGQUIT]
because now that the JVM doesn't touch it, it will lead to process death. *)
let wrap_buck_call ?(extend_env = []) version ~label cmd =
let wrap_buck_call ?(extend_env = []) ?(kill_infer_env_vars = false) version ~label cmd =
let is_buck2 = match (version : version) with V1 -> false | V2 -> true in
let stdout_file =
let prefix = Printf.sprintf "%s_%s" (if is_buck2 then "buck2" else "buck") label in
Expand All @@ -65,8 +69,8 @@ let wrap_buck_call ?(extend_env = []) version ~label cmd =
(* Uninstall the default handler for [SIGQUIT]. *)
Printf.sprintf "trap '' SIGQUIT ; %s" cmd_with_output
in
let env =
if is_buck2 then `Extend []
let env_vars =
if is_buck2 then []
else
let explicit_buck_java_heap_size =
Option.map Config.buck_java_heap_size_gb ~f:(fun size -> Printf.sprintf "-Xmx%dG" size)
Expand All @@ -82,7 +86,14 @@ let wrap_buck_call ?(extend_env = []) version ~label cmd =
in
L.environment_info "Buck: setting %s to '%s'@\n" buck_extra_java_args_env_var
new_buck_extra_java_args ;
`Extend ((buck_extra_java_args_env_var, new_buck_extra_java_args) :: extend_env)
(buck_extra_java_args_env_var, new_buck_extra_java_args) :: extend_env
in
let env =
if kill_infer_env_vars then
`Override
( List.map infer_vars_to_kill ~f:(fun var -> (var, None))
@ List.map env_vars ~f:(fun (lhs, rhs) -> (lhs, Some rhs)) )
else `Extend env_vars
in
L.debug Capture Quiet "Running buck command '%s'@." command ;
let Unix.Process_info.{stdin; stdout; stderr; pid} =
Expand Down
10 changes: 8 additions & 2 deletions infer/src/integration/Buck.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ module Target : sig
end

val wrap_buck_call :
?extend_env:(string * string) list -> version -> label:string -> string list -> string list
?extend_env:(string * string) list
-> ?kill_infer_env_vars:bool
-> version
-> label:string
-> string list
-> string list
(** Wrap a call to buck while (i) logging standard error to our standard error in real time; (ii)
redirecting standard out to a file, the contents of which are returned; (iii) protect the child
process from [SIGQUIT].
Expand All @@ -32,7 +37,8 @@ val wrap_buck_call :
[(variable, value)] that will extend the environment of the subprocess; [label] is appended to
[buck_] to make the prefix of the temporary file storing the standard output of the command, for
quick identification; [cmd] is a list of strings making up the shell command to execute; the
return value is the standard output of the command split on newlines. *)
return value is the standard output of the command split on newlines. If [kill_infer_env_vars]
is true then all Infer environment variables will be unset in the child process.Absint *)

val config : BuckMode.t -> version -> string list
(** return list of string parameters of the form
Expand Down
3 changes: 2 additions & 1 deletion infer/src/integration/BxlCapture.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ let run_capture buck2_build_cmd =
L.debug Capture Quiet "Processed buck2 bxl command '%a'@\n" (Pp.seq F.pp_print_string)
buck2_build_cmd ;
let infer_deps_lines =
Buck.wrap_buck_call ~extend_env:[] V2 ~label:"build" ("buck2" :: buck2_build_cmd)
Buck.wrap_buck_call ~extend_env:[] ~kill_infer_env_vars:true V2 ~label:"build"
("buck2" :: buck2_build_cmd)
|> List.fold ~init:[] ~f:(traverse ~root:Config.buck2_root (Visited.create 11))
|> List.dedup_and_sort ~compare:String.compare
in
Expand Down

0 comments on commit 8aa2f14

Please sign in to comment.