You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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?
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.
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.
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...
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.
@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.
@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?
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.
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!
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.
@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.
@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.
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:
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.
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.
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.
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.
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. :-/
I can reproduce this behaviour with stack 2.9.1 on MacOS Big Sur (11.5.2).
build with stack build --ghc-options "-L<dir> -l<library-name>" (dynlib file is in dir)
stack exec ... crashes with "Image not found" ❌
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.
Activity
snoyberg commentedon Oct 15, 2015
Just saw this issue after responding on Reddit. I'm not aware of anything Stack is doing to make
DYLD_LIBRARY_PATHbe handled specially, perhaps it's the OS itself doing something funny?alexbiehl commentedon Oct 15, 2015
Source: https://forums.developer.apple.com/thread/9233
snoyberg commentedon Oct 15, 2015
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.
borsboom commentedon Oct 16, 2015
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_PATHitself. But if cabal-install doesn't have this problem then it should be fixable in Stack. Could it be related to the fact thatghcis 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...
DemiMarie commentedon Oct 20, 2015
Sounds like the only solution is to avoid going through a shell script wrapper, or distributing a custom shell.
alexbiehl commentedon Oct 20, 2015
I can have a look at it when I'm back home. The idea is:
I was able to compile with regular
libmysqlclient(without settingDYLD_LIBRARY_PATH) maybe it was inPATHor some other variable so thathsc2hswere able to find it nonetheless. The same principle should apply tolibmariadb. Maybe then I can find what differed there.borsboom commentedon Oct 25, 2015
@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.
YPares commentedon Nov 20, 2015
@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:
it causes the error (during the hsc2hs phase since it says
dist/build/Foreign/R/Type_hsc_make failed), whereas (after cleaning)works. So it's not stack-specific, cabal does the same.
mgsloan commentedon Nov 21, 2015
Sounds like we should close this one out and file a bug with Cabal (if one doesn't already exist).
borsboom commentedon Nov 23, 2015
@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?
23 remaining items
borsboom commentedon Feb 20, 2016
And now I have workaround that worked in my case (more-or-less what I mentioned in #1161 (comment)). First I copied
/bin/shto 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/shwith the new path. This fixed it for all three cases (stack, cabal-install, and directly usingrunghc Setup.lhs). I suspect that some of the other wrappers (such asghcandghciat 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 setupcould copy/bin/shsomewhere into~/.stack/programsand 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.borsboom commentedon Feb 20, 2016
Opened a GHC issue to discuss solutions: https://ghc.haskell.org/trac/ghc/ticket/11617
bitemyapp commentedon Feb 20, 2016
Thank you @borsboom! All the work to get this sorted is much appreciated 😄
sam2000 commentedon Feb 20, 2016
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
borsboom commentedon Feb 20, 2016
@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.
sam2000 commentedon Feb 21, 2016
@EmanuelBorsboom in that case I will try this on my Mac. Thanks again!
Sent from my iPhone
borsboom commentedon Mar 31, 2016
Gershom recently made this comment on the GHC issue:
I've never been bitten by this problem (I've always installed libraries using Homebrew, which puts them in
/usr/local/libwhere 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.
borsboom commentedon Jun 23, 2016
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.
dtaskoff commentedon Nov 6, 2017
Is it possible for
stackto copy theshfromwhich shand modify the scripts instack path | grep compiler-bin | cut -d' ' -f2to use the copied executable?Doing that manually solves the issue.
dtaskoff commentedon Nov 6, 2017
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 setupstep for macOSes.mboes commentedon Jun 3, 2019
FTR this breaks inline-java. The reason is: the
jnipackage useshsc2hs. Cabal seems to thinkhsc2hsshould link againstlibjvm.soif thejnilibrary will, but it doesn't set an-rpath. So the intermediate binary produced byhsc2hsfails to run because it can't findlibjvm.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_PATHwould be a workaround to the-rpathissue, except that with SIP that env var becomes pretty useless. :-/ocramz commentedon Jan 5, 2023
I can reproduce this behaviour with
stack 2.9.1on MacOS Big Sur (11.5.2).stack build --ghc-options "-L<dir> -l<library-name>"(dynlib file is indir)stack exec ...crashes with "Image not found" ❌./.stack-work/...and theDYLD_LIBRARY_PATHvariable 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.