From d8160798747ac9953d3374f9473393868d76dfe6 Mon Sep 17 00:00:00 2001 From: Billy O'Neal Date: Wed, 6 Mar 2024 11:28:35 -0800 Subject: [PATCH] [vcpkg_from_git] Improve diagnostics. (#36539) While authoring vcpkg_from_git to authenticated servers documentation, @JavierMatosD and I discovered some failure modes that are not clear in the output. Example with REF set to a branch name: ```console The port under test is: vcpkg_from_git( OUT_SOURCE_PATH out URL https://github.com/microsoft/vcpkg REF 2024.01.12 ) ========== BEFORE =============== Computing installation plan... The following packages will be built and installed: test-port:x64-windows@1 -- D:\test\test-port Detecting compiler hash for triplet x64-windows... Installing 1/1 test-port:x64-windows@1... Building test-port:x64-windows@1... -- Installing port from location: D:\test\test-port -- Fetching https://github.com/microsoft/vcpkg 2024.01.12... CMake Error at scripts/cmake/vcpkg_from_git.cmake:141 (message): unable to rev-parse 2024.01.12 after fetching git repository Call Stack (most recent call first): D:/test/test-port/portfile.cmake:1 (vcpkg_from_git) scripts/ports.cmake:170 (include) error: building test-port:x64-windows failed with: BUILD_FAILED Elapsed time to handle test-port:x64-windows: 1.8 s Please ensure you're using the latest port files with `git pull` and `vcpkg update`. Then check for known issues at: https://github.com/microsoft/vcpkg/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+test-port You can submit a new issue at: https://github.com/microsoft/vcpkg/issues/new?title=[test-port]+Build+error+on+x64-windows&body=Copy+issue+body+from+D%3A%2Fvcpkg2%2Finstalled%2Fvcpkg%2Fissue_body.md ========== AFTER ================ Computing installation plan... The following packages will be built and installed: test-port:x64-windows@1 -- D:\test\test-port Detecting compiler hash for triplet x64-windows... Installing 1/1 test-port:x64-windows@1... Building test-port:x64-windows@1... -- Installing port from location: D:\test\test-port -- Fetching https://github.com/microsoft/vcpkg 2024.01.12... CMake Error at scripts/cmake/vcpkg_from_git.cmake:142 (message): After fetching 2024.01.12, the target ref 2024.01.12 appears inaccessible. A common cause of this failure is setting REF to a named branch or tag rather than a commit SHA. REF must be a commit SHA. If the git server does not advertise commit SHAs (uploadpack.allowReachableSHA1InWant is false), you can set FETCH_REF to a named branch in which the desired commit SHA is in the history. For example, you may be able to fix this error by changing "REF 2024.01.12" to "REF a-commit-sha FETCH_REF 2024.01.12". (git rev-parse 2024.01.12 failed) Call Stack (most recent call first): D:/test/test-port/portfile.cmake:1 (vcpkg_from_git) scripts/ports.cmake:170 (include) error: building test-port:x64-windows failed with: BUILD_FAILED Elapsed time to handle test-port:x64-windows: 1.9 s Please ensure you're using the latest port files with `git pull` and `vcpkg update`. Then check for known issues at: https://github.com/microsoft/vcpkg/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+test-port You can submit a new issue at: https://github.com/microsoft/vcpkg/issues/new?title=[test-port]+Build+error+on+x64-windows&body=Copy+issue+body+from+D%3A%2Fvcpkg%2Finstalled%2Fvcpkg%2Fissue_body.md ``` Example with REF ste to a branch name with FETCH_REF set: ```console PS D:\test> run-test The following packages are not installed: test-port:x64-windows The following packages are not installed: test-port:x64-windows The port under test is: vcpkg_from_git( OUT_SOURCE_PATH out URL https://github.com/microsoft/vcpkg REF 2024.01.12 FETCH_REF master ) ========== BEFORE =============== Computing installation plan... The following packages will be built and installed: test-port:x64-windows@1 -- D:\test\test-port Detecting compiler hash for triplet x64-windows... Installing 1/1 test-port:x64-windows@1... Building test-port:x64-windows@1... -- Installing port from location: D:\test\test-port -- Fetching https://github.com/microsoft/vcpkg master... CMake Error at scripts/cmake/vcpkg_from_git.cmake:141 (message): unable to rev-parse 2024.01.12 after fetching git repository Call Stack (most recent call first): D:/test/test-port/portfile.cmake:1 (vcpkg_from_git) scripts/ports.cmake:170 (include) error: building test-port:x64-windows failed with: BUILD_FAILED Elapsed time to handle test-port:x64-windows: 1.6 s Please ensure you're using the latest port files with `git pull` and `vcpkg update`. Then check for known issues at: https://github.com/microsoft/vcpkg/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+test-port You can submit a new issue at: https://github.com/microsoft/vcpkg/issues/new?title=[test-port]+Build+error+on+x64-windows&body=Copy+issue+body+from+D%3A%2Fvcpkg2%2Finstalled%2Fvcpkg%2Fissue_body.md ========== AFTER ================ Computing installation plan... The following packages will be built and installed: test-port:x64-windows@1 -- D:\test\test-port Detecting compiler hash for triplet x64-windows... Installing 1/1 test-port:x64-windows@1... Building test-port:x64-windows@1... -- Installing port from location: D:\test\test-port -- Fetching https://github.com/microsoft/vcpkg master... CMake Error at scripts/cmake/vcpkg_from_git.cmake:138 (message): After fetching master, the target ref 2024.01.12 appears inaccessible. A common cause of this failure is setting REF to a named branch or tag rather than a commit SHA. REF must be a commit SHA. (git rev-parse 2024.01.12 failed) Call Stack (most recent call first): D:/test/test-port/portfile.cmake:1 (vcpkg_from_git) scripts/ports.cmake:170 (include) error: building test-port:x64-windows failed with: BUILD_FAILED Elapsed time to handle test-port:x64-windows: 1.5 s Please ensure you're using the latest port files with `git pull` and `vcpkg update`. Then check for known issues at: https://github.com/microsoft/vcpkg/issues?q=is%3Aissue+is%3Aopen+in%3Atitle+test-port You can submit a new issue at: https://github.com/microsoft/vcpkg/issues/new?title=[test-port]+Build+error+on+x64-windows&body=Copy+issue+body+from+D%3A%2Fvcpkg%2Finstalled%2Fvcpkg%2Fissue_body.md ``` --------- Co-authored-by: Thomas1664 <46387399+Thomas1664@users.noreply.github.com> --- scripts/cmake/vcpkg_from_git.cmake | 74 +++++++++++-------- .../vcpkg-from-git-test/portfile.cmake | 48 ++++++++++-- 2 files changed, 84 insertions(+), 38 deletions(-) diff --git a/scripts/cmake/vcpkg_from_git.cmake b/scripts/cmake/vcpkg_from_git.cmake index c77e63e1dc..0b477acb82 100644 --- a/scripts/cmake/vcpkg_from_git.cmake +++ b/scripts/cmake/vcpkg_from_git.cmake @@ -104,49 +104,63 @@ function(vcpkg_from_git) vcpkg_execute_required_process( ALLOW_IN_DOWNLOAD_MODE - COMMAND ${GIT} lfs install --local --force + COMMAND "${GIT}" lfs install --local --force WORKING_DIRECTORY "${git_working_directory}" LOGNAME "git-lfs-install-${TARGET_TRIPLET}" ) vcpkg_execute_required_process( ALLOW_IN_DOWNLOAD_MODE - COMMAND ${GIT} lfs fetch "${arg_LFS}" "${ref_to_fetch}" + COMMAND "${GIT}" lfs fetch "${arg_LFS}" "${ref_to_fetch}" WORKING_DIRECTORY "${git_working_directory}" LOGNAME "git-lfs-fetch-${TARGET_TRIPLET}" ) endif() if(VCPKG_USE_HEAD_VERSION) - vcpkg_execute_in_download_mode( - COMMAND "${GIT}" rev-parse FETCH_HEAD - OUTPUT_VARIABLE rev_parse_ref - ERROR_VARIABLE rev_parse_ref - RESULT_VARIABLE error_code - WORKING_DIRECTORY "${git_working_directory}" - ) - if(error_code) - message(FATAL_ERROR "unable to determine FETCH_HEAD after fetching git repository") - endif() - string(STRIP "${rev_parse_ref}" rev_parse_ref) - set(VCPKG_HEAD_VERSION "${rev_parse_ref}" PARENT_SCOPE) + set(expected_rev_parse FETCH_HEAD) else() - vcpkg_execute_in_download_mode( - COMMAND "${GIT}" rev-parse "${arg_REF}" - OUTPUT_VARIABLE rev_parse_ref - ERROR_VARIABLE rev_parse_ref - RESULT_VARIABLE error_code - WORKING_DIRECTORY "${git_working_directory}" + set(expected_rev_parse "${arg_REF}") + endif() + + vcpkg_execute_in_download_mode( + COMMAND "${GIT}" rev-parse "${expected_rev_parse}" + OUTPUT_VARIABLE rev_parse_ref + ERROR_VARIABLE rev_parse_ref + RESULT_VARIABLE error_code + WORKING_DIRECTORY "${git_working_directory}" + ) + + if(error_code) + if(VCPKG_USE_HEAD_VERSION) + message(FATAL_ERROR "Unable to determine the commit SHA of the HEAD version to use after \ +fetching ${ref_to_fetch} from the git repository. (git rev-parse ${expected_rev_parse} failed)") + elseif(DEFINED arg_FETCH_REF) + message(FATAL_ERROR "After fetching ${ref_to_fetch}, the target ref ${expected_rev_parse} appears \ +inaccessible. A common cause of this failure is setting REF to a named branch or tag rather than a commit SHA. REF \ +must be a commit SHA. (git rev-parse ${expected_rev_parse} failed)") + else() + message(FATAL_ERROR "After fetching ${ref_to_fetch}, the target ref ${expected_rev_parse} appears \ +inaccessible. A common cause of this failure is setting REF to a named branch or tag rather than a commit SHA. REF \ +must be a commit SHA. If the git server does not advertise commit SHAs \ +(uploadpack.allowReachableSHA1InWant is false), you can set FETCH_REF to a named branch in which the desired commit \ +SHA is in the history. For example, you may be able to fix this error by changing \"REF ${arg_REF}\" to \ +\"REF a-commit-sha FETCH_REF ${arg_REF}\". (git rev-parse ${expected_rev_parse} failed)") + endif() + endif() + + string(STRIP "${rev_parse_ref}" rev_parse_ref) + if(VCPKG_USE_HEAD_VERSION) + set(VCPKG_HEAD_VERSION "${rev_parse_ref}" PARENT_SCOPE) + elseif(NOT "${rev_parse_ref}" STREQUAL "${arg_REF}") + message(FATAL_ERROR "After fetching ${ref_to_fetch}, the requested REF (${arg_REF}) does not match \ +its commit SHA returned by git rev-parse (${rev_parse_ref}). This is usually caused by trying to set REF to a named \ +branch or tag rather than a commit SHA. REF must be a commit SHA. If the git server does not advertise commit SHAs \ +(uploadpack.allowReachableSHA1InWant is false), you can set FETCH_REF to a named branch in which the desired commit \ +SHA is in the history. For example, you may be able to fix this error by changing \"REF ${arg_REF}\" to \ +\"REF a-commit-sha FETCH_REF ${arg_REF}\". + [Expected : ( ${arg_REF} )]) + [ Actual : ( ${rev_parse_ref} )]" ) - if(error_code) - message(FATAL_ERROR "unable to rev-parse ${arg_REF} after fetching git repository") - endif() - string(STRIP "${rev_parse_ref}" rev_parse_ref) - if(NOT "${rev_parse_ref}" STREQUAL "${arg_REF}") - message(FATAL_ERROR "REF (${arg_REF}) does not match rev-parse'd reference (${rev_parse_ref}) - [Expected : ( ${arg_REF} )]) - [ Actual : ( ${rev_parse_ref} )]" - ) - endif() endif() file(MAKE_DIRECTORY "${DOWNLOADS}/temp") diff --git a/scripts/test_ports/vcpkg-from-git-test/portfile.cmake b/scripts/test_ports/vcpkg-from-git-test/portfile.cmake index e2c6bd8866..841a6e2208 100644 --- a/scripts/test_ports/vcpkg-from-git-test/portfile.cmake +++ b/scripts/test_ports/vcpkg-from-git-test/portfile.cmake @@ -6,6 +6,7 @@ file(REMOVE_RECURSE "${git_test_repo}") # LFS expects a URL for a local repository set(git_remote "file:///${git_test_repo}") +message(STATUS "Creating test git repository") vcpkg_find_acquire_program(GIT) vcpkg_list(SET git_config -c core.autocrlf=false @@ -75,7 +76,7 @@ if(NOT "${error_code}" EQUAL "0") endif() string(STRIP "${head_ref}" head_ref) -# test regular mode +message(STATUS "Testing regular mode") set(VCPKG_USE_HEAD_VERSION OFF) vcpkg_from_git( OUT_SOURCE_PATH source_path @@ -90,7 +91,22 @@ ${contents} ") endif() -# test regular mode with FETCH_REF +message(STATUS "Testing regular mode that happens to match HEAD") +set(VCPKG_USE_HEAD_VERSION OFF) +vcpkg_from_git( + OUT_SOURCE_PATH source_path + URL "${git_remote}" + REF "${head_ref}" + HEAD_REF main +) +file(READ "${source_path}/README.txt" contents) +if(NOT "${contents}" STREQUAL "second commit") + message(FATAL_ERROR "Failed to checkout the second commit. Contents were: +${contents} +") +endif() + +message(STATUS "Testing regular mode with FETCH_REF") vcpkg_execute_required_process( COMMAND ${git} config uploadpack.allowReachableSHA1InWant false WORKING_DIRECTORY "${git_test_repo}" @@ -111,13 +127,29 @@ ${contents} ") endif() +message(STATUS "Testing regular mode with FETCH_REF that happens to match HEAD") +set(VCPKG_USE_HEAD_VERSION OFF) +vcpkg_from_git( + OUT_SOURCE_PATH source_path + URL "${git_remote}" + REF "${head_ref}" + FETCH_REF main + HEAD_REF main +) +file(READ "${source_path}/README.txt" contents) +if(NOT "${contents}" STREQUAL "second commit") + message(FATAL_ERROR "Failed to checkout the second commit. Contents were: +${contents} +") +endif() + vcpkg_execute_required_process( COMMAND ${git} config uploadpack.allowReachableSHA1InWant true WORKING_DIRECTORY "${git_test_repo}" LOGNAME "git-config" ) -# test head mode +message(STATUS "Testing head mode") set(VCPKG_USE_HEAD_VERSION ON) vcpkg_from_git( OUT_SOURCE_PATH source_path @@ -138,7 +170,7 @@ if(NOT "${VCPKG_HEAD_VERSION}" STREQUAL "${head_ref}") ") endif() -# test head mode + no HEAD_REF -> just uses REF +message(STATUS "Testing head mode + no HEAD_REF -> just uses REF") set(VCPKG_USE_HEAD_VERSION ON) vcpkg_from_git( OUT_SOURCE_PATH source_path @@ -152,7 +184,7 @@ ${contents} ") endif() -# test new head ref +message(STATUS "Testing new head ref") file(WRITE "${git_test_repo}/README.txt" "third commit") vcpkg_execute_required_process( COMMAND ${git} add "README.txt" @@ -195,7 +227,7 @@ if(NOT "${VCPKG_HEAD_VERSION}" STREQUAL "${new_head_ref}") ") endif() -# test LFS support +message(STATUS "Testing LFS support") vcpkg_execute_in_download_mode( COMMAND "${GIT}" lfs --version OUTPUT_VARIABLE lfs_version_output @@ -211,7 +243,7 @@ if(NOT lfs_version_result) ) file(WRITE "${git_test_repo}/.gitattributes" "* text=auto\n*.bin filter=lfs diff=lfs merge=lfs -text\n") - # test fetching with the same Git and LFS urls + message(STATUS "Testing fetching with the same Git and LFS urls") file(WRITE "${git_test_repo}/lfs_file.bin" "fourth commit") vcpkg_execute_required_process( COMMAND ${git} add ".gitattributes" "lfs_file.bin" @@ -263,7 +295,7 @@ ${contents} ") endif() - # test fetching from different Git and LFS urls + message(STATUS "Testing fetching from different Git and LFS urls") # requires LFS 3.0.0 or later for "--force" on prune string(REGEX MATCH "git-lfs/([0-9\\.]+) " lfs_version "${lfs_version_output}") set(lfs_version "${CMAKE_MATCH_1}")