MantisBT - CMake
View Issue Details
0014928CMakeCMakepublic2014-05-22 15:012016-02-16 13:11
dirk-thomas 
 
normalminoralways
closedfixed 
LinuxUbuntu14.04
CMake 2.8.12.2 
CMake 3.0 
0014928: cmake -E create_symlink always returns code 0 even when failing to create symlink
When invoking the command "cmake -E create_symlink old new" the return code is always 0 even when the command failed to create the symlink.
Invoke "cmake ." with the following CMakeLists.txt file in the same folder to reproduce:

# create a real file
set(file "${CMAKE_CURRENT_SOURCE_DIR}/test")
file(WRITE "${file}" "foo")
if(EXISTS "${file}" AND NOT IS_SYMLINK "${file}")
  message(STATUS "OK: it is a file, not a symlink")
else()
  message(FATAL_ERROR "ups, this hould not happen")
endif()

# try to create a symlink
execute_process(
  COMMAND "${CMAKE_COMMAND}" "-E" "create_symlink" "/tmp/somewhere" "${file}"
  RESULT_VARIABLE rc
  OUTPUT_VARIABLE output
  ERROR_VARIABLE error
)
message(STATUS "RESULT_VARIABLE ${rc}")
message(STATUS "OUTPUT_VARIABLE ${output}")
message(STATUS "ERROR_VARIABLE ${error}")

if(EXISTS "${file}" AND IS_SYMLINK "${file}")
  message(STATUS "WEIRD: it is a symlink, not a file, has overwritten the existing file")
elseif(NOT rc EQUAL 0)
  message(STATUS "OK: could not create symlink and returned error code")
else()
  message(FATAL_ERROR "ups, this hould not happen, not creating the symlink but returning code 0 pretending it was successful")
endif()
I currently manually have to check that neither a symlink nor file is present before invoking the command and check afterwards that the symlink is present.
No tags attached.
related to 0014713closed Brad King cmake -create-symlink doesn't overwrite existing dangling(!) symlinks 
Issue History
2014-05-22 15:01dirk-thomasNew Issue
2014-05-23 08:52Brad KingNote Added: 0035919
2014-05-23 12:37dirk-thomasNote Added: 0035923
2014-05-23 12:37dirk-thomasNote Edited: 0035923bug_revision_view_page.php?bugnote_id=35923#r1461
2014-05-23 13:33Brad KingNote Added: 0035924
2014-05-23 13:34Brad KingNote Added: 0035925
2014-05-23 13:37dirk-thomasNote Added: 0035926
2014-05-23 13:39Brad KingRelationship addedrelated to 0014713
2014-05-23 13:39Brad KingStatusnew => resolved
2014-05-23 13:39Brad KingResolutionopen => fixed
2014-05-23 13:39Brad KingFixed in Version => CMake 3.0
2014-11-03 08:38Robert MaynardNote Added: 0037141
2014-11-03 08:38Robert MaynardStatusresolved => closed

Notes
(0035919)
Brad King   
2014-05-23 08:52   
Look at the actual directory content with "ls" after running your example. A symlink called "test" exists and points to /tmp/somewhere. Creation of the symlink worked.

The problem in your example is that if(EXISTS) returns FALSE for a broken symlink. See 0014022.
(0035923)
dirk-thomas   
2014-05-23 12:37   
You seem to get a different result then me. After I run it there is no symlink but still the dummy file with the content "foo". I added the messages in CMake only to illustrate the actual and expected behavior.

The same happens for me even when I remove the "EXISTS" check you refer to:

# create a real file
set(file "${CMAKE_CURRENT_SOURCE_DIR}/test")
file(WRITE "${file}" "foo")
if(EXISTS "${file}" AND NOT IS_SYMLINK "${file}")
  message(STATUS "OK: it is a file, not a symlink")
else()
  message(FATAL_ERROR "ups, this hould not happen")
endif()

# try to create a symlink
execute_process(
  COMMAND "${CMAKE_COMMAND}" "-E" "create_symlink" "/tmp/somewhere" "${file}"
  RESULT_VARIABLE rc
  OUTPUT_VARIABLE output
  ERROR_VARIABLE error
)
message(STATUS "RESULT_VARIABLE ${rc}")
message(STATUS "OUTPUT_VARIABLE ${output}")
message(STATUS "ERROR_VARIABLE ${error}")

if(IS_SYMLINK "${file}")
  message(STATUS "WEIRD: it is a symlink, not a file, has overwritten the existing file")
elseif(NOT rc EQUAL 0)
  message(STATUS "OK: could not create symlink and returned error code")
else()
  message(FATAL_ERROR "ups, this hould not happen, not creating the symlink but returning code 0 pretending it was successful")
endif()

(0035924)
Brad King   
2014-05-23 13:33   
Behavior has changed since 2.8.12.2. See 0014713 and this commit for it:

 cmake: Improve '-E create_symlink' edge case handling
 http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=e0228e2b [^]
(0035925)
Brad King   
2014-05-23 13:34   
You can try a CMake 3.0 release candidate here:

 http://www.cmake.org/files/v3.0/?C=M;O=D [^]
(0035926)
dirk-thomas   
2014-05-23 13:37   
I guess the ticket can be closed then and marked with "fixed in 3.0". Thanks!
(0037141)
Robert Maynard   
2014-11-03 08:38   
Closing resolved issues that have not been updated in more than 4 months.