Backport r781898: do create the parent directory if necessary. Seems canonicalPath returns empty for a non-existing dir.
Changed paths: