scikit-build/scikit-build-core

UnboundLocalError when system cmake is installed using pip

njzjz opened this issue · 5 comments

I got the following error

 Traceback (most recent call last):
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/program_search.py", line 81, in get_cmake_program
      result = Run().capture(cmake_path, "-E", "capabilities")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/_shutil.py", line 43, in capture
      return self._run(args, capture=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/_shutil.py", line 71, in _run
      return subprocess.run(
             ^^^^^^^^^^^^^^^
    File "/home/njzjz/anaconda3/lib/python3.12/subprocess.py", line 571, in run
      raise CalledProcessError(retcode, process.args,
  subprocess.CalledProcessError: Command '['/home/njzjz/anaconda3/bin/cmake', '-E', 'capabilities']' returned non-zero exit status 1.

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/program_search.py", line 90, in get_cmake_program
      result = Run().capture(cmake_path, "--version")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/_shutil.py", line 43, in capture
      return self._run(args, capture=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/_shutil.py", line 71, in _run
      return subprocess.run(
             ^^^^^^^^^^^^^^^
    File "/home/njzjz/anaconda3/lib/python3.12/subprocess.py", line 571, in run
      raise CalledProcessError(retcode, process.args,
  subprocess.CalledProcessError: Command '['/home/njzjz/anaconda3/bin/cmake', '--version']' returned non-zero exit status 1.

  During handling of the above exception, another exception occurred:

  Traceback (most recent call last):
    File "/home/njzjz/anaconda3/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
      main()
    File "/home/njzjz/anaconda3/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
      json_out['return_val'] = hook(**hook_input['kwargs'])
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/home/njzjz/anaconda3/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
      return hook(config_settings)
             ^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/build/__init__.py", line 126, in get_requires_for_build_wheel
      [*requires.cmake(), *requires.ninja()] if requires.settings.wheel.cmake else []
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/builder/get_requires.py", line 78, in cmake
      cmake = best_program(get_cmake_programs(module=False), version=cmake_verset)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/program_search.py", line 169, in best_program
      for program in programs:
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/program_search.py", line 125, in get_cmake_programs
      yield get_cmake_program(cmake_path)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "/tmp/pip-build-env-nizg97b2/overlay/lib/python3.12/site-packages/scikit_build_core/program_search.py", line 105, in get_cmake_program
      result.stdout,
      ^^^^^^
  UnboundLocalError: cannot access local variable 'result' where it is not associated with a value

The error handling here has a problem. So I hack subprocess.py to print the stdout/stderr:

   Traceback (most recent call last):
    File "/home/njzjz/anaconda3/bin/cmake", line 5, in <module>
      from cmake import cmake
  ModuleNotFoundError: No module named 'cmake'

   Traceback (most recent call last):
    File "/home/njzjz/anaconda3/bin/cmake", line 5, in <module>
      from cmake import cmake
  ModuleNotFoundError: No module named 'cmake'

This seems to be expected, as scikit-build-core runs in an isolated environment.

The error handing problem should be related to #717.

After checking #717, I think this will work:

-        except subprocess.CalledProcessError:
+        except subprocess.CalledProcessError as result:
             logger.warning(
                 "Could not determine CMake version via --version, got {!r} {!r}",
                 result.stdout,
                 result.stderr,
             )

A script called CMake that imports CMake from CMake sounds suspect (what is anaconda doing?), but you are correct, static checks didn’t catch the unbound variable bug.

A script called CMake that imports CMake from CMake sounds suspect (what is anaconda doing?)

To be clear, I installed cmake using pip install cmake. cmake.cmake is defined here as a script.

The content of bin/cmake:

#!/home/njzjz/anaconda3/bin/python3.12
# -*- coding: utf-8 -*-
import re
import sys
from cmake import cmake
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(cmake())

Yes, that makes sense - if cmake is installed in an "outer" environment (such as no environment), then it might get picked up on the path but not have a valid Python install. If cmake became a real "script", that would fix this, though I'm not sure where I'd need to put the helper module files if we do that (something I do want to explore).

Hopefully this is fixed in #719. It's a tricky1 thing to test for.

Footnotes

  1. as in it would take more time than I have currently for quick fixes to set one up.

Was able to test locally. Fixed in 0.9.2.