(dbPath: string, projectRoot: string)
| 1801 | |
| 1802 | /** The include directories the compilation database at `dbPath` names; null when it can't be read. */ |
| 1803 | export function readCompileDbIncludeDirs(dbPath: string, projectRoot: string): string[] | null { |
| 1804 | try { |
| 1805 | const content = fs.readFileSync(dbPath, 'utf-8'); |
| 1806 | const entries = JSON.parse(content) as Array<{ |
| 1807 | directory: string; |
| 1808 | command?: string; |
| 1809 | arguments?: string[]; |
| 1810 | }>; |
| 1811 | if (!Array.isArray(entries)) return null; |
| 1812 | |
| 1813 | const dirSet = new Set<string>(); |
| 1814 | for (const entry of entries) { |
| 1815 | const dir = entry.directory || projectRoot; |
| 1816 | const args = entry.arguments || (entry.command ? shlexSplit(entry.command) : []); |
| 1817 | for (let i = 0; i < args.length; i++) { |
| 1818 | const arg = args[i]!; |
| 1819 | let includeDir: string | undefined; |
| 1820 | // -I<dir> (no space) |
| 1821 | if (arg.startsWith('-I') && arg.length > 2) { |
| 1822 | includeDir = arg.substring(2); |
| 1823 | } |
| 1824 | // -isystem <dir> (space-separated) |
| 1825 | else if ((arg === '-isystem' || arg === '-I') && i + 1 < args.length) { |
| 1826 | includeDir = args[i + 1]; |
| 1827 | i++; // skip next arg |
| 1828 | } |
| 1829 | if (includeDir) { |
| 1830 | // Normalize: resolve relative to the compilation directory |
| 1831 | const absPath = path.isAbsolute(includeDir) |
| 1832 | ? includeDir |
| 1833 | : path.resolve(dir, includeDir); |
| 1834 | const relPath = path.relative(projectRoot, absPath).replace(/\\/g, '/'); |
| 1835 | // Skip system directories and paths outside the project |
| 1836 | // (relative paths starting with .. or absolute paths like |
| 1837 | // /usr/include or C:\usr on Windows) |
| 1838 | if (!relPath.startsWith('..') && relPath.length > 0 && !path.isAbsolute(relPath)) { |
| 1839 | dirSet.add(relPath); |
| 1840 | } |
| 1841 | } |
| 1842 | } |
| 1843 | } |
| 1844 | return Array.from(dirSet); |
| 1845 | } catch { |
| 1846 | return null; |
| 1847 | } |
| 1848 | } |
| 1849 | |
| 1850 | /** |
| 1851 | * Minimal shlex-style split for compiler command strings. |
no test coverage detected