All files / server/services/compiler cli-runner.ts

83.75% Statements 67/80
72.05% Branches 49/68
75% Functions 12/16
86.84% Lines 66/76

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                15x                                             45x             45x 41x   45x 45x                       45x 45x   45x   45x 45x           45x       45x 2x 2x 2x                           43x 45x 45x 45x   45x 30x 30x   30x 30x               13x 13x 13x                                                                   13x 13x 13x   13x       2x       12x 12x 12x             43x 75x 75x             43x       75x       58x       13x             30x 30x     9x 30x       21x   30x             31x 31x   31x 31x   31x 8x 23x   1x 1x 1x 1x     22x 22x   19x 14x   5x   3x      
import { Logger } from "@shared/logger";
import { readFile, readdir } from "node:fs/promises";
import { dirname, join } from "node:path";
import { ProcessExecutor, ProcessOutputLimitError } from "../process-executor";
import type { CompilationError } from "./compiler-output-parser";
import { limitCompilerDiagnostics, MAX_COMPILER_OUTPUT_BYTES, parseCompilerDiagnostics } from "../compiler-diagnostics";
import { config as appConfig } from "../../config";
 
const logger = new Logger("CLIRunner");
 
export interface CLICompileConfig {
  fqbn: string;
  buildPath?: string;
  buildCachePath?: string;
}
 
export interface CLICompileResult {
  success: boolean;
  output: string;
  errors?: string;
  parsedErrors?: CompilationError[];
  binary?: Buffer;
}
 
/**
 * Builds arduino-cli compile arguments.
 */
export function buildCompileArgs(
  config: CLICompileConfig,
  sketchDir: string,
): string[] {
  const args = [
    "compile",
    "--fqbn",
    config.fqbn,
    "--verbose",
  ];
 
  if (config.buildPath) {
    args.push("--build-path", config.buildPath);
  }
  args.push(sketchDir);
  return args;
}
 
/**
 * Executes arduino-cli compilation.
 */
export async function compileWithArduinoCli(
  sketchFile: string,
  config: CLICompileConfig,
  processExecutor: ProcessExecutor,
): Promise<CLICompileResult> {
  // Arduino CLI expects the sketch directory; the public compiler passes the .ino path.
  const sketchDir = dirname(sketchFile);
  const args = buildCompileArgs(config, sketchDir);
 
  logger.info(`Executing arduino-cli ${args.join(" ")}`);
 
  try {
    const result = await processExecutor.execute("arduino-cli", args, {
      timeout: appConfig.compilation.timeoutMs,
      stdio: "pipe",
      maxOutputBytes: MAX_COMPILER_OUTPUT_BYTES,
    });
    // A diagnostics flood is a failed compile with truncated output, not a missing CLI.
    const outputLimited = result.error instanceof ProcessOutputLimitError;
 
    // ProcessExecutor also reports a non-zero exit code as `error`. Only code
    // -1 means that arduino-cli itself could not be started (e.g. ENOENT).
    if (result.error && !outputLimited && (result.code === -1 || isCliSpawnError(result.error))) {
      const errorMessage = withPathHint(`Failed to execute arduino-cli: ${result.error.message}.`);
      logger.error(errorMessage);
      return {
        success: false,
        output: "",
        errors: errorMessage,
        parsedErrors: [{
          file: "system",
          line: 0,
          column: 0,
          type: "error",
          message: errorMessage,
        }],
      };
    }
 
    const output = result.stdout || "";
    const errors = result.stderr || "";
    const code = result.code;
    const diagnosticOutput = selectDiagnosticOutput(output, errors);
 
    if (code === 0) {
      const parsedOutput = parseCompilerOutput(output);
      const parsedWarnings = parseCompilerDiagnostics(diagnosticOutput, 0)
        .filter((diagnostic) => diagnostic.type === "warning");
      const binary = await discoverBuildBinary(config.buildPath || sketchDir);
      return {
        success: true,
        output: parsedOutput,
        errors: "",
        parsedErrors: parsedWarnings,
        binary,
      };
    } else {
      const cleanedErrors = limitCompilerDiagnostics(cleanErrorMessage(diagnosticOutput, sketchFile));
      const parsedErrors = parseCompilerDiagnostics(cleanedErrors, 0);
      return {
        success: false,
        output: hasMemoryUsage(diagnosticOutput) ? parseCompilerOutput(diagnosticOutput) : "",
        errors: cleanedErrors,
        parsedErrors,
      };
    }
  } catch (error) {
    const errorText = error instanceof Error ? error.message : String(error);
    const errorMessage = withPathHint(`Failed to execute arduino-cli: ${errorText}.`, error);
    const errorType = error instanceof Error ? error.name : typeof error;
    const errorCode = error && typeof error === "object" && "code" in error && typeof error.code === "string"
      ? `, ${error.code}`
      : "";
    logger.error(`Failed to execute arduino-cli (${errorType}${errorCode}, ${Buffer.byteLength(errorMessage)} diagnostic bytes)`);
    return {
      success: false,
      output: "",
      errors: errorMessage,
      parsedErrors: [{
        file: "system",
        line: 0,
        column: 0,
        type: "error",
        message: errorMessage,
      }],
    };
  }
}
 
/**
 * Cleans error messages by removing sketch directory paths.
 */
function cleanErrorMessage(errors: string, sketchDir: string): string {
  let cleanedErrors = errors;
  Eif (sketchDir) {
    cleanedErrors = cleanedErrors.replaceAll(sketchDir, "sketch.ino");
  }
  return cleanedErrors;
}
 
function withPathHint(message: string, error?: unknown): string {
  return `${message}${error === undefined || isCliSpawnError(error) ? " Make sure arduino-cli is installed and in PATH." : ""}`;
}
 
function isCliSpawnError(error: unknown): boolean {
  Iif (!error || typeof error !== "object") return false;
  const candidate = error as { code?: unknown; message?: unknown };
  return (
    candidate.code === "ENOENT" ||
    (typeof candidate.message === "string" && /\bspawn\b.*(?:ENOENT|not found|cannot find)/i.test(candidate.message))
  );
}
 
function selectDiagnosticOutput(stdout: string, stderr: string): string {
  const outputLines = stdout.split(/\r?\n/).filter((line) => {
    const trimmed = line.trim();
    return (
      isMemoryLine(trimmed) ||
      isLibraryScanLine(trimmed) ||
      /^(?:[^:\n]+:\d+(?::\d+)?:\s*)?(?:warning|error):/i.test(trimmed)
    );
  });
 
  return [...outputLines, stderr].filter(Boolean).join("\n");
}
 
function isMemoryLine(line: string): boolean {
  return /^(?:sketch uses|global variables use|der sketch verwendet|globale variablen verwenden|not enough memory|data section exceeds available space|compilation error:\s*data section exceeds available space)/i.test(line);
}
 
function isLibraryScanLine(line: string): boolean {
  return /(?:warning\s*:\s*library\b|multiple libraries were found|library .*\b(?:not found|incompatible|failed)\b)/i.test(line);
}
 
function hasMemoryUsage(output: string): boolean {
  return /^(?:sketch uses|global variables use|der sketch verwendet|globale variablen verwenden)\b/im.test(output);
}
 
/**
 * Reads the HEX artifact emitted by arduino-cli from the build output directory.
 */
async function discoverBuildBinary(buildOutputDir: string): Promise<Buffer | undefined> {
  try {
    const hexCandidates = (await readdir(buildOutputDir))
      .filter((entry) => entry.endsWith(".hex"))
      .sort((a, b) => a.localeCompare(b));
    const preferred = hexCandidates.find((entry) => !entry.includes("with_bootloader")) || hexCandidates[0];
    Iif (preferred) {
      return await readFile(join(buildOutputDir, preferred));
    }
  } catch (error) {
    logger.debug(`[CompileCache] failed to read build hex output: ${error instanceof Error ? error.message : String(error)}`);
  }
  return undefined;
}
 
/**
 * Parses arduino-cli output to extract memory usage and format it.
 */
function parseCompilerOutput(output: string): string {
  const progSizeRegex = /(Sketch uses[^\n]*\.|Der Sketch verwendet[^\n]*\.)/;
  const ramSizeRegex = /(Global variables use[^\n]*\.|Globale Variablen verwenden[^\n]*\.)/;
 
  const progSizeMatch = progSizeRegex.exec(output);
  const ramSizeMatch = ramSizeRegex.exec(output);
 
  if (progSizeMatch && ramSizeMatch) {
    return `${progSizeMatch[0]}\n${ramSizeMatch[0]}\n\nBoard: Arduino UNO`;
  } else if (progSizeMatch || ramSizeMatch) {
    // Partial memory info
    const parts = [];
    Eif (progSizeMatch) parts.push(progSizeMatch[0]);
    Iif (ramSizeMatch) parts.push(ramSizeMatch[0]);
    return `${parts.join("\n")}\n\nBoard: Arduino UNO`;
  } else {
    // No memory info - check if there's any success output
    const trimmed = output.trim();
    if (trimmed) {
      // If output looks like a generic success message, use (Simulation) suffix
      if (trimmed.toLowerCase().includes("success") || trimmed.toLowerCase().includes("done")) {
        return `${trimmed}\n\nBoard: Arduino UNO (Simulation)`;
      }
      return `${trimmed}\n\nBoard: Arduino UNO`;
    }
    return `Board: Arduino UNO (Simulation)`;
  }
}