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

Avoid Windows linker errors with GHC 9.4.5+ #6

Merged
merged 1 commit into from
May 11, 2023
Merged

Avoid Windows linker errors with GHC 9.4.5+ #6

merged 1 commit into from
May 11, 2023

Conversation

RyanGlScott
Copy link

@RyanGlScott RyanGlScott commented May 11, 2023

Previously, text-ansi declared a direct foreign import on the isatty function. This is subtly incorrect on Windows, which provides an _isatty function rather than isatty. (See https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-isatty?view=msvc-170)

This happened to work on pre-9.4.5 versions of GHC due to them linking against the msvcrt C runtime (which still provided isatty), but GHC 9.4.5 and later link against ucrt, which does not provide isatty at all. This manifests in linker errors when using text-ansi in GHCi or Template Haskell on Windows, as seen in https://gitlab.haskell.org/ghc/ghc/-/issues/23378 and in this hledger build: https://github.com/simonmichael/hledger/actions/runs/4931242306/jobs/8815809083

The solution is to instead declare a foreign import on _isatty. The c_isatty function from System.Posix.Internals in base already does this, in fact, so I have removed the hand-written c_isatty functions in text-ansi in favor of the one in System.Posix.Internals. (Despite that module's name, System.Posix.Internals is cross-platform and does in fact work on Windows.)

Previously, `text-ansi` declared a direct `foreign import` on the `isatty`
function. This is subtly incorrect on Windows, which provides an `_isatty`
function rather than `isatty`. (See
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/posix-isatty?view=msvc-170)

This happened to work on pre-9.4.5 versions of GHC due to them linking against
the `msvcrt` C runtime (which still provided `isatty`), but GHC 9.4.5 and later
link against `ucrt`, which does not provide `isatty` at all. This manifests in
linker errors when using `text-ansi` in GHCi or Template Haskell on Windows, as
seen in https://gitlab.haskell.org/ghc/ghc/-/issues/23378 and in this `hledger`
build:
https://github.com/simonmichael/hledger/actions/runs/4931242306/jobs/8815809083

The solution is to instead declare a foreign import on `_isatty`. The
`c_isatty` function from `System.Posix.Internals` in `base` already does this,
in fact, so I have removed the hand-written `c_isatty` functions in `text-ansi`
in favor of the one in `System.Posix.Internals`. (Despite that module's name,
`System.Posix.Internals` is cross-platform and does in fact work on Windows.)
@mitchellwrosen
Copy link
Member

Excellent, thank you!

@mitchellwrosen mitchellwrosen merged commit b9e1084 into awkward-squad:main May 11, 2023
7 checks passed
@mitchellwrosen
Copy link
Member

@RyanGlScott
Copy link
Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants