All files / transpiler/data IncludeDiscovery.ts

100% Statements 145/145
94.44% Branches 85/90
100% Functions 21/21
100% Lines 143/143

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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532            34x                                                   1237x     1237x 1237x     1237x 1237x   1051x 1051x 3153x 3153x 1042x           1051x 1051x 15x 15x 5x 5x       15x         15x           1237x 1237x     1237x       34x                     9x   9x 27x 27x 3x                           8x 8x 9x 9x 9x                                 1237x 1237x   1237x     1236x         1236x 2472x 2x 2x             1236x                                 5x   5x   5x 5x 6x 6x 6x             5x                                   15x 15x   15x 15x 40x 40x 40x 32x     8x 8x       5x 5x     8x     15x                         13x                                               15x   15x 15x               15x       8x       14x 14x 14x       14x       14x       1x   13x   14x   8x   11x 11x 10x               15x                                     1792x   1792x                 1792x 3123x 15265x 15265x 1211x     1912x     581x                           38x                                 700x   700x 700x 141575x 141180x 141180x   395x 395x 95x 95x   300x 300x     700x         395x 395x       576x 5x     390x       1577x         693x 693x 308x   693x                     395x 5x     390x 390x 87x     303x 303x 303x 1x     302x 302x 302x         2658x     302x 2x     300x                                     700x     700x 300x           700x                                 267x 2x       265x 363x 363x 207x       58x          
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;
  }
 
  /**
   * Collect the raw value of every `lib_extra_dirs` key in a platformio.ini.
   *
   * Line-based rather than a single pattern. The previous
   * /^\s*lib_extra_dirs\s*=\s*(.+?)(?=^\s*\[|\s*^\w+\s*=|$)/gms was
   * super-linear (S8786) and, more importantly, wrong: under /m the `$`
   * alternative matches at the end of every line, so the lazy capture stopped
   * at the first one and the documented multi-line form kept only its first
   * path (#1181). The section and next-key alternatives were unreachable.
   *
   * A continuation line is one that is indented and contains no `=` of its
   * own; the value ends at the next section header, the next key, or the end
   * of the file.
   */
  private static _collectLibExtraDirsValues(content: string): string[] {
    const values: string[] = [];
    const lines = content.split("\n");
 
    let index = 0;
    while (index < lines.length) {
      const keyMatch = /^[ \t]*lib_extra_dirs[ \t]*=(.*)$/.exec(lines[index]);
      index += 1;
      if (!keyMatch) {
        continue;
      }
 
      const collected = [keyMatch[1]];
      while (
        index < lines.length &&
        IncludeDiscovery._isContinuationLine(lines[index])
      ) {
        collected.push(lines[index]);
        index += 1;
      }
 
      values.push(collected.join("\n"));
    }
 
    return values;
  }
 
  /**
   * A continuation of the value above it: indented, not starting a key of its
   * own, and not opening a new section.
   *
   * The key test is anchored rather than a bare `includes("=")`: a directory
   * name may contain `=` (`/opt/vendor/lib=v2`), and treating that as a new key
   * would end the value early and silently drop it -- the same loss #1181 was
   * about. Only `name =` at the start of the line begins a key.
   */
  private static _isContinuationLine(line: string): boolean {
    return (
      /^[ \t]+\S/.test(line) &&
      !/^[ \t]*[\w.]+[ \t]*=/.test(line) &&
      !line.trimStart().startsWith("[")
    );
  }
 
  /**
   * 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
      for (const value of IncludeDiscovery._collectLibExtraDirsValues(
        content,
      )) {
        // 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);
  }
 
  /**
   * Scan `#include` directives, replacing
   * /^\s*#\s*include\s*([<"])([^>"]+)[>"]/gm, which backtracks
   * super-linearly on its whitespace runs (S8786).
   *
   * Behavior is preserved exactly, including two quirks worth naming:
   * the closing delimiter is not required to match the opening one
   * (`#include <a.h"` is accepted), and every whitespace run may span
   * newlines, so a `#` alone on one line with `include` on the next still
   * matches. Only whitespace may precede the `#` on its own line.
   */
  private static _scanIncludeDirectives(
    content: string,
  ): { delimiter: string; path: string }[] {
    const found: { delimiter: string; path: string }[] = [];
 
    let index = 0;
    while (index < content.length) {
      if (content[index] !== "#") {
        index += 1;
        continue;
      }
      const directive = IncludeDiscovery._readIncludeAt(content, index);
      if (directive === null) {
        index += 1;
        continue;
      }
      found.push({ delimiter: directive.delimiter, path: directive.path });
      index = directive.next;
    }
 
    return found;
  }
 
  /** True when only whitespace separates `index` from the start of its line. */
  private static _lineStartIsClear(content: string, index: number): boolean {
    for (
      let before = index - 1;
      before >= 0 && content[before] !== "\n";
      before -= 1
    ) {
      if (!IncludeDiscovery._isSpace(content[before])) {
        return false;
      }
    }
    return true;
  }
 
  private static _isSpace(character: string | undefined): boolean {
    return character !== undefined && /\s/.test(character);
  }
 
  /** Advance past a run of whitespace. */
  private static _skipSpace(content: string, from: number): number {
    let cursor = from;
    while (IncludeDiscovery._isSpace(content[cursor])) {
      cursor += 1;
    }
    return cursor;
  }
 
  /**
   * Read one `#include` beginning at the `#` in `index`, or null if there
   * isn't one there. `next` is the index just past the closing delimiter.
   */
  private static _readIncludeAt(
    content: string,
    index: number,
  ): { delimiter: string; path: string; next: number } | null {
    if (!IncludeDiscovery._lineStartIsClear(content, index)) {
      return null;
    }
 
    let cursor = IncludeDiscovery._skipSpace(content, index + 1);
    if (!content.startsWith("include", cursor)) {
      return null;
    }
 
    cursor = IncludeDiscovery._skipSpace(content, cursor + "include".length);
    const delimiter = content[cursor];
    if (delimiter !== "<" && delimiter !== '"') {
      return null;
    }
 
    const pathStart = cursor + 1;
    let pathEnd = pathStart;
    while (
      pathEnd < content.length &&
      content[pathEnd] !== ">" &&
      content[pathEnd] !== '"'
    ) {
      pathEnd += 1;
    }
    // [^>"]+ requires at least one character, and a closing delimiter
    if (pathEnd === pathStart || pathEnd >= content.length) {
      return null;
    }
 
    return {
      delimiter,
      path: content.slice(pathStart, pathEnd),
      next: pathEnd + 1,
    };
  }
 
  /**
   * 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
    for (const directive of IncludeDiscovery._scanIncludeDirectives(content)) {
      includes.push({
        path: directive.path,
        isLocal: directive.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;