[Bug] g.extension fails in CMake builds for script addons that import grass.pygrass at module level ("Could not load grass_gis")

Aperta
#8,220 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Shreyanshsingh23 ci sta già lavorando.

Dal 23/11/2026.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cmake, linux, python

Direzione di ricerca

Start by reading build_addon.cmake and tracing the environment used by the --html-description and --md-description commands through g.extension and the build process. The report identifies the missing library search path as a likely cause but has not located where it is lost, so first confirm that path. Done means script addons that import grass.pygrass or grass.lib at module level install in the described CMake environment.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug CMake

Describe the bug

With the conda-forge GRASS 8.5 package on Linux (a CMake build), g.extension fails for script addons that import grass.pygrass or grass.lib at module level, for example r.learn.train and r.learn.predict (r.learn.ml2).

The addon build generates the manual page by running the script with --html-description. The module-level import loads libgrass_gis through ctypes, the load fails, and the build stops:

Traceback (most recent call last):
  File "/tmp/tmpnbuc2gzu/r.learn.train/grass_addons/src/raster/r.learn.ml2/r.learn.train/build/output/scripts/r.learn.train", line 347, in <module>
    from grass.pygrass.raster import RasterRow
  File "/usr/local/lib/grass85/etc/python/grass/pygrass/raster/__init__.py", line 10, in <module>
    import grass.lib.gis as libgis
  File "/usr/local/lib/grass85/etc/python/grass/lib/gis.py", line 30, in <module>
    _libs["grass_gis"] = load_library("grass_gis")
  File "/usr/local/lib/grass85/etc/python/grass/lib/ctypes_loader.py", line 105, in __call__
    raise ImportError("Could not load %s." % libname)
ImportError: Could not load grass_gis.
gmake[2]: *** [CMakeFiles/r.learn.train-docs.dir/build.make:74: output/docs/html/r.learn.train.html] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:122: CMakeFiles/r.learn.train-docs.dir/all] Error 2
gmake: *** [Makefile:124: all] Error 2
ERROR: Compilation failed, sorry. Please check above error messages.

To reproduce

On Google Colab:

  1. pip install condacolab, then condacolab.install()
  2. mamba install -y grass=8.5 scikit-learn
  3. Start a session in a notebook with grass.jupyter.init(<mapset>)
  4. gs.run_command("g.extension", extension="r.learn.train")

Discovered when testing a my courses lab in Google Colab image_classification.ipynb

Expected behavior

The addon installs. In a session started by GRASS, building an addon should not depend on the caller's dynamic library path.

What I found

  • The shared libraries are in $GISBASE/lib (/usr/local/lib/grass85/lib), which is not a system linker directory.

  • import grass.lib.gis works in the session environment and fails as soon as LD_LIBRARY_PATH is removed, with each Python on the machine (conda Python 3.13.13 and the Colab kernel Python):

    /usr/local/bin/python3 -> ImportError: Could not load grass_gis.
    /usr/local/bin/python  -> ImportError: Could not load grass_gis.
    /usr/bin/python3.real  -> ImportError: Could not load grass_gis.
    
  • The generated wrapper calls add_library_search_dirs([]) with an empty list, so the ctypes loader can only find libgrass_gis through LD_LIBRARY_PATH or the system linker cache.

  • In build_addon.cmake, the --html-description and --md-description commands set only PYTHONPATH, GISBASE, GISRC, VERSION_NUMBER, and VERSION_DATE. They rely on the library path being inherited.

  • Setting LD_LIBRARY_PATH to include $GISBASE/lib in the notebook before calling g.extension did not help: the import then worked in a subprocess of the notebook, but the docs step of the build failed with the same error. I have not yet identified where the variable is lost between g.extension, cmake --build, gmake, and the script.

Workaround

Registering the library directory with the system linker makes the import independent of the environment, and the addons then install:

Path("/etc/ld.so.conf.d/grass.conf").write_text(f"{os.environ['GISBASE']}/lib\n")
subprocess.run(["ldconfig"], check=True)

After this, the same test without LD_LIBRARY_PATH loads the library under all three interpreters, and r.learn.train, r.learn.predict, i.superpixels.slic, i.segment.stats, r.confusionmatrix, and i.sam2 install.

Possible fixes

  • In build_addon.cmake, set the dynamic library path explicitly ($GISBASE/lib) on the commands that run the script for --html-description and --md-description.
  • Have the ctypes wrappers in grass.lib search $GISBASE/lib, so grass.lib and grass.pygrass do not depend on LD_LIBRARY_PATH.

Additional context

The same build prints CMake policy CMP0010 warnings for unescaped regular expressions in build_addon.cmake:

build_addon.cmake:56   '</body>\|</html>\|</div>.<!--.end.container.-->'   Invalid escape sequence \|
build_addon.cmake:123  .*\.py$                                             Invalid escape sequence \.
build_addon.cmake:249  .*\.(jpg|png)$                                      Invalid escape sequence \.

It also warns that the addon's CMakeLists.txt calls project() without cmake_minimum_required() (r.learn.ml2/r.learn.train/CMakeLists.txt, in grass-addons).

System description

  • Operating system: Google Colab (Ubuntu, x86_64)
  • GRASS: 8.5 from conda-forge, installed with condacolab into /usr/local
  • Python: 3.13.13 (conda)
Lingua principale
C
Stelle
1.2k
Fork
447
Merge medio
2g 10h
PR unite (30g)
86

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OSGeo/grass

Tutte le issue di OSGeo/grass

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.