Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quote all the paths to OPAMROOT when creating the init scripts on Unix #5841

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -100,6 +101,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