MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / readCompileDbIncludeDirs

Function readCompileDbIncludeDirs

src/resolution/import-resolver.ts:2051–2096  ·  view source on GitHub ↗
(dbPath: string, projectRoot: string)

Source from the content-addressed store, hash-verified

2049
2050/** The include directories the compilation database at `dbPath` names; null when it can't be read. */
2051export function readCompileDbIncludeDirs(dbPath: string, projectRoot: string): string[] | null {
2052 try {
2053 const content = fs.readFileSync(dbPath, 'utf-8');
2054 const entries = JSON.parse(content) as Array<{
2055 directory: string;
2056 command?: string;
2057 arguments?: string[];
2058 }>;
2059 if (!Array.isArray(entries)) return null;
2060
2061 const dirSet = new Set<string>();
2062 for (const entry of entries) {
2063 const dir = entry.directory || projectRoot;
2064 const args = entry.arguments || (entry.command ? shlexSplit(entry.command) : []);
2065 for (let i = 0; i < args.length; i++) {
2066 const arg = args[i]!;
2067 let includeDir: string | undefined;
2068 // -I<dir> (no space)
2069 if (arg.startsWith('-I') && arg.length > 2) {
2070 includeDir = arg.substring(2);
2071 }
2072 // -isystem <dir> (space-separated)
2073 else if ((arg === '-isystem' || arg === '-I') && i + 1 < args.length) {
2074 includeDir = args[i + 1];
2075 i++; // skip next arg
2076 }
2077 if (includeDir) {
2078 // Normalize: resolve relative to the compilation directory
2079 const absPath = path.isAbsolute(includeDir)
2080 ? includeDir
2081 : path.resolve(dir, includeDir);
2082 const relPath = path.relative(projectRoot, absPath).replace(/\\/g, '/');
2083 // Skip system directories and paths outside the project
2084 // (relative paths starting with .. or absolute paths like
2085 // /usr/include or C:\usr on Windows)
2086 if (!relPath.startsWith('..') && relPath.length > 0 && !path.isAbsolute(relPath)) {
2087 dirSet.add(relPath);
2088 }
2089 }
2090 }
2091 }
2092 return Array.from(dirSet);
2093 } catch {
2094 return null;
2095 }
2096}
2097
2098/**
2099 * Minimal shlex-style split for compiler command strings.

Callers 2

moduleConfigDigestFunction · 0.90

Calls 5

shlexSplitFunction · 0.85
parseMethod · 0.80
fromMethod · 0.80
resolveMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected