Debugging gi-gtk with VsCode on Windows

After many hours of lonely struggle, I managed to get a sample Gtk program to build and execute with stack.
HLS is working fine and the GHCi Debug Adapter works with non-Gtk code.
When I try to debug a Gtk program, the first Gtk instruction crashes with

[CRITICAL][APP] invalid dap result from ghci. ["","Access violation in generated code when executing data at 0x7ff6975305b0", ...

At the start of the debugging session, there are several errors of the form

Symbol type mismatch.
Symbol IID_IDXGIObject was defined by C:\Users\info\AppData\Local\Programs\stack\x86_64-windows\ghc-9.4.6\mingw\x86_64-w64-mingw32\lib\libuuid.a to be a data symbol.
      yet was defined by C:\Users\info\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\lib\libcairo.dll.a to be a data symbol.

I have the same problem when simply trying to load and run the program with stack ghci

I’m using the latest stack snapshot.

What could my problem be ?

Your struggle may be lonely, but you are not alone in your struggles. I wrote up some notes on my own July 2022 experiments here. Your post has prompted me to look again at GTK4 on Windows with a modern version of GHC.

2 Likes

Hello Mike, I wish I’d found your tutorial before.
I first installed gtk4, but the stack version of gtk requires gtk3.
To find the start of the Windows path to the msys2 shared directories, I used stack exec -- mount.

My 2022 notes are still more or less good in 2023. For GTK4, I am using example Main.hs:

{-# LANGUAGE LexicalNegation   #-}
{-# LANGUAGE OverloadedLabels  #-}
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

-- From package haskell-gi-base
import           Data.GI.Base (AttrOp ((:=)), new, on, set)
-- From package gi-gtk
import qualified GI.Gtk       as Gtk

import           Control.Monad (void)

main :: IO ()
main = do
  app <- new Gtk.Application []
  on app #activate (activate app)
  void $ #run app Nothing

activate :: Gtk.Application -> IO ()
activate app = do
  win <- new Gtk.Window [ #title := "Hi there" ]
  button <- new Gtk.Button [ #label := "Click me" ]
  on button #clicked (set button [ #sensitive := False,
                                   #label := "Thanks for clicking me" ])
  #setChild win (Just button)
  #setDefaultSize win 400 -1
  #show win
  #addWindow app win

My package.yaml is (I don’t need to constrain the upper bound on vector):

name:                gtkTest
version:             0.1.0.0

dependencies:
- base >= 4.7 && < 5
- gi-gtk >= 4
- haskell-gi-base
- vector

executables:
  gtkTest:
    main:                Main.hs
    source-dirs:         app
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N

My stack.yaml is:

# GHC 9.4.6 and gi-gtk-3.0.41
resolver: lts-21.9

extra-deps:
- gi-gdk-4.0.7@sha256:e1915be39e2eebaf445584986dea7b750aef57c00894d1a218a54811da27b381,9025
- gi-gsk-4.0.7@sha256:1c96f0d0fdc39b1dfa3314cb00e57cb39931bd7f1a125fb0890435fcae624952,7393
- gi-gtk-4.0.8@sha256:6011c2d7880c6f9b1a319a593db6b76e5175b3f020fc4b3ca86707ee70c3a1d8,35759

I can check the name of my Stack-supplied MSYS2 directory with:

> stack ls tools --filter msys2
msys2-20230526

and I can check I have set up the environment variables correctly with dir Env:PKG_CONFIG_PATH and dir Env:XDG_DATA_DIRS, which on my machine yield:

Name                           Value
----                           -----
PKG_CONFIG_PATH                C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\lib\pkgconfig
XDG_DATA_DIRS                  C:\Users\mike\AppData\Local\Programs\stack\x86_64-windows\msys2-20230526\mingw64\share

After all that, stack build and stack exec gtkTest behave as expected. stack ghci however, is disappointing:

❯ stack ghci
Using main module:
1.  Package gtkTest, component gtkTest:exe:gtkTest, with main-is file: D:\Users\mike\Code\Haskell\gtkTest2023\app\Main.hs.


Warning: The following GHC options are incompatible with GHCi and have not been passed to it: -threaded.

Configuring GHCi with the following packages: gtkTest.
GHCi, version 9.4.6: https://www.haskell.org/ghc/  :? for help
[1 of 3] Compiling Main             ( D:\Users\mike\Code\Haskell\gtkTest2023\app\Main.hs, interpreted )
[2 of 3] Compiling Paths_gtkTest    ( D:\Users\mike\Code\Haskell\gtkTest2023\.stack-work\dist\51f21a8f\build\gtkTest\autogen\Paths_gtkTest.hs, interpreted )
Ok, two modules loaded.
Loaded GHCi configuration from C:\Users\mike\AppData\Local\stack\ghci-script\aa682954\ghci-script
ghci> main

Access violation in generated code when executing data at 0x7ff6f31df010

 Attempting to reconstruct a stack trace...

   Frame        Code address
 * 0x28361fda80 0x7ff6f31df010
 * 0x28361fda88 0x7ff6efa55ae2 C:\sr\snapshots\debcadbf\lib\x86_64-windows-ghc-9.4.6\gi-gtk-4.0.8-6qUVSHE3e3gLkatWPgxKXJ\libHSgi-gtk-4.0.8-6qUVSHE3e3gLkatWPgxKXJ.a(#65:ghc_1998.o)+0x5ad2
                 (gizmgtkzm4zi0zi8zm6qUVSHE3e3gLkatWPgxKXJ_GIziGtkziObjectsziApplication_toApplication_info+0x55e)
3 Likes

Thanks Mike, it looks like we’re in the same situation.

Yes. I don’t think this is Stack - I get something similar with ‘raw’ GHCi. Looks like an issue for haskell-gi/haskell-gi or GHC, or both.