ts-json-schema-generator icon indicating copy to clipboard operation
ts-json-schema-generator copied to clipboard

tsconfig loading issue with monorepo

Open tomwidmer opened this issue 1 year ago • 2 comments

When trying to use this tool in a monorepo, I'm getting 'module not found' errors for some of my cross-module imports that rely on the paths setting in tsconfig. This seems to be due to an issue with how the tsconfig file is being loaded when your tsconfig is in a subdirectory and extends other tsconfig files:

i.e. tsconfig: 'lib/mydomain/tsconfig.lib.json':

{
  "extends": "./tsconfig.json",
  // ...
}

In loadTsConfigFile.ts, you have:

const parseResult = ts.parseJsonConfigFileContent(
    config.config,
    ts.sys,
    path.resolve(path.dirname(configFile)),
    {},
    configFile
);

If I log parseResult, I see it has errors and is unable to find./tsconfig.json, since it's resolving configFile against the passed basePath path.resolve(path.dirname(configFile)), which is yielding a path of lib/mydomain/lib/mydomain/tsconfig.lib.json. The trivial fix is to pass path.resolve(configFile) as the 5th argument, instead of configFile. Making this change locally resolved the issue.

See https://github.com/microsoft/TypeScript/blob/cf33fd0cde22905effce371bb02484a9f2009023/src/compiler/commandLineParser.ts#L2074 for the equivalent code in tsc, which does indeed resolve the path of configFile.

I couldn't find a workaround, since your code handles the tsconfig loading and there's no way to specify the config manually.

tomwidmer avatar Jan 15 '24 13:01 tomwidmer

Maybe using the get-tsconfig package to load the tsconfig.json would help.

jonaskello avatar Feb 19 '24 15:02 jonaskello