Check if a project exists before collecting referenced/referencing projects

Fix #1001752