Skip to content

Commit fd0ba7e

Browse files
Fix error when no user tsconfig exists
1 parent 4e5ccfe commit fd0ba7e

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

ndc-lambda-sdk/src/inference.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ export function deriveSchema(functionsFilePath: string): SchemaDerivationResults
4242
}
4343

4444
function createTsProgram(functionsFilePath: string): Result<[ts.Program, ts.Diagnostic[]], ts.Diagnostic[]> {
45-
const fileDirectory = path.dirname(functionsFilePath);
46-
return loadTsConfig(fileDirectory).bind(parsedCommandLine => {
45+
return loadTsConfig(functionsFilePath).bind(parsedCommandLine => {
4746
const compilerHost = ts.createCompilerHost(parsedCommandLine.options);
4847
const program = ts.createProgram([functionsFilePath], parsedCommandLine.options, compilerHost);
4948
const compilerDiagnostics = ts.getPreEmitDiagnostics(program);
@@ -53,12 +52,24 @@ function createTsProgram(functionsFilePath: string): Result<[ts.Program, ts.Diag
5352
})
5453
}
5554

56-
function loadTsConfig(functionsDir: string): Result<ts.ParsedCommandLine, ts.Diagnostic[]> {
57-
const configPath = ts.findConfigFile(functionsDir, ts.sys.fileExists) ?? path.resolve(require.resolve("@tsconfig/node18/tsconfig.json"));
55+
function loadTsConfig(functionsFilePath: string): Result<ts.ParsedCommandLine, ts.Diagnostic[]> {
56+
const functionsDir = path.dirname(functionsFilePath);
57+
const userTsConfig = ts.findConfigFile(functionsDir, ts.sys.fileExists);
58+
// If the user doesn't have a tsconfig, use this one as a fallback. The TypeScript defaults are bad
59+
// (eg. strict and strictNullChecks is off by default)
60+
const fallbackTsConfig = path.resolve(require.resolve("@tsconfig/node18/tsconfig.json"));
61+
const configPath = userTsConfig ?? fallbackTsConfig;
5862
const configFile = ts.readConfigFile(configPath, ts.sys.readFile)
5963
if (configFile.error) {
6064
return new Err([configFile.error])
6165
}
66+
67+
// If we're using the fallback tsconfig, override the include path to point to the user's
68+
// functions directory, otherwise it will look in the fallback tsconfig's directory
69+
if (userTsConfig === undefined) {
70+
configFile.config.include = [path.join(functionsDir, "./**/*")];
71+
}
72+
6273
const parsedCommandLine = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath));
6374
if (parsedCommandLine.errors.find(d => d.category === ts.DiagnosticCategory.Error) !== undefined) {
6475
return new Err([...parsedCommandLine.errors]);

0 commit comments

Comments
 (0)