Consider on-disk state when computing dependencies

Fixes #28754.
Fixes #29846.
Fixes #30920.