file RENAME not behaving as expected

Hi,

the RENAME functionality of the file command is not behaving as expected. I have a small reproducer right here:

set(old_name "file.txt") 
set(new_name "new_file.txt") 

message("Pre rename:") 
execute_process(COMMAND ls) 

file(RENAME "${old_name}" "${new_name}" NO_REPLACE) 

message("\nPost rename:") 
execute_process(COMMAND ls) 

Running this via cmake -P <script name> produces the following output:

Pre rename: 
file.txt 
script.cmake 

Post rename: 
file.txt 
new_file.txt 
script.cmake 

According to the docs, the file should be moved and not copied. This problem only occurs when I pass NO_REPLACE in the arguments. If I remove that extra argument, the command behaves as expected. Am I misunderstanding the NO_REPLACE option or is this a bug?

The background for even noticing this is my usage of ExternalData. Post download, the files are “moved” with file(RENAME ... NO_REPLACE). But this also just leads to a copy, so I end up with duplicates of very large files on my filesystem.

That’s unexpected. Please open an issue in the issue tracker. Be sure to mention the platform(s) you’re seeing this on, and what CMake versions exhibit the behavior.

For reference, the NO_REPLACE option was implemented by MR 5877 and first appeared in CMake 3.21.