Skip to content

Do not use environment variables when empty#48

Merged
buddly27 merged 1 commit intopython-cmake:mainfrom
corrodedHash:main
Jan 29, 2025
Merged

Do not use environment variables when empty#48
buddly27 merged 1 commit intopython-cmake:mainfrom
corrodedHash:main

Conversation

@corrodedHash
Copy link
Contributor

Hello,
thank you for making this!
Worked almost flawlessly, but CMake had one error:

CMake Error (dev) at cmake/FindPytest.cmake:74 (cmake_path):
  uninitialized variable 'LD_LIBRARY_PATH'
Call Stack (most recent call first):
  test/CMakeLists.txt:3 (pytest_discover_tests)
This error is for project developers. Use -Wno-error=dev to suppress it.

CMake Error (dev) at cmake/FindPytest.cmake:75 (cmake_path):
  uninitialized variable 'PYTHONPATH'
Call Stack (most recent call first):
  test/CMakeLists.txt:3 (pytest_discover_tests)
This error is for project developers. Use -Wno-error=dev to suppress it.

It seemed unhappy when the environment variables were not defined.
I am uncertain if I am using it wrong, or maybe this check is only done in newer versions of CMake, I made a little change to only convert the paths if the environment variables are not empty.

@buddly27
Copy link
Collaborator

buddly27 commented Dec 7, 2024

@corrodedHash Hi, thanks for contributing!

I am a bit surprised by this issue, as neither cmake_path nor the environment getter syntax ($ENV{...}) should trigger an error for uninitialized variables. But maybe I'm missing something.

I couldn't reproduce this issue with the following example:

cmake_path(CONVERT "UNINITIALIZED_VAR" TO_CMAKE_PATH_LIST RESULT)
cmake_path(CONVERT "$ENV{UNINITIALIZED_VAR}" TO_CMAKE_PATH_LIST RESULT)

Could you give me more details about your use case? (Cmake version, platform, etc)

@corrodedHash
Copy link
Contributor Author

I now know this error came from my CMakePreset.txt configuration, which I copied from a template and invested too little time in to understand.
This is the relevant part:

"warnings": {
  "uninitialized": true,
},
"errors": {
  "dev": true,
}

Or the CMake flags -Werror=dev --warn-uninitialized.

I copied the two Pytest CMake files into my tree, which is probably why the linting is also triggering there.

As such, this is not a defect of this project.

@buddly27
Copy link
Collaborator

I'm still struggling to reproduce this error with your presets, or their corresponding command line options:
https://godbolt.org/z/bT71K65vs

Is there anything I'm missing?

Safeguarding against uninitialized variables can be indeed important in the context of restricted settings, but I just want to ensure I can reliably reproduce this issue for confirmation.

@corrodedHash
Copy link
Contributor Author

The godbolt explorer was a great idea, thank you!

It seems to be doing some work in the background, if I execute the two lines on my system, I get warnings that I should use project and cmake_minimum_required.

With cmake_minimum_required(VERSION 3.29) I get at least a warning; -Werror=dev has no effect:
https://godbolt.org/z/vrcdYonec

@buddly27
Copy link
Collaborator

Ah, I see! CMake behaves differently when used as a script vs. as a project builder, that's a strange one. Good catch!

CMake reported uninitialized variable errors for `LD_LIBRARY_PATH` and
`PYTHONPATH` when using `-Werror=dev --warn-uninitialized`, causing
`pytest_discover_tests` to fail.
@buddly27 buddly27 merged commit 9c1ef8d into python-cmake:main Jan 29, 2025
72 checks passed
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 this pull request may close these issues.

2 participants