Open broofa opened 2 months ago
Quick followup to confirm that this issue does, indeed, cause TJS.generateSchema()
operations to fail when run concurrently (async) on multiple projects. 😢
[Breadcrumb for anyone else that might stumble across this issue...]
This appears to be an issue with the TS compiler API. Specifically, in ts.createProgram()
. It looks like repeated calls to this method reuse the cached file information for any rootNames
paths that match previous rootNames
, regardless of basePath
.
FWIW, you can diagnose this problem by just doing console.log(program)
and looking at the contents of the program.sourceFileToPackageName
map. If it's showing unexpected files... this is probably the issue.
The solution ("workaround"? "Hack"?) is to use a basePath
in getProgramFiles()
that is "high enough" to encompass all of the rootNames
paths to be processed, so each rootNames
path is unique across all sources being processed.
Maintainers: I'm going to leave this open since it affects the expected use of TJS, and in the hopes someone more knowledgable than I might know a better workaround. But if not, feel free to close.
Issue
When generating a schema programmatically, specifying an absolute
basePath
causes a "Error: type [target TS type] not found" error, presumably because TSJ is failing to find the specified source file(s).Similarly, if the
basePath
is relative to a current working directory that is off to the side of the target directory (i.e.cwd
is not a parent of the target directory sobasePath
has ".." paths in it), it fails similarly.Impact: This is a non-trivial issue because, combined, these two problems make it next to impossible to run multiple
async
schema generation tasks. The inability to provide an absolute path meansbasePath
must be specified relative toprocess.cwd()
. But relative paths only work if they descend directly fromprocess.cwd()
, which means you very likely need to change the working directory for things to work. (But that will likely break any other TSJ task that happens to depend on the currentcwd
).To Reproduce
cd tsj-test
npm install
npm test
(with node@18 or node@20)You should see output something like this:
The test attempts to generate schemas for the exact same code, using four different configurations of current working directory,
basePath
, androotNames
. The first two - which use relativebasePath
values with a current working directory in or above the target directory - work fine.The third test, which uses an absolute
basePath
that points to the same directory as the second test, fails.Similarly, the fourth test uses a relative path that points to the same directory, but with a current working directory "off to the side" (in
./stubdir
) of the target directory, also fails.Expected behavior
All four tests should pass.