Skip to content

Commit

Permalink
Harden windows_get_shell
Browse files Browse the repository at this point in the history
In Windows native containers, the Filename.chop_suffix call in
windows_get_shell was occasionally failing. OpamStubs.getProcessAncestry
can return empty strings. windows_get_shell now explictly handles the
empty string case and .exe is now _optionally_ stripped.
  • Loading branch information
dra27 committed Nov 2, 2023
1 parent c4961ba commit c13c346
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ users)
## Internal

## Internal: Windows
* Fix sporadic crash in shell detection (seen in native containers) [#5714 @dra27]

## Test

Expand Down
6 changes: 5 additions & 1 deletion src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,9 @@ module OpamSys = struct
else
fun x -> x

let chop_exe_suffix name =
Option.default name (Filename.chop_suffix_opt name ~suffix:".exe")

let windows_process_ancestry = Lazy.from_fun OpamStubs.getProcessAncestry

type shell_choice = Accept of shell
Expand All @@ -1085,10 +1088,11 @@ module OpamSys = struct
| "pwsh.exe" -> Some (Accept (SH_pwsh Powershell_pwsh))
| "cmd.exe" -> Some (Accept SH_cmd)
| "env.exe" -> Some (Accept SH_sh)
| "" -> None
| name ->
Option.map
(fun shell -> Accept shell)
(shell_of_string (Filename.chop_suffix name ".exe"))
(shell_of_string (chop_exe_suffix name))
in
lazy (
let lazy ancestors = windows_process_ancestry in
Expand Down
3 changes: 3 additions & 0 deletions src/core/opamStd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,9 @@ module Sys : sig
(** Append .exe (only if missing) to executable filenames on Windows *)
val executable_name : string -> string

(** Remove .exe (if present) from an executable filename on Windows *)
val chop_exe_suffix : string -> string

(** The different families of shells we know about *)
type powershell_host = Powershell_pwsh | Powershell
type shell = SH_sh | SH_bash | SH_zsh | SH_csh | SH_fish
Expand Down

0 comments on commit c13c346

Please sign in to comment.