diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 4f50aa3ab1f59..9103ca2432d74 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -2677,18 +2677,30 @@ namespace ts { return fromCache || undefined; } - // An absolute path pointing to the containing directory of the config file - const basePath = getNormalizedAbsolutePath(getDirectoryPath(refPath), host.getCurrentDirectory()); - const sourceFile = host.getSourceFile(refPath, ScriptTarget.JSON) as JsonSourceFile | undefined; - addFileToFilesByName(sourceFile, sourceFilePath, /*redirectedPath*/ undefined); - if (sourceFile === undefined) { - projectReferenceRedirects.set(sourceFilePath, false); - return undefined; + let commandLine: ParsedCommandLine | undefined; + let sourceFile: JsonSourceFile | undefined; + if (host.getParsedCommandLine) { + commandLine = host.getParsedCommandLine(refPath); + if (!commandLine) { + projectReferenceRedirects.set(sourceFilePath, false); + return undefined; + } + sourceFile = Debug.assertDefined(commandLine.options.configFile); + } + else { + // An absolute path pointing to the containing directory of the config file + const basePath = getNormalizedAbsolutePath(getDirectoryPath(refPath), host.getCurrentDirectory()); + sourceFile = host.getSourceFile(refPath, ScriptTarget.JSON) as JsonSourceFile | undefined; + addFileToFilesByName(sourceFile, sourceFilePath, /*redirectedPath*/ undefined); + if (sourceFile === undefined) { + projectReferenceRedirects.set(sourceFilePath, false); + return undefined; + } + sourceFile.path = sourceFilePath; + sourceFile.resolvedPath = sourceFilePath; + sourceFile.originalFileName = refPath; + commandLine = parseJsonSourceFileConfigFileContent(sourceFile, configParsingHost, basePath, /*existingOptions*/ undefined, refPath); } - sourceFile.path = sourceFilePath; - sourceFile.resolvedPath = sourceFilePath; - sourceFile.originalFileName = refPath; - const commandLine = parseJsonSourceFileConfigFileContent(sourceFile, configParsingHost, basePath, /*existingOptions*/ undefined, refPath); const resolvedRef: ResolvedProjectReference = { commandLine, sourceFile }; projectReferenceRedirects.set(sourceFilePath, resolvedRef); if (commandLine.projectReferences) { diff --git a/src/compiler/tsbuild.ts b/src/compiler/tsbuild.ts index f60641fe0cfe7..10beb32136140 100644 --- a/src/compiler/tsbuild.ts +++ b/src/compiler/tsbuild.ts @@ -399,6 +399,7 @@ namespace ts { let projectCompilerOptions = baseCompilerOptions; const compilerHost = createCompilerHostFromProgramHost(host, () => projectCompilerOptions); setGetSourceFileAsHashVersioned(compilerHost, host); + compilerHost.getParsedCommandLine = parseConfigFile; const buildInfoChecked = createFileMap(toPath); diff --git a/src/compiler/types.ts b/src/compiler/types.ts index b421efa19e0d8..36b1a32e54a6a 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -5130,6 +5130,7 @@ namespace ts { /* @internal */ hasInvalidatedResolution?: HasInvalidatedResolution; /* @internal */ hasChangedAutomaticTypeDirectiveNames?: boolean; createHash?(data: string): string; + /*@internal*/getParsedCommandLine?(fileName: string): ParsedCommandLine | undefined; // TODO: later handle this in better way in builder host instead once the api for tsbuild finalizes and doesnt use compilerHost as base /*@internal*/createDirectory?(directory: string): void;