Fix getExecutablePath to return absolute path
Needs RevisionPublic

Authored by oherrala on Jul 22 2016, 10:45 AM.

Details

Reviewers
erikd
goldfire
hvr
bgamari
austin
Trac Issues
#12377
Summary

On non-Windows, non-Linux and non-OSX (The big three) operating
systems getExecutablePath uses argv[0] to find executable
path. However, this might not be absolute path as promised by
documentation.

This patch moves POSIX realpath(3) to be shared across non-Windows
systems and then uses it for making path found in argv[0] to be
canonicalized and absolute path.

Fixes Trac #12377

Diff Detail

Repository
rGHC Glasgow Haskell Compiler
Lint
Lint Skipped
Unit
Unit Tests Skipped
oherrala retitled this revision from to Fix getExecutablePath to return absolute path.Jul 22 2016, 10:45 AM
oherrala updated this object.
oherrala edited the test plan for this revision. (Show Details)
oherrala set the repository for this revision to rGHC Glasgow Haskell Compiler.
oherrala updated the Trac tickets for this revision.

This change seems dubious to me; the program might have changed directories since being started and then realpath will produce a nonsense error. Moreover if the programmer needs an absolute path, they can just call canonicalizePath themselves. There is the same caveat of course, but now the programmer is aware of it and can make sure to call canonicalizePath from the original working directory.

Hm, I see the OS X implementation has the same issue...

It would be better I think if there was an OS-specific way to get the correct absolute path to the executable; is that possible on OpenBSD?

For that matter, what does getExecutablePath return on OpenBSD currently when you invoke a program from the shell via the $PATH using the bare command name? Won't it just be the filename of the executable, like cabal? Then realpath will not help.

Hm, I see the OS X implementation has the same issue...

Actually the OS X one looks mostly okay, because _NSGetExecutablePath apparently returns an absolute path already.

For that matter, what does getExecutablePath return on OpenBSD currently when you invoke a program from the shell via the $PATH using the bare command name? Won't it just be the filename of the executable, like cabal? Then realpath will not help.

Ah, good catch. You are right.

hvr requested changes to this revision.Aug 10 2016, 2:36 PM

I just stumbled over a bug that was introduced in cabal due to https://github.com/haskell/cabal/pull/3531

This revision now requires changes to proceed.Aug 10 2016, 2:36 PM
austin resigned from this revision.Nov 9 2017, 9:44 AM