Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kronodeus opened this issue Jul 8, 2024 · 0 comments

Comments

@kronodeus
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant