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

UnboundLocalError when system cmake is installed using pip #718

Closed
njzjz opened this issue Apr 20, 2024 · 5 comments · Fixed by #719
Closed

UnboundLocalError when system cmake is installed using pip #718

njzjz opened this issue Apr 20, 2024 · 5 comments · Fixed by #719

Comments

@njzjz
Copy link
Contributor

njzjz commented Apr 20, 2024

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.

@njzjz
Copy link
Contributor Author

njzjz commented Apr 20, 2024

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,
             )

@henryiii
Copy link
Collaborator

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.

@njzjz
Copy link
Contributor Author

njzjz commented Apr 21, 2024

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())

@henryiii
Copy link
Collaborator

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.

henryiii added a commit that referenced this issue Apr 21, 2024
Fix #718

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
@henryiii
Copy link
Collaborator

Was able to test locally. Fixed in 0.9.2.

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 a pull request may close this issue.

2 participants