# CPack External generator runs CPACK\_EXTERNAL\_PACKAGE\_SCRIPT before creating JSON metadata

**URL:** https://discourse.cmake.org/t/cpack-external-generator-runs-cpack-external-package-script-before-creating-json-metadata/1773
**Category:** Usage
**Created:** [September 2, 2020, 3:04pm UTC](https://discourse.cmake.org/t/cpack-external-generator-runs-cpack-external-package-script-before-creating-json-metadata/1773 "2020-09-02T15:04:39Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![cheesy\_falafel](https://discourse.cmake.org/user_avatar/discourse.cmake.org/cheesy_falafel/32/809_2.png) [@cheesy\_falafel](https://discourse.cmake.org/u/cheesy_falafel)
#### Post date: [September 2, 2020, 3:04pm UTC](https://discourse.cmake.org/t/cpack-external-generator-runs-cpack-external-package-script-before-creating-json-metadata/1773/1 "2020-09-02T15:04:40Z")

</div>

Hi.

I’m having some trouble understanding how the CPack external generator is supposed to work. I’m running CPack via the CMake “package” target. In CMake I’m setting `CPACK_EXTERNAL_PACKAGE_SCRIPT` to point to a CMake script that invokes an external tool using `execute_process`. I want the external process to read the metadata from the CPack generated JSON file and use it to package my library. However, when I run a clean build, the external script runs before the JSON metadata is created causing my packaging step to fail. Is there a way to instruct CPack to generate the JSON metadata before my external script is executed?

Thanks.

---

<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: [November 15, 2020, 2:45pm UTC](https://discourse.cmake.org/t/cpack-external-generator-runs-cpack-external-package-script-before-creating-json-metadata/1773/2 "2020-11-15T14:45:34Z")

</div>

It looks like the `package.json` is written out before the script is executed. Could you get a trace of why the `package.json` is disappearing perhaps?

---

<div class="post-metadata">

### Author: ![t1-nva](https://discourse.cmake.org/user_avatar/discourse.cmake.org/t1-nva/32/4722_2.png) [@t1-nva](https://discourse.cmake.org/u/t1-nva)
#### Post date: [July 4, 2024, 9:58am UTC](https://discourse.cmake.org/t/cpack-external-generator-runs-cpack-external-package-script-before-creating-json-metadata/1773/3 "2024-07-04T09:58:15Z")

</div>

A long time has passed since the OP, but I’m seeing the same problem, when running the `CPACK_EXTERNAL_PACKAGE_SCRIPT` the JSON metadata file is present, but with partial contents. Running the same script later/manually works without problems i.e. after cpack has finished. I also tried just copying the JSON metadata file to another location at that point and it has partial content at that time.

Could there be some race condition/file flush issue making the script run before the JSON metadata file is fully completed and/or flushed to disk?

---

<div class="post-metadata">

### Author: ![Zitrax](https://discourse.cmake.org/user_avatar/discourse.cmake.org/zitrax/32/4776_2.png) [@Zitrax](https://discourse.cmake.org/u/Zitrax)
#### Post date: [July 18, 2024, 8:56am UTC](https://discourse.cmake.org/t/cpack-external-generator-runs-cpack-external-package-script-before-creating-json-metadata/1773/4 "2024-07-18T08:56:11Z")

</div>

We see the same issue here. @ben.boeckel , it’s not that the file does not exist, it does but as @t1-nva mentions it is not complete, like it has not been fully flushed.

In our case it just ends like so:

```auto
   ⋮

    "tests" : 
    {
      "archiveFile" : "",
      "dependencies" : [],
      "description" : "",
      "displayName" : "Unit Tests",
      "group" : "Development",
      "installationTypes" : [],
      "isDisabledByDefault" : true,
      "isDownloaded" : false,
      "isHidden" : true,
      "isRequired" : false,
      "name" : "tests"
    }
  },
  "errorOnAbsoluteInstallDes

```

so it ends right in the middle of a word and is thus not even valid json.

---

<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: [July 18, 2024, 11:53am UTC](https://discourse.cmake.org/t/cpack-external-generator-runs-cpack-external-package-script-before-creating-json-metadata/1773/5 "2024-07-18T11:53:24Z")

</div>

Ah, that makes the problem a lot clearer, thanks for the extra details. Can you test this patch: [https://gitlab.kitware.com/cmake/cmake/-/merge\_requests/9669](https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9669)

---

<div class="post-metadata">

### Author: ![t1-nva](https://discourse.cmake.org/user_avatar/discourse.cmake.org/t1-nva/32/4722_2.png) [@t1-nva](https://discourse.cmake.org/u/t1-nva)
#### Post date: [August 30, 2024, 8:21pm UTC](https://discourse.cmake.org/t/cpack-external-generator-runs-cpack-external-package-script-before-creating-json-metadata/1773/6 "2024-08-30T20:21:54Z")

</div>

In CMake 3.30.2 this issue has been fixed and `CMAKE_EXTERNAL_PACKAGE_SCRIPT` is working as expected!

Thanks a lot!
