Open ryan-palmer-snc opened 4 months ago
I am not sure why this line which attempts to export FileNotFoundError
doesn't make its way to ts-morph.d.ts
const {
InvalidOperationError,
FileNotFoundError,
ArgumentError,
ArgumentNullOrWhitespaceError,
ArgumentOutOfRangeError,
ArgumentTypeError,
BaseError,
DirectoryNotFoundError,
NotImplementedError,
NotSupportedError,
PathNotFoundError,
} = errors;
export {
ArgumentError,
ArgumentNullOrWhitespaceError,
ArgumentOutOfRangeError,
ArgumentTypeError,
BaseError,
DirectoryNotFoundError,
FileNotFoundError,
InvalidOperationError,
NotImplementedError,
NotSupportedError,
PathNotFoundError,
};
Describe the bug
Version: 23.0.0
When implementing a
FileSystemHost
to pass tots-morph
, yourreadFileSync
function must throwFileNotFoundError
if the file does not exist. Otherwise project functions likeaddSourceFileAtPathIfExists
will fail. This is because of this code from TransactionalFileSystem#readFileIfExistsSync in@ts-morph/common
:There is a note in the documentation that this function should throw
FileNotFoundError
which I noticed later, but from what I can tell,FileNotFoundError
is not even exported fromts-morph
so it's actually impossible to fix this without taking a dependency on@ts-morph/common
.To Reproduce
Implement a
FileSystemHost
which, when a file doesn't exist onreadFileSync
, throws any error other thanFileNotFoundError
. Inject this host into a newProject
instance, and then try to add a new source file viaproject.addSourceFileAtPathIfExists(...)
.Expected behavior
This is an implicit contract which
FileSystemHost
implementations must adhere to but (A) it is not obvious that this contract exists and (B) there seems to be no proper way to adhere to it.I strongly feel that the runtime dependency on the specific subclass of error being thrown by a
FileSystemHost
implementation effectively sabotages the inversion of that dependency making it impractical to provide a custom implementation, and should be removed. Short of that, at the very least the issue where this error is apparently not exported correctly should be resolved.