#2801·ninja

Ninja fail to build because see an inexistent dependency between a target and its alias.

Author: DPD85Created Jun 18, 2026Updated Jul 7, 2026

In the following conditions:

  • have a folder with space in the name, "foo bar";
  • have a target placed inside the folder with the name "foo bar/foo";
  • have a virtual target named "foo" that is an alias to "foo bar/foo";
  • build with gcc/g++;
  • configure gcc/g++ to create the dependency files;

Ninja will fail to build because see the inexistent dependency "foo bar/foo" -> "foo" and consequently report a dependency cycle error: "foo" -> "foo bar/foo" -> "foo".

Environment:

  • operating system: Debian 13 inside WSL 2 on Windows 11;
  • Ninja versions: 1.12.1 and 1.13.2.

How to reproduce (minimal example at my best):

  1. create a folder for the minimal example;
  2. create a folder named "foo bar" inside the first;
  3. create the file empty.c inside "foo bar" folder with this content: int main(int, char**) { return 0; };
  4. create the build.ninja file inside the root folder with the following content:
rule cc
  depfile = $DEP_FILE
  deps = gcc
  command = gcc -MD -MF $DEP_FILE -c $in -o $out
rule ll
  depfile = $DEP_FILE
  deps = gcc
  command = gcc -Wl,--dependency-file="foo bar/foo.d" $in -o $out

build foo$ bar/empty.o: cc foo$ bar/empty.c
  DEP_FILE = "foo bar/empty.d"
  depfile = foo bar/empty.d

build foo$ bar/foo: ll foo$ bar/empty.o
  DEP_FILE = "foo bar/foo.d"
  depfile = foo bar/foo.d

build foo: phony foo$ bar/foo

default foo
  1. execute ninja the first time as usual: ninja;
  2. execute ninja the second time, here ninja give the error: "dependency cycle: foo -> foo bar/foo -> foo".

Output of "ninja -t deps":

foo bar/empty.o: #deps 2, deps mtime 1781795086170202466 (STALE)
    foo bar/empty.c
    /usr/include/stdc-predef.h

foo bar/foo: #deps 14, deps mtime 1781795086461931133 (STALE)
    /usr/lib/x86_64-linux-gnu/Scrt1.o
    /usr/lib/x86_64-linux-gnu/crti.o
    /usr/lib/gcc/x86_64-linux-gnu/14/crtbeginS.o
    foo
    bar/empty.o
    /usr/lib/gcc/x86_64-linux-gnu/14/libgcc.a
    /usr/lib/gcc/x86_64-linux-gnu/14/libgcc_s.so
    /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
    /usr/lib/x86_64-linux-gnu/libc.so
    /lib/x86_64-linux-gnu/libc.so.6
    /usr/lib/x86_64-linux-gnu/libc_nonshared.a
    /lib64/ld-linux-x86-64.so.2
    /usr/lib/gcc/x86_64-linux-gnu/14/crtendS.o
    /usr/lib/x86_64-linux-gnu/crtn.o