Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | 16x 935x 935x 935x 935x 935x 829x 829x 2487x 2487x 832x 829x 829x 12x 12x 5x 5x 12x 12x 935x 935x 935x 16x 9x 9x 27x 27x 3x 8x 8x 9x 9x 9x 935x 935x 935x 934x 934x 1868x 2x 2x 934x 5x 5x 5x 5x 6x 6x 6x 5x 12x 12x 12x 12x 12x 5x 5x 6x 6x 6x 6x 6x 1x 5x 6x 5x 6x 6x 5x 12x 1181x 1181x 1181x 1708x 8316x 8316x 915x 793x 266x 12x 294x 294x 294x 105x 105x 105x 294x 95x 2x 93x 119x 119x 85x 8x | import { dirname, resolve, join, isAbsolute } from "node:path";
import IFileSystem from "../types/IFileSystem";
import NodeFileSystem from "../NodeFileSystem";
/** Default file system instance (singleton for performance) */
const defaultFs = NodeFileSystem.instance;
/**
* Auto-discovery of include paths for C-Next compilation
*
* Implements 4-tier include path discovery:
* 1. File's own directory (for relative #include "header.h")
* 2. Project root (walk up to find platformio.ini, cnext.config.json, .git)
* 3. PlatformIO library dependencies (.pio/libdeps/ and lib_extra_dirs)
* 4. Arduino library paths (~/Arduino/libraries/ or ~/Documents/Arduino/libraries/)
*
* Note: System paths (compiler defaults) not included to avoid dependencies.
* Users can add system paths via --include flag if needed.
*/
class IncludeDiscovery {
/**
* Discover include paths for a file
*
* @param inputFile - Path to .cnx file being compiled
* @param fs - File system abstraction (defaults to NodeFileSystem)
* @returns Array of include directory paths
*/
static discoverIncludePaths(
inputFile: string,
fs: IFileSystem = defaultFs,
): string[] {
const paths: string[] = [];
// Tier 1: File's own directory (highest priority)
const fileDir = dirname(resolve(inputFile));
paths.push(fileDir);
// Tier 2: Project root detection
const projectRoot = this.findProjectRoot(fileDir, fs);
if (projectRoot) {
// Add common include directories if they exist
const commonDirs = ["include", "src", "lib"];
for (const dir of commonDirs) {
const includePath = join(projectRoot, dir);
if (fs.exists(includePath) && fs.isDirectory(includePath)) {
paths.push(includePath);
}
}
// Tier 3: Issue #355 - PlatformIO library dependencies
// When platformio.ini exists, check for .pio/libdeps/ and add all library paths
const pioIniPath = join(projectRoot, "platformio.ini");
if (fs.exists(pioIniPath)) {
const libDepsPath = join(projectRoot, ".pio", "libdeps");
if (fs.exists(libDepsPath) && fs.isDirectory(libDepsPath)) {
const pioLibPaths = this.discoverPlatformIOLibPaths(libDepsPath, fs);
paths.push(...pioLibPaths);
}
// Issue #355: Also parse lib_extra_dirs from platformio.ini
const extraDirs = this.parsePlatformIOLibExtraDirs(
pioIniPath,
projectRoot,
fs,
);
paths.push(...extraDirs);
}
}
// Tier 4: Issue #355 - Arduino library paths
// Check for Arduino libraries in common locations
const arduinoPaths = this.discoverArduinoLibPaths(fs);
paths.push(...arduinoPaths);
// Remove duplicates
return Array.from(new Set(paths));
}
/** Common subdirectories where library headers might live */
private static readonly LIBRARY_SUB_DIRS = ["src", "include", "src/include"];
/**
* Add a library path and its common subdirectories to the paths array.
* Checks src/, include/, and src/include/ subdirectories.
*/
private static _addLibraryWithSubDirs(
libPath: string,
paths: string[],
fs: IFileSystem,
): void {
paths.push(libPath);
for (const subDir of this.LIBRARY_SUB_DIRS) {
const subPath = join(libPath, subDir);
if (fs.exists(subPath) && fs.isDirectory(subPath)) {
paths.push(subPath);
}
}
}
/**
* Collect all library directories from a parent directory.
* Each subdirectory is treated as a library root.
*/
private static _collectLibrariesFromDir(
parentDir: string,
paths: string[],
fs: IFileSystem,
): void {
const entries = fs.readdir(parentDir);
for (const entry of entries) {
const entryPath = join(parentDir, entry);
Eif (fs.isDirectory(entryPath)) {
this._addLibraryWithSubDirs(entryPath, paths, fs);
}
}
}
/**
* Discover Arduino library paths
*
* Issue #355: Arduino stores libraries in platform-specific locations:
* - Linux: ~/Arduino/libraries/
* - macOS: ~/Documents/Arduino/libraries/
* - Windows: %USERPROFILE%\Documents\Arduino\libraries\
*
* @param fs - File system abstraction
* @returns Array of library directory paths
*/
private static discoverArduinoLibPaths(fs: IFileSystem): string[] {
const paths: string[] = [];
const home = process.env.HOME || process.env.USERPROFILE || "";
if (!home) return paths;
// Common Arduino library locations
const arduinoLibDirs = [
join(home, "Arduino", "libraries"), // Linux
join(home, "Documents", "Arduino", "libraries"), // macOS / Windows
];
for (const libDir of arduinoLibDirs) {
if (fs.exists(libDir) && fs.isDirectory(libDir)) {
try {
this._collectLibrariesFromDir(libDir, paths, fs);
} catch {
// Expected: directory may not exist or be readable
}
}
}
return paths;
}
/**
* Discover PlatformIO library dependency paths
*
* PlatformIO stores libraries in .pio/libdeps/<env>/<library>/
* This function finds all library directories across all environments.
*
* @param libDepsPath - Path to .pio/libdeps/
* @param fs - File system abstraction
* @returns Array of library directory paths
*/
private static discoverPlatformIOLibPaths(
libDepsPath: string,
fs: IFileSystem,
): string[] {
const paths: string[] = [];
try {
// Iterate through environment directories (e.g., teensy40, teensy41, esp32)
const envDirs = fs.readdir(libDepsPath);
for (const envDir of envDirs) {
const envPath = join(libDepsPath, envDir);
Eif (fs.isDirectory(envPath)) {
this._collectLibrariesFromDir(envPath, paths, fs);
}
}
} catch {
// Expected: .pio directory may not exist
}
return paths;
}
/**
* Parse platformio.ini for lib_extra_dirs
*
* Issue #355: PlatformIO allows specifying additional library directories
* via lib_extra_dirs in platformio.ini. This parses those and returns
* resolved absolute paths.
*
* @param pioIniPath - Path to platformio.ini
* @param projectRoot - Project root directory for resolving relative paths
* @param fs - File system abstraction
* @returns Array of library directory paths
*/
private static parsePlatformIOLibExtraDirs(
pioIniPath: string,
projectRoot: string,
fs: IFileSystem,
): string[] {
const paths: string[] = [];
try {
const content = fs.readFile(pioIniPath);
// Match lib_extra_dirs in any section
// Format can be:
// lib_extra_dirs = path1, path2
// lib_extra_dirs =
// path1
// path2
const libExtraDirsRegex =
/^\s*lib_extra_dirs\s*=\s*(.+?)(?=^\s*\[|\s*^\w+\s*=|$)/gms;
let match;
while ((match = libExtraDirsRegex.exec(content)) !== null) {
const value = match[1];
// Split by newlines or commas, handling both single-line and multi-line formats
const dirs = value
.split(/[\n,]/)
.map((d) => {
// Strip inline comments (e.g., "path ; comment" or "path # comment")
const semicolonIdx = d.indexOf(";");
const hashIdx = d.indexOf("#");
const commentIndex = Math.min(
semicolonIdx === -1 ? Infinity : semicolonIdx,
hashIdx === -1 ? Infinity : hashIdx,
);
return d.slice(0, commentIndex).trim();
})
.map((d) => {
// Strip surrounding quotes (e.g., "path with spaces" or 'path')
if (
(d.startsWith('"') && d.endsWith('"')) ||
(d.startsWith("'") && d.endsWith("'"))
) {
return d.slice(1, -1);
}
return d;
})
.filter((d) => d.length > 0);
for (const dir of dirs) {
// Resolve relative to project root
const fullPath = isAbsolute(dir) ? dir : join(projectRoot, dir);
if (fs.exists(fullPath) && fs.isDirectory(fullPath)) {
paths.push(fullPath);
}
}
}
} catch {
// Expected: platformio.ini may not exist or be malformed
}
return paths;
}
/**
* Find project root by walking up directory tree looking for markers
*
* Project markers (in order of preference):
* - platformio.ini (PlatformIO project)
* - cnext.config.json or .cnext.json (C-Next config)
* - .git/ (Git repository root)
*
* @param startDir - Directory to start search from
* @param fs - File system abstraction (defaults to NodeFileSystem)
* @returns Project root path or null if not found
*/
static findProjectRoot(
startDir: string,
fs: IFileSystem = defaultFs,
): string | null {
let dir = resolve(startDir);
const markers = [
"platformio.ini",
"cnext.config.json",
".cnext.json",
".cnextrc",
".git",
];
// Walk up directory tree until marker found or filesystem root
while (dir !== dirname(dir)) {
for (const marker of markers) {
const markerPath = join(dir, marker);
if (fs.exists(markerPath)) {
return dir;
}
}
dir = dirname(dir);
}
return null;
}
/**
* Extract #include directives from source code
*
* Matches both:
* - #include "header.h" (local includes)
* - #include <header.h> (system includes)
*
* @param content - Source file content
* @returns Array of include paths (for backwards compatibility)
*/
static extractIncludes(content: string): string[] {
return this.extractIncludesWithInfo(content).map((info) => info.path);
}
/**
* Extract #include directives with local/system info
*
* Issue #355: Returns whether each include is local ("...") or system (<...>)
* so we can warn appropriately when local includes aren't found.
*
* @param content - Source file content
* @returns Array of include info objects
*/
static extractIncludesWithInfo(
content: string,
): Array<{ path: string; isLocal: boolean }> {
const includes: Array<{ path: string; isLocal: boolean }> = [];
// Match #include directives, capturing the delimiter to determine local vs system
const includeRegex = /^\s*#\s*include\s*([<"])([^>"]+)[>"]/gm;
let match;
while ((match = includeRegex.exec(content)) !== null) {
const delimiter = match[1];
const path = match[2];
includes.push({
path,
isLocal: delimiter === '"',
});
}
return includes;
}
/**
* Resolve an include path using search directories
*
* @param includePath - The include path from #include directive
* @param searchPaths - Directories to search in
* @param fs - File system abstraction (defaults to NodeFileSystem)
* @returns Resolved absolute path or null if not found
*/
static resolveInclude(
includePath: string,
searchPaths: string[],
fs: IFileSystem = defaultFs,
): string | null {
// If already absolute, check if it exists
if (isAbsolute(includePath)) {
return fs.exists(includePath) ? includePath : null;
}
// Search in each directory
for (const searchDir of searchPaths) {
const fullPath = join(searchDir, includePath);
if (fs.exists(fullPath) && fs.isFile(fullPath)) {
return fullPath;
}
}
return null;
}
}
export default IncludeDiscovery;
|