Skip to content

Commit

Permalink
Merge pull request #5841 from kit-ty-kate/env-quote-path-unix
Browse files Browse the repository at this point in the history
Quote all the paths to OPAMROOT when creating the init scripts on Unix
  • Loading branch information
kit-ty-kate authored Feb 19, 2024
2 parents adb6911 + 3b8e0ce commit 3179157
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ users)
* Fix shell detection on Windows when opam is called via Cygwin's /usr/bin/env even though cmd/powershell is used [#5797 @kit-ty-kate]
* Fix incorrect deduplication of environment variables on update. Effect was that FOO += "" would occlude the value of FOO in the environment [#5837 @dra27]
* Fix regression from #5356 on the detection of out-of-date environment variables. As part of a refactoring, a filter predicate got inverted [#5837 @dra27]
* Unixify Windows paths in init shells scripts (sh, bash, zsh, fish & tsh) [#5797 @rjbou]

## Opamfile

Expand Down Expand Up @@ -101,6 +102,7 @@ users)
## Client

## Shell
* Quote all the paths to OPAMROOT when creating the init scripts on Unix in case OPAMROOT contains spaces, backslashes or special characters [#5841 @kit-ty-kate - fix #5804]

## Internal

Expand Down
3 changes: 1 addition & 2 deletions src/core/opamSystem.mli
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ val get_cygpath_function: command:string -> (string -> string) lazy_t
val get_cygpath_path_transform: (pathlist:bool -> string -> string) lazy_t

(** [apply_cygpath path] applies the `cygpath` command to [name] using
`cygpath -- name`. `cygpath` is resolved with default environment
in the function. *)
`cygpath -- name`. *)
val apply_cygpath: string -> string

(** [command cmd] executes the command [cmd] in the correct OPAM
Expand Down
18 changes: 14 additions & 4 deletions src/state/opamEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -875,16 +875,26 @@ let env_hook_script shell =

let source root shell f =
let fname = OpamFilename.to_string (OpamPath.init root // f) in
let unix_transform ?using_backslashes () =
let cygpath = Lazy.force OpamSystem.get_cygpath_path_transform in
cygpath ~pathlist:false fname
|> OpamStd.Env.escape_single_quotes ?using_backslashes
in
match shell with
| SH_csh ->
Printf.sprintf "if ( -f %s ) source %s >& /dev/null\n" fname fname
let fname = unix_transform () in
Printf.sprintf "if ( -f '%s' ) source '%s' >& /dev/null\n"
fname fname
| SH_fish ->
Printf.sprintf "source %s > /dev/null 2> /dev/null; or true\n" fname
let fname = unix_transform ~using_backslashes:true () in
Printf.sprintf "source '%s' > /dev/null 2> /dev/null; or true\n" fname
| SH_sh | SH_bash ->
Printf.sprintf "test -r %s && . %s > /dev/null 2> /dev/null || true\n"
let fname = unix_transform () in
Printf.sprintf "test -r '%s' && . '%s' > /dev/null 2> /dev/null || true\n"
fname fname
| SH_zsh ->
Printf.sprintf "[[ ! -r %s ]] || source %s > /dev/null 2> /dev/null\n"
let fname = unix_transform () in
Printf.sprintf "[[ ! -r '%s' ]] || source '%s' > /dev/null 2> /dev/null\n"
fname fname
| SH_cmd ->
Printf.sprintf "if exist \"%s\" call \"%s\" >NUL 2>NUL\n" fname fname
Expand Down

0 comments on commit 3179157

Please sign in to comment.