dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
5k stars 195 forks source link

Calling `addSourceFileAtPathIfExists` has a side effect of internally remembering directories that don't exist which causes subsequent operations to fail #1554

Open kronodeus opened 4 months ago

kronodeus commented 4 months ago

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.