# cmake\_path(APPEND) with a variable gives unexpected result

**URL:** <https://discourse.cmake.org/t/cmake-path-append-with-a-variable-gives-unexpected-result/11002>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [June 12, 2024, 11:14am UTC](https://discourse.cmake.org/t/cmake-path-append-with-a-variable-gives-unexpected-result/11002 "2024-06-12T11:14:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johan.derek](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9f8e36/32.png) [@johan.derek](https://discourse.cmake.org/u/johan.derek)\
**Post date:** [June 12, 2024, 11:14am UTC](https://discourse.cmake.org/t/cmake-path-append-with-a-variable-gives-unexpected-result/11002/1 "2024-06-12T11:14:53Z")

</div>

I’m using CMake 3.29.0 on Ubuntu 20.04.6 LTS

The objective is to create an application configuration file that is generated with user-configurable directories in ccmake.

What I want to do is:

- Specify a project root directory, typically /var/my-project.
- Separately specify subdirectories, like a staging subdirectory, e.g. /staging/

I then want to create a CI\_CD\_STAGING\_DIR\_PATH variable by concatenating the project root directory and the staging subdirectory names. (i.e. /var/my-project/staging/)

```auto
# Initialise variables
set(THIS_EXECUTABLE_NAME delta-deployment)
set(CI_CD_PROJECT_ROOT_DIR "/var/my-project/" CACHE STRING "Root project directory")
set(CI_CD_STAGING_SUBDIR "/${THIS_EXECUTABLE_NAME}/staging/" CACHE STRING "Staging subdirectory")

# Do some path cleanup and convert to absolute path
cmake_path(SET CI_CD_PROJECT_ROOT_DIR NORMALIZE "${CI_CD_PROJECT_ROOT_DIR}/")
cmake_path(ABSOLUTE_PATH CI_CD_PROJECT_ROOT_DIR NORMALIZE)

# Concatenate root dir and subdir (this is where things go awry)
cmake_path(APPEND CI_CD_STAGING_DIR {CI_CD_PROJECT_ROOT_DIR} ${CI_CD_STAGING_SUBDIR})
message("CI_CD_PROJECT_ROOT_DIR " ${CI_CD_PROJECT_ROOT_DIR})
message("CI_CD_STAGING_DIR " ${CI_CD_STAGING_DIR})

```

What I expect to see is this:

```auto
 CI_CD_PROJECT_ROOT_DIR /var/my-project/
 CI_CD_STAGING_DIR /var/my-project/delta-deployment/staging/

```

What I **actually** see is:

```auto
 CI_CD_PROJECT_ROOT_DIR /var/my-project/
 CI_CD_STAGING_DIR /delta-deployment/staging/

```

So I do a test by replacing the cmake\_path(APPEND) with some hard-coded values

```auto
cmake_path(APPEND CI_CD_STAGING_DIR "yadda" "foo" "bar")

```

Now I see

```auto
 CI_CD_PROJECT_ROOT_DIR /var/my-project/
 CI_CD_STAGING_DIR yadda/foo/bar

```

Then I change it to

```auto
cmake_path(APPEND CI_CD_STAGING_DIR "yadda" "foo" "bar" "${CI_CD_STAGING_SUBDIR}")

```

And I see

```auto
 CI_CD_PROJECT_ROOT_DIR /var/my-project/
 CI_CD_STAGING_DIR /delta-deployment/staging/

```

To the untrained eye it seems like cmake\_path(APPEND) doesn’t work when one of the arguments is a variable; you only get the value of one variable and none of the others, including hard-coded values.

Any suggestions from the pros here?

---

<div class="post-metadata">

**Author:** ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)\
**Post date:** [June 12, 2024, 12:05pm UTC](https://discourse.cmake.org/t/cmake-path-append-with-a-variable-gives-unexpected-result/11002/2 "2024-06-12T12:05:44Z")

</div>

The observed behavior has nothing to do with the fact a variable is used of not.  
If you read carefully the documentation related to [cmake\_path(APPEND)](https://cmake.org/cmake/help/latest/command/cmake_path.html#append) you will see that the result depends on the semantics of arguments.  
In your case, the variable `CI_CD_STAGING_SUBDIR` is an **absolute** path, so this implies the discard of previous arguments!  
To get the expected results:

```cmake
# Initialise variables
set(THIS_EXECUTABLE_NAME delta-deployment)
set(CI_CD_PROJECT_ROOT_DIR "/var/my-project/" CACHE STRING "Root project directory")
set(CI_CD_STAGING_SUBDIR "${THIS_EXECUTABLE_NAME}/staging/" CACHE STRING "Staging subdirectory")

# Do some path cleanup and convert to absolute path
cmake_path(SET CI_CD_PROJECT_ROOT_DIR NORMALIZE "${CI_CD_PROJECT_ROOT_DIR}")
cmake_path(ABSOLUTE_PATH CI_CD_PROJECT_ROOT_DIR NORMALIZE)

# Concatenate root dir and subdir (this is where things go awry)
cmake_path(APPEND CI_CD_STAGING_DIR "${CI_CD_PROJECT_ROOT_DIR}" "${CI_CD_STAGING_SUBDIR}")

```
