ts-morph icon indicating copy to clipboard operation
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

Open kronodeus opened this issue 1 year ago • 0 comments

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.

kronodeus avatar Jul 08 '24 03:07 kronodeus