# CMAKE\_MSVCIDE\_RUN\_PATH

**URL:** <https://discourse.cmake.org/t/cmake-msvcide-run-path/3617>\
**Category:** Code\
**Tags:** os:windows, gen:vs\
**Created:** [June 24, 2021, 10:18am UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617 "2021-06-24T10:18:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maarten\_Braakhekke](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maarten_braakhekke/32/1434_2.png) [@Maarten\_Braakhekke](https://discourse.cmake.org/u/Maarten_Braakhekke)\
**Post date:** [June 24, 2021, 10:18am UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/1 "2021-06-24T10:18:20Z")

</div>

My project has a custom target that prepares some files that are later used in a test. The command of the custom target uses a tool that requires some shared libraries to be on the path. I don’t want to force my users to add it to the system path so I looked for a way to add it just for the custom target. My system is Visual Studio 2019 on Windows.  
According to the first answer in [this SO post](https://stackoverflow.com/a/28533013/6610407) the variable [CMAKE\_MSVCIDE\_RUN\_PATH](https://cmake.org/cmake/help/latest/envvar/CMAKE_MSVCIDE_RUN_PATH.html#cmake-msvcide-run-path) is the tool for this. It should add a line `set PATH=<path/to/add>;%PATH%` to the custom build step in VS. However it doesn’t seem to work for me–the line is not present. I could add the line manually but since there are many such custom targets, this would be a hassle and would clutter up the code because I’d need to add `if (MSVC)` clauses.  
I prepared a minimal example. It’d be easier to add it as an attachment but since I’m new user that’s not possible so I paste the code below.

`CMakeLists.txt`:

```auto
project(msvcide_run_path_example LANGUAGES NONE)
cmake_minimum_required(VERSION 3.20)
enable_testing()
cmake_path(CONVERT "${CMAKE_SOURCE_DIR}/test_prep" TO_NATIVE_PATH_LIST test_prep_path)
set(CMAKE_MSVCIDE_RUN_PATH ${test_prep_path}) # this doesn't work
add_custom_target (test_prep 
	#COMMAND set "PATH=${test_prep_path};%PATH%" # uncomment this to make it work
	COMMAND "test_prep.bat")
add_test(
  NAME "my_test"
  COMMAND "FC.exe" "test_file1.txt" "test_file2.txt"  
)

```

The tool to prepare the files (`test_prep.bat`) is located in subdirectory `test_prep` which needs to be added to the path. The contents of the batch file is as follows:

```auto
echo test > test_file1.txt
echo test > test_file2.txt

```

---

<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 24, 2021, 12:52pm UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/2 "2021-06-24T12:52:56Z")

</div>

Why not pass the full path to `test_prep.bat` to `COMMAND`? Wouldn’t that let the `.` entry in `%PATH%` do what you want?

---

<div class="post-metadata">

**Author:** ![Maarten\_Braakhekke](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maarten_braakhekke/32/1434_2.png) [@Maarten\_Braakhekke](https://discourse.cmake.org/u/Maarten_Braakhekke)\
**Post date:** [June 24, 2021, 4:06pm UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/3 "2021-06-24T16:06:15Z")

</div>

The example was just to demonstrate that the CMAKE\_MSVCIDE\_RUN\_PATH variable doesn’t work here. In my real project adding the dir to the path is necessary because of a shared library. In other words `test_prep.bat` is in reality a binary that loads a shared library.

---

<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 24, 2021, 4:35pm UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/4 "2021-06-24T16:35:48Z")

</div>

Hmm. I don’t seem to see a test for this variable. @brad.king?

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [June 25, 2021, 11:39am UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/5 "2021-06-25T11:39:38Z")

</div>

The implementation is [here](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.20.5/Source/cmLocalVisualStudioGenerator.cxx#L172-183). It adds `set PATH=...` to the generated custom command script inside the `.vxcproj` file. It has nothing to do with `add_test` or ctest.

---

<div class="post-metadata">

**Author:** ![Maarten\_Braakhekke](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maarten_braakhekke/32/1434_2.png) [@Maarten\_Braakhekke](https://discourse.cmake.org/u/Maarten_Braakhekke)\
**Post date:** [June 25, 2021, 12:08pm UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/6 "2021-06-25T12:08:28Z")

</div>

Are you saying that the fact that CMAKE\_MSVCIDE\_RUN\_PATH doesn’t work in my example is expected/correct behavior? That’s not what I would expect based on the [docs](https://cmake.org/cmake/help/latest/variable/CMAKE_MSVCIDE_RUN_PATH.html#cmake-msvcide-run-path).

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [June 25, 2021, 12:19pm UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/7 "2021-06-25T12:19:56Z")

</div>

The docs you linked say "when executing `add_custom_command()` or `add_custom_target()` ". That has nothing to do with `add_test()`.

---

<div class="post-metadata">

**Author:** ![Maarten\_Braakhekke](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maarten_braakhekke/32/1434_2.png) [@Maarten\_Braakhekke](https://discourse.cmake.org/u/Maarten_Braakhekke)\
**Post date:** [June 25, 2021, 12:24pm UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/8 "2021-06-25T12:24:41Z")

</div>

But my [code](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617) _does_ use `add_custom_target()` and that’s where I want to add the path. The custom target prepares files later used in a test but that’s irrelevant I would say.

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [June 25, 2021, 12:38pm UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/9 "2021-06-25T12:38:46Z")

</div>

Sorry, I misread the example.

The logic I linked is conditioned on [`MSVC_IDE`](https://cmake.org/cmake/help/v3.21/variable/MSVC_IDE.html) being set, but that is only set [here](https://gitlab.kitware.com/cmake/cmake/-/blob/v3.20.5/Modules/Platform/Windows-MSVC.cmake#L45) when a language is enabled. Adding `set(MSVC_IDE 1)` can work around the problem.

I’ve opened [CMake Issue 22343](https://gitlab.kitware.com/cmake/cmake/-/issues/22343) for this.

---

<div class="post-metadata">

**Author:** ![Maarten\_Braakhekke](https://discourse.cmake.org/user_avatar/discourse.cmake.org/maarten_braakhekke/32/1434_2.png) [@Maarten\_Braakhekke](https://discourse.cmake.org/u/Maarten_Braakhekke)\
**Post date:** [June 25, 2021, 12:54pm UTC](https://discourse.cmake.org/t/cmake-msvcide-run-path/3617/10 "2021-06-25T12:54:19Z")

</div>

OK thanks, also for the workaround!
