Skip to content

Forwarding DYLD_LIBRARY_PATH #1161

Closed
Closed

Description

@bitemyapp

https://www.reddit.com/r/haskell/comments/3ooxu4/library_not_loaded_libmariadb2dylib_os_x/

Ah, I got the solution myself. It seems that I needed to do a

export DYLD_LIBRARY_PATH=/usr/local/lib/mariadb/

to add it to the search paths for dylds and it seems stack isn't forwarding env variables like DYLD_LIBRARY_PATH. I noticed because I tried to run the hsc2hs generated executable manually and it worked as expected.

Is this intended behaviour for stack?

Activity

  1. snoyberg commented on Oct 15, 2015

    @snoyberg
    Contributor

    Just saw this issue after responding on Reddit. I'm not aware of anything Stack is doing to make DYLD_LIBRARY_PATH be handled specially, perhaps it's the OS itself doing something funny?

  2. alexbiehl commented on Oct 15, 2015

    @alexbiehl
    Contributor
    Spawning children processes of processes restricted by System Integrity Protection, such as by
    launching a helper process in a bundle with NSTask or calling the exec(2) command, resets the Mach
    special ports of that child process. Any dynamic linker (dyld) environment variables, such as
    DYLD_LIBRARY_PATH, are purged when launching protected processes.
    

    Source: https://forums.developer.apple.com/thread/9233

  3. snoyberg commented on Oct 15, 2015

    @snoyberg
    Contributor

    Well, that explains that. If someone has an idea for how to resolve this issue, please send a PR, but my lack of OS X experience will prevent me from getting involved.

  4. added this to the P3: Optional milestone on Oct 15, 2015
  5. borsboom commented on Oct 16, 2015

    @borsboom
    Contributor

    Is this specific to Stack, or does the same thing happen with cabal-install? I just wonder if a workaround is even possible, because it would presumably require getting the subprocess to set DYLD_LIBRARY_PATH itself. But if cabal-install doesn't have this problem then it should be fixable in Stack. Could it be related to the fact that ghc is actually a shell script wrapper rather than a binary?

    I'm not a Mac developer, just a Un*x developer that happens run OS X on the desktop, so I don't have much understanding of System Integrity Protection at this point. I'm avoiding upgrading to El Capitan because I don't want to break my own development environment, but probably I should bite the bullet so I can look at these issues...

  6. DemiMarie commented on Oct 20, 2015

    @DemiMarie

    Sounds like the only solution is to avoid going through a shell script wrapper, or distributing a custom shell.

  7. alexbiehl commented on Oct 20, 2015

    @alexbiehl
    Contributor

    I can have a look at it when I'm back home. The idea is:

    I was able to compile with regular libmysqlclient (without setting DYLD_LIBRARY_PATH) maybe it was in PATH or some other variable so that hsc2hs were able to find it nonetheless. The same principle should apply to libmariadb. Maybe then I can find what differed there.

  8. borsboom commented on Oct 25, 2015

    @borsboom
    Contributor

    @alexbiehl Have you had a chance to look into this? I'm also still curious whether this is unique to Stack or whether cabal-install has the same problem.

  9. YPares commented on Nov 20, 2015

    @YPares
    Collaborator

    @borsboom After doing some tests on OSX, I can say it's not specific to stack. cabal-install has the same problem.
    When trying to build https://github.com/tweag/HaskellR/tree/master/inline-r for instance:

    $ cabal configure --extra-lib-dirs=/nix/store/hjr3jryjq1kgk1lgc7ikwnqh0xwlc5yx-R-3.2.2/lib/R/lib
    $ cabal build
    

    it causes the error (during the hsc2hs phase since it says dist/build/Foreign/R/Type_hsc_make failed), whereas (after cleaning)

    $ export DYLD_LIBRARY_PATH=/nix/store/hjr3jryjq1kgk1lgc7ikwnqh0xwlc5yx-R-3.2.2/lib/R/lib
    $ cabal configure
    $ cabal build
    

    works. So it's not stack-specific, cabal does the same.

  10. mgsloan commented on Nov 21, 2015

    @mgsloan
    Contributor

    Sounds like we should close this one out and file a bug with Cabal (if one doesn't already exist).

  11. borsboom commented on Nov 23, 2015

    @borsboom
    Contributor

    @YPares: unless I am misunderstanding the issue, it actually looks like this problem is Stack-specific based on your results with cabal-install. As I understand it, Stack doesn't even work if you do set DYLD_LIBRARY_PATH, since El Capitan's System Integrity Protection prevents the environment variable from being passed through. Did you perform your test on El Capitan?

  12. 23 remaining items

  13. borsboom commented on Feb 20, 2016

    @borsboom
    Contributor

    And now I have workaround that worked in my case (more-or-less what I mentioned in #1161 (comment)). First I copied /bin/sh to an another location in my home directory, then I edited <GHC-PATH>/bin/hsc2hs (which is a shell script) and replaced the path in #!/bin/sh with the new path. This fixed it for all three cases (stack, cabal-install, and directly using runghc Setup.lhs). I suspect that some of the other wrappers (such as ghc and ghci at least) would also need to be modified to handle loading dynamic libraries in Template Haskell and the REPL, so probably best to do this to all of them.

    Potentially Stack could apply this sort of workaround itself. stack setup could copy /bin/sh somewhere into ~/.stack/programs and scan the GHC bin directory for any shell scripts and replace their shebangs to the new PATH. To work around it with a system-installed GHC would be a bit more involved -- it would have to copy the shell scripts elsewhere before rewriting the shebangs and then modify PATH so that the rewritten scripts are preferred over those installed in the system.

  14. borsboom commented on Feb 20, 2016

    @borsboom
    Contributor
  15. bitemyapp commented on Feb 20, 2016

    @bitemyapp
    ContributorAuthor

    Thank you @borsboom! All the work to get this sorted is much appreciated 😄

  16. sam2000 commented on Feb 20, 2016

    @sam2000

    What wonderful news! I recently wanted to give yesod a try but when I realised that just setting it up required stack which means messing with OSX security stuff I gave up on it! Now what you have done gives me hope! Thank you so much Emanuel Borsboom! You are a star!

    Sent from my iPhone

    On 20 Feb 2016, at 22:06, Emanuel Borsboom [email protected] wrote:

    And now I have workaround that worked in my case (more-or-less what I mentioned in #1161 (comment)). First I copied /bin/sh to an another location in my home directory, then I edited GHC-PATH/bin/hsc2hs (which is a shell script) and replaced the path in #!/bin/sh with the new path. This fixed it for all three cases (stack, cabal-install, and directly using runghc Setup.lhs).

    Potentially Stack could apply this sort of workaround itself. stack setup could copy /bin/sh somewhere into ~/.stack/programs and scan the GHC bin directory for any shell scripts and replace their shebangs to the new PATH. To work around it with a system-installed GHC would be a bit more involved -- it would have to copy the shell scripts elsewhere before rewriting the shebangs and then modify PATH so that the rewritten scripts are preferred over those installed in the system.

    This all seems pretty doable

    —
    Reply to this email directly or view it on GitHub.

  17. borsboom commented on Feb 20, 2016

    @borsboom
    Contributor

    @sam2000 Chances are Yesod will work out of the box, without making any changes to SIP, anyway. This is only necessary for the rare case when you have to set DYLD_LIBRARY_PATH, and I had to manually move a some libraries out of the location where Homebrew installs them in order to reproduce this.

  18. sam2000 commented on Feb 21, 2016

    @sam2000

    @EmanuelBorsboom in that case I will try this on my Mac. Thanks again!

    Sent from my iPhone

    On 20 Feb 2016, at 23:40, Emanuel Borsboom [email protected] wrote:

    @sam2000 Chances are Yesod will work out of the box, without making any changes to SIP, anyway. This is only necessary for the rare case when you have to set DYLD_LIBRARY_PATH, and I had to manually move a some libraries out of the location where Homebrew installs them in order to reproduce this.

    —
    Reply to this email directly or view it on GitHub.

  19. borsboom commented on Mar 31, 2016

    @borsboom
    Contributor

    Gershom recently made this comment on the GHC issue:

    Having done a little investigating, it seems to me that the real answer is "you shouldn't need to set DYLD_LIBRARY_PATH" . Indeed I don't think there are any normal workflows on OS X that should require this. The issue would only arise when linking to an external lib that inserts a path on DYLD_LIBRARY_PATH, and such libs are now effectively basically broken throughout the OS X ecosystem.

    Here are a few articles that recommend against setting it (ever) as taken from a thread discussing this issue on Oracle bindings for node:

    ​https://blogs.oracle.com/ali/entry/avoiding_ld_library_path_the
    ​http://linuxmafia.com/faq/Admin/ld-lib-path.html
    We may want to add a FAQ page for manual workarounds, but my gut says "don't fix, and recommend to those who encounter this that they should reconsider why they need DYLD_LIBRARY_PATH to begin with".

    That said, I'm not one of the people "feeling the pain" here, so opinions may vary.

    I've never been bitten by this problem (I've always installed libraries using Homebrew, which puts them in /usr/local/lib where they belong), so I can't really offer any particular opinion there. If you do have this problem, and you disagree with Gershom's assessment, please comment on the GHC issue.

    Any ideas for workarounds that don't require DYLD_LIBRARY_PATH also appreciated. For example, maybe we can document an easy way to specify the location of libraries via the command-line rather than via the environment variable.

  20. borsboom commented on Jun 23, 2016

    @borsboom
    Contributor

    Since the discussion here seems to have stalled without any feedback from anyone still having problems, I'm closing this issue. We can re-open later if necessary.

  21. dtaskoff commented on Nov 6, 2017

    @dtaskoff

    Is it possible for stack to copy the sh from which sh and modify the scripts in stack path | grep compiler-bin | cut -d' ' -f2 to use the copied executable?
    Doing that manually solves the issue.

  22. dtaskoff commented on Nov 6, 2017

    @dtaskoff

    To save some people that manual work, I'm providing the script I'm using.

    NOTE: the script must be ran from a stack project or you can pass --stack-yaml <path-to-stack-yaml> to it (so that it can find the appropriate directory containing the GHC scripts for that project).

    If you're okay with that behaviour, I can try to implement it in the stack setup step for macOSes.

  23. mboes commented on Jun 3, 2019

    @mboes
    Contributor

    FTR this breaks inline-java. The reason is: the jni package uses hsc2hs. Cabal seems to think hsc2hs should link against libjvm.so if the jni library will, but it doesn't set an -rpath. So the intermediate binary produced by hsc2hs fails to run because it can't find libjvm.so. The reason this hits us now and did not do so previously is because Homebrew now installs all JVM's in /Library/Java/JavaVirtualMachines, which is a directory protected by SIP, whereas /usr/local/opt, which is where things previously were, is not.

    DYLD_LIBRARY_PATH would be a workaround to the -rpath issue, except that with SIP that env var becomes pretty useless. :-/

  24. ocramz commented on Jan 5, 2023

    @ocramz

    I can reproduce this behaviour with stack 2.9.1 on MacOS Big Sur (11.5.2).

    1. build with stack build --ghc-options "-L<dir> -l<library-name>" (dynlib file is in dir)
    2. stack exec ... crashes with "Image not found" ❌
    3. Running the built program manually with ./.stack-work/... and the DYLD_LIBRARY_PATH variable set to the absolute path of the directory where the dynlib is successful ✅

    Thank you to @alexbiehl for finding the root cause and @bitemyapp for showing a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions