Repository: pyinstaller/pyinstaller

Issue:
GTK3 MSYS2 ImportError: Typelib file for namespace 'freetype2', version '2.0' not found.
## Description of the issue

Looks like pyinstaller forgets to package freetype2 with the other libraries causing gi to fail importing it.

### Context information (for bug reports)

* Output of `pyinstaller --version`: ```5.2```
* Version of Python: ```Python 3.10.5```
* Platform: Windows 11 English
* Used MSYS2 with "MSYS2 MINGW 64-bit" enviroment
* Used the latest development version, using the following command:

```shell
pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip
```
* [X] Started with clean installation
* [X] Used the latest development version

### A minimal example program which shows the error
```
# test.py

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

print("Hello I'm a Test")
```

### Stacktrace / full error message
```
Traceback (most recent call last):
  File "gi/importer.py", line 142, in load_module
  File "gi/module.py", line 257, in get_introspection_module
  File "gi/module.py", line 109, in __init__
gi.RepositoryError: Typelib file for namespace 'freetype2', version '2.0' not found

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 7, in <module>
  File "gi/importer.py", line 144, in load_module
ImportError: Typelib file for namespace 'freetype2', version '2.0' not found
[13600] Failed to execute script 'main' due to unhandled exception!
```


Discussion:

Resolving patch:
diff --git a/PyInstaller/hooks/hook-gi.repository.GObject.py b/PyInstaller/hooks/hook-gi.repository.GObject.py
--- a/PyInstaller/hooks/hook-gi.repository.GObject.py
+++ b/PyInstaller/hooks/hook-gi.repository.GObject.py
@@ -8,10 +8,12 @@
 #
 # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
 #-----------------------------------------------------------------------------
-
+from PyInstaller.utils.hooks import is_module_satisfies
 from PyInstaller.utils.hooks.gi import GiModuleInfo
 
 module_info = GiModuleInfo('GObject', '2.0')
 if module_info.available:
     binaries, datas, hiddenimports = module_info.collect_typelib_data()
-    hiddenimports += ['gi._gobject']
+    # gi._gobject removed from PyGObject in version 3.25.1
+    if is_module_satisfies('PyGObject < 3.25.1'):
+        hiddenimports += ['gi._gobject']
diff --git a/PyInstaller/hooks/hook-gi.repository.freetype2.py b/PyInstaller/hooks/hook-gi.repository.freetype2.py
new file mode 100644
--- /dev/null
+++ b/PyInstaller/hooks/hook-gi.repository.freetype2.py
@@ -0,0 +1,16 @@
+#-----------------------------------------------------------------------------
+# Copyright (c) 2005-2022, PyInstaller Development Team.
+#
+# Distributed under the terms of the GNU General Public License (version 2
+# or later) with exception for distributing the bootloader.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#
+# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
+#-----------------------------------------------------------------------------
+
+from PyInstaller.utils.hooks.gi import GiModuleInfo
+
+module_info = GiModuleInfo('freetype2', '2.0')
+if module_info.available:
+    binaries, datas, hiddenimports = module_info.collect_typelib_data()
diff --git a/PyInstaller/hooks/pre_safe_import_module/hook-gi.repository.freetype2.py b/PyInstaller/hooks/pre_safe_import_module/hook-gi.repository.freetype2.py
new file mode 100644
--- /dev/null
+++ b/PyInstaller/hooks/pre_safe_import_module/hook-gi.repository.freetype2.py
@@ -0,0 +1,16 @@
+#-----------------------------------------------------------------------------
+# Copyright (c) 2005-2022, PyInstaller Development Team.
+#
+# Distributed under the terms of the GNU General Public License (version 2
+# or later) with exception for distributing the bootloader.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#
+# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
+#-----------------------------------------------------------------------------
+
+
+def pre_safe_import_module(api):
+    # PyGObject modules loaded through the gi repository are marked as MissingModules by modulegraph, so we convert them
+    # to RuntimeModules in order for their hooks to be loaded and executed.
+    api.add_runtime_module(api.module_name)


Test patch: