# How to deal with failing file(MAKE\_DIRECTORY

**URL:** https://discourse.cmake.org/t/how-to-deal-with-failing-file-make-directory/10953
**Category:** Code
**Created:** [June 5, 2024, 4:03pm UTC](https://discourse.cmake.org/t/how-to-deal-with-failing-file-make-directory/10953 "2024-06-05T16:03:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![oweissbarth\_syk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/oweissbarth_syk/32/4644_2.png) [@oweissbarth\_syk](https://discourse.cmake.org/u/oweissbarth_syk)
#### Post date: [June 5, 2024, 4:03pm UTC](https://discourse.cmake.org/t/how-to-deal-with-failing-file-make-directory/10953/1 "2024-06-05T16:03:08Z")

</div>

Hi,  
I am trying to create a directory using file(MAKE\_DIRECTORY …).  
I want to handle the failure case: abort the configuration and print a custom error message.

The call to file(MAKE\_DIRECTORY …) is somewhere deep in a function call. When it fails it prints its own error message, the execution of the function stops and unwinds all the way to the top-level CMakeLists.txt. From there the execution continues with the next instruction.

This means I cannot handle this error locally but only at the top level. Am i missing something?

A condensed example looks like this:

```auto
cmake_minimum_required(VERSION 3.29)

project(demo)

function(this_function_fails)
  file(MAKE_DIRECTORY "/root/test")
  message(STATUS "This line will never be reached")
  if(NOT EXISTS "/root/test")
    message(FATAL_ERROR "Failed to create directory /root/test")
  endif()
endfunction()

this_function_fails()
# Error handling can only happen here?
message(STATUS "The next line that will be executed")

```

The output of running the script is as follows:

```auto
❯ cmake .          
-- The C compiler identification is GNU 12.3.0
-- The CXX compiler identification is GNU 12.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at CMakeLists.txt:6 (file):
  file failed to create directory:

    /root/test

  because: Permission denied
Call Stack (most recent call first):
  CMakeLists.txt:13 (this_function_fails)

-- The next line that will be executed.
-- Configuring incomplete, errors occurred!

```

Thanks in advance for your help.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [June 5, 2024, 8:52pm UTC](https://discourse.cmake.org/t/how-to-deal-with-failing-file-make-directory/10953/2 "2024-06-05T20:52:05Z")

</div>

This is something that’s annoyed me too–I wish `file(MAKE_DIRECTORY)` had an optional `RESULT` or such that allowed handling error oneself.

This allows the behavior you want:

```cmake
cmake_minimum_required(VERSION 3.10)

project(demo LANGUAGES NONE)

function(this_function_fails dir)
  # file(MAKE_DIRECTORY ${dir})
  # message(STATUS "This line will never be reached")

  execute_process(
    COMMAND ${CMAKE_COMMAND} -E make_directory ${dir}
    RESULT_VARIABLE result
  )

  if(NOT result EQUAL 0 OR NOT IS_DIRECTORY ${dir})
    message(FATAL_ERROR "Failed to create directory ${dir}")
  endif()
endfunction()

this_function_fails("/test")

message(STATUS "The next line that will be executed")

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [June 8, 2024, 2:13am UTC](https://discourse.cmake.org/t/how-to-deal-with-failing-file-make-directory/10953/3 "2024-06-08T02:13:38Z")

</div>

> [@scivision](#):
>
> This is something that’s annoyed me too–I wish `file(MAKE_DIRECTORY)` had an optional `RESULT` or such that allowed handling error oneself.

Indeed. Please file a feature request.

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [June 10, 2024, 8:13pm UTC](https://discourse.cmake.org/t/how-to-deal-with-failing-file-make-directory/10953/4 "2024-06-10T20:13:54Z")

</div>

done in [https://gitlab.kitware.com/cmake/cmake/-/issues/26041](https://gitlab.kitware.com/cmake/cmake/-/issues/26041)
