ts-morph
ts-morph copied to clipboard
Calling `addSourceFileAtPathIfExists` has a side effect of internally remembering directories that don't exist which causes subsequent operations to fail
Description
Version: 23.0.0
If you call addSourceFileAtPathIfExists with a path where some of the directories in that path do not exist, trying to add those directories later via addDirectoryAtPath or addDirectoryAtPathIfExists will fail with an error.
How to reproduce
This only occurs if you have { recursive: true } so it has something to do with the recursive logic.
import { Project } from "ts-morph";
const project = new Project();
project.addDirectoryAtPathIfExists('/some/path/that/doesnt/exist', { recursive: true })
project.addSourceFileAtPathIfExists('/some/path/that/doesnt/exist/file.ts')
// This call will throw an error: "Directory not found: /some/path/that/doesnt/exist"
project.addDirectoryAtPathIfExists('/some/path/that/doesnt/exist', { recursive: true })
Expected behavior
Adding a source file or directory using the *IfExists functions should be "safe" operations. They should not have side effects that create error states. It should be possible to attempt to add a directory or source file that doesn't exist, and then attempt again later without errors being thrown.