only chdir if necessary