$dir can still be undefined if the code passes through line 22