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

How to gracefully handle failing downloads? #383

Open
wouterbeek opened this issue Dec 26, 2021 · 1 comment
Open

How to gracefully handle failing downloads? #383

wouterbeek opened this issue Dec 26, 2021 · 1 comment

Comments

@wouterbeek
Copy link

wouterbeek commented Dec 26, 2021

Observed

With the currently documented approach for obtaining this tool, a failed download results in an empty file called conan.cmake in the build directory:

list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})

# Some lines skipped.

if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
  message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
  file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.16.1/conan.cmake"
                "${CMAKE_BINARY_DIR}/conan.cmake"
                EXPECTED_HASH SHA256=396e16d0f5eabdc6a14afddbcfff62a54a7ee75c6da23f32f7a31bc85db23484
                TLS_VERIFY ON)
endif()

include(${CMAKE_BINARY_DIR}/conan.cmake)

This means that a second run of the same CMake script, now with a working Internet connection, will also fail:

CMake Error at Generics.cmake:308 (conan_cmake_run):
  Unknown CMake command "conan_cmake_run".

The existence check for ${CMAKE_BINARY_DIR}/conan.cmake now succeeds on the empty file, and the include statement succeeds too. But none of the CMake/Conan features are available in that included empty file, so the conan_cmake_run statement will fail.

Expected

I expect a failing downloading to be handled gracefully. CMake seems to have facilities for this that check the HTTP status code and allow cleanup actions based on an observed error status code.

Suggestion

I currently use the following snippet instead of the one included in the README of this project to cleanup after a failed download (I'm using the latest version in this snippet):

list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
if(NOT EXISTS ${CMAKE_BINARY_DIR}/conan.cmake)
  message(STATUS "Downloading conan.cmake 0.17.0.")
  file(
    DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/0.17.0/conan.cmake"
    ${CMAKE_BINARY_DIR}/conan.cmake
    EXPECTED_HASH SHA256=3bef79da16c2e031dc429e1dac87a08b9226418b300ce004cc125a82687baeef
    STATUS status-object
    TLS_VERIFY ON)
  list(GET status-object 0 status-code)
  if(NOT status-code EQUAL 0)
    list(GET status-object 1 status-message)
    file(REMOVE ${CMAKE_BINARY_DIR}/conan.cmake)
    message(FATAL_ERROR "Could not download conan.cmake: ${status-message}")
  endif()
endif()
include(conan)
@wouterbeek
Copy link
Author

Ah, this is already being worked on in pull request #374 .

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

No branches or pull requests

1 participant