# Error using FetchContent with URL

**URL:** https://discourse.cmake.org/t/error-using-fetchcontent-with-url/10788
**Category:** Usage
**Tags:** os:linux
**Created:** [May 3, 2024, 12:33am UTC](https://discourse.cmake.org/t/error-using-fetchcontent-with-url/10788 "2024-05-03T00:33:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [May 3, 2024, 12:33am UTC](https://discourse.cmake.org/t/error-using-fetchcontent-with-url/10788/1 "2024-05-03T00:33:01Z")

</div>

Hello,

I’m trying to download a binary dependency and put it in the same location as my source build (this is due to some legacy nonsense involving environment variables).

I do the following:

```auto
FetchContent_Declare(MyDependency
    URL my/dependency/resource.tar.gz
    HTTP_HEADER "PRIVATE-TOKEN: <private-token>" -L
    SOURCE_DIR ${CMAKE_BINARY_DIR}
)

FetchContent_MakeAvailable(MyDependency)

```

When I configure, I get the following after downloading the resource:

```auto
-- extracting... done
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
chdir: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
cmake -E touch: failed to update "/path/to/project/build/_deps/myproject_subbuild/myproject-populate-prefix/src/myproject-populate-stamp/myproject-populate-download".

```

This is followed by some CMake errors about failing to direct populate, the most recent call being:

`/path/to/cmake/Modules/FetchContent.cmake:1819:EVAL:2 (__FetchContent_directPopulate)`

Removing the `SOURCE_DIR` line lets me download and configure in the expected way (except that my artifacts are now located in `.../build/_deps/myproject-src/` which is a problem for the legacy nonsense involving environment variables).

Am I missing something else I need in the `FetchContent` invocation, or is this just not possible without a script or custom command or something?

Thanks in advance

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 3, 2024, 10:42pm UTC](https://discourse.cmake.org/t/error-using-fetchcontent-with-url/10788/2 "2024-05-03T22:42:35Z")

</div>

Do not use `${CMAKE_BINARY_DIR}` as the `SOURCE_DIR`. The `${CMAKE_BINARY_DIRECTORY}` is already something CMake populates with various things, and trying to make it your dependency’s source directory will cause corruption. You can specify a _subdirectory_ below `${CMAKE_BINARY_DIRECTORY}` though (and I do this in some projects). For example:

```cmake
FetchContent_Declare(MyDependency
    URL my/dependency/resource.tar.gz
    HTTP_HEADER "PRIVATE-TOKEN: <private-token>" -L
    SOURCE_DIR ${CMAKE_BINARY_DIR}/MyDependency
)

```

I’m also wondering about the `-L` at the end of the `HTTP_HEADER` line. That looks suspicious, it isn’t a HTTP header and looks more like a raw curl command line option, but CMake uses internal function calls directly in the C/C++ layer for its networking.

---

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [May 6, 2024, 8:06pm UTC](https://discourse.cmake.org/t/error-using-fetchcontent-with-url/10788/3 "2024-05-06T20:06:06Z")

</div>

Hi Craig, thanks for the response.

My issue is mostly that pre-existing legacy systems require that these artifacts be located in the same directory, not a subdirectory or a parallel one. The old systems use python scripts to fetch dependencies, and I’m trying to unify everything in CMake. This only has to be done after the build is complete (at application runtime). Would you recommend a custom command to do this after the build is complete? I suppose I could create symlinks to the directory as well.

Regarding the `-L`, my code above works but definitely does _not_ work without the `-L`. 🤷

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [May 6, 2024, 10:07pm UTC](https://discourse.cmake.org/t/error-using-fetchcontent-with-url/10788/4 "2024-05-06T22:07:12Z")

</div>

Do you need the sources or the built binaries to all be located in the same directory? Those are two very different scenarios.

> Regarding the `-L` , my code above works but definitely does _not_ work without the `-L` .

I assume you’re using that to convince the implementation to follow redirections. I’d have to crawl through the implementation to find out if there’s an officially supported way to achieve that. I’m kinda surprised this works though, I would have expected it to cause an error (it _should_ cause an error, since `-L` is not a valid HTTP header). Take this as a warning that using `-L` might stop working at any time with a future update to CMake.

---

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [May 7, 2024, 12:39am UTC](https://discourse.cmake.org/t/error-using-fetchcontent-with-url/10788/5 "2024-05-07T00:39:21Z")

</div>

> Take this as a warning that using `-L` might stop working at any time with a future update to CMake.

Understood.

> Do you need the sources or the built binaries to all be located in the same directory? Those are two very different scenarios.

We need the binaries of the dependencies (prebuilt, stored in a generic gitlab package repository) to be located in the same directory as our project’s built binaries.
