add_custom_command rebuilds every time

Dear CMake forums folks,

I run into issues with add_custom_command. I’d kindly appreciate if you give me a direction.

The issue is that the target rebuilds every time, regardless of dependencies.

Expected behavior, the target rebuilds only if dependency files were modified

# TODO: consider fetching list of outputs from ExpectedDistContents.json instead of hardcoding
  OUTPUT ${out}
  COMMAND yarn --cwd "\"${CMAKE_CURRENT_LIST_DIR}\"" build
  DEPENDS ${sources}
add_custom_target(skymp5-front ALL
  DEPENDS ${out}
  SOURCES ${sources}

Try to use Ninja with -d explain, so it should tell you why it’s rebuilding.

I guess you get a rebuild at least every time CMake was called.
You glob the source directory (which is usually discouraged) and you write that config.js to the list dir (which is the same as source dir).
Using file(WRITE) and file(APPEND) do write the file every time CMake is executed.
You should use file(CONFIGURE) or file(GENERATE) instead, which write the file at generation time only if the content differs.

Ignore my previous reply; I completely missed the DEPENDS sitting right in front of my face.