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 | 14x 14x 14x 1x 1x 1x 69x 69x 69x 40x 14x 26x 26x 26x 7x 7x 19x 19x 19x 26x 26x 26x 26x 26x 7x 7x 7x 7x 19x 19x 19x 19x 19x 19x 19x 19x 19x 20x 20x 13x 13x 7x 7x 2x 5x 5x 5x 3x 20x 20x 20x 20x 6x 6x 6x 14x 14x 14x 14x 14x 14x 14x 1x 1x 1x 1x 13x 40x 40x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 5x 5x 19x 19x 19x 19x 19x 19x 5x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 1x 26x | /**
* Local Compiler
*
* Handles local compilation of Arduino sketches using g++.
* Used as fallback when Docker is not available.
*/
import { chmod, mkdir, access, rm, stat } from "node:fs/promises";
import { dirname, join } from "node:path";
import { ChildProcess, spawn } from "node:child_process";
import { Logger } from "@shared/logger";
import { ProcessExecutor } from "./process-executor";
/**
* Custom error type for compiler-specific failures
*/
class CompilerError extends Error {
readonly isCompilerError = true;
constructor(message: string) {
super(message);
this.name = "CompilerError";
}
}
export class LocalCompiler {
private readonly logger = new Logger("LocalCompiler");
private compileTimeoutMs = 20000; // 20 seconds
private readonly processExecutor: ProcessExecutor;
constructor() {
this.processExecutor = new ProcessExecutor();
}
/**
* Public helper so callers can detect if a compile process is currently running
*/
get isBusy(): boolean {
return this.processExecutor.isBusy;
}
/**
* Compiles a sketch file using g++
*
* @param sketchFile - Path to the .cpp file
* @param exeFile - Path for the output executable
* @throws CompilerError if compilation fails
*/
private static readonly CLI_CACHE_PATH = join(process.cwd(), "cache", "cores", "uno-cli-feedback.a");
async compile(
sketchFile: string,
exeFile: string,
coreArchive?: string,
onProcess?: (proc: ChildProcess) => void,
): Promise<void> {
const config = this._detectEnvironmentConfig();
this.logger.debug(`[LocalCompiler] compile() invoked (testEnv=${config.usingTestEnv}, coverage=${config.coverageActive}) sketch=${sketchFile}`);
// In test environments with mocked spawn, run lightweight fake compile
if (config.usingTestEnv && config.spawnIsMock) {
await this._handleMockCompilation(exeFile, onProcess);
return;
}
// Real compilation: setup, CLI, then g++
await this.setupOutputDirectory(exeFile);
await this._checkAndRunArduinoCli(sketchFile, config.usingTestEnv, config.coverageActive);
await this._compileWithRetry(sketchFile, exeFile, coreArchive, onProcess);
}
private _detectEnvironmentConfig(): {
usingTestEnv: boolean;
coverageActive: boolean;
spawnIsMock: boolean;
} {
const usingTestEnv = process.env.NODE_ENV === "test";
const coverageActive = !!process.env.NODE_V8_COVERAGE || !!process.env.VITEST_COVERAGE;
Iif (coverageActive) {
this.logger.debug("[LocalCompiler] coverage mode detected – skipping Arduino CLI and extending timeout");
this.compileTimeoutMs = 60000;
}
const spawnIsMock = (spawn as any).mock !== undefined;
return { usingTestEnv, coverageActive, spawnIsMock };
}
private async _handleMockCompilation(exeFile: string, onProcess?: (proc: ChildProcess) => void): Promise<void> {
const result = await this.processExecutor.execute("echo", ["test"], {
timeout: this.compileTimeoutMs,
onProcess,
});
Iif (result.error) {
throw new CompilerError(result.stderr || `Compiler exit ${result.code}`);
}
try {
await this.makeExecutable(exeFile);
} catch {
// Ignore – tests don't care if chmod fails
}
}
private async _checkAndRunArduinoCli(sketchFile: string, usingTestEnv: boolean, coverageActive: boolean): Promise<void> {
let skipCli = false;
Iif (!usingTestEnv) {
try {
const cliCacheStat = await stat(LocalCompiler.CLI_CACHE_PATH);
skipCli = cliCacheStat.size > 0;
} catch {
skipCli = false;
}
}
Iif (skipCli) {
this.logger.debug("[LocalCompiler] skipping arduino-cli because CLI cache exists");
} else {
const buildDir = join(dirname(sketchFile), "build");
const sketchDir = dirname(sketchFile);
await this.runArduinoCli(sketchDir, buildDir, coverageActive);
await this.updateCliCache(buildDir);
}
}
private async _compileWithRetry(
sketchFile: string,
exeFile: string,
coreArchive?: string,
onProcess?: (proc: ChildProcess) => void,
): Promise<void> {
let lastError: Error | null = null;
for (let attempt = 1; attempt <= 2; attempt++) {
try {
await this.runCompilation(sketchFile, exeFile, attempt, coreArchive, onProcess);
await this.makeExecutable(exeFile);
return; // Success
} catch (err) {
lastError = err instanceof Error ? err : new Error(String(err));
if (lastError instanceof CompilerError || attempt >= 2) {
break;
}
Eif (attempt < 2) {
this.logger.warn(`Compilation attempt ${attempt} failed, retrying... (${lastError.message})`);
await new Promise<void>(r => setTimeout(r, 500));
}
}
}
if (lastError) throw lastError;
}
/**
* Internal method to run the actual g++ compilation
*/
private async runCompilation(sketchFile: string, exeFile: string, attempt: number, coreArchive?: string, onProcess?: (proc: ChildProcess) => void): Promise<void> {
this.logger.debug("[LocalCompiler] runCompilation spawning g++");
// Verify output directory exists
const outDir = dirname(exeFile);
try {
await access(outDir);
} catch {
const raceErr = new Error(
`[RaceCondition] Output directory vanished before g++ spawn: ${outDir}`,
);
this.logger.error(raceErr.message);
throw raceErr;
}
// Verify sketch file exists before spawn
try {
await access(sketchFile);
} catch {
const raceErr = new Error(
`[RaceCondition] sketch file vanished before g++ spawn: ${sketchFile}`,
);
this.logger.error(raceErr.message);
throw raceErr;
}
const args = [sketchFile];
Iif (coreArchive) {
args.push(coreArchive);
}
args.push("-o", exeFile, "-pthread");
// Use ProcessExecutor for safe, unified compilation handling
const result = await this.processExecutor.execute("g++", args, {
timeout: this.compileTimeoutMs,
detached: true,
stdio: "pipe",
onProcess,
});
if (result.error || result.code !== 0) {
const cleanedError = this.cleanCompilerErrors(result.stderr || "");
const errorMsg = `Compiler error (Code ${result.code}, attempt ${attempt}): ${cleanedError}`;
this.logger.error(errorMsg);
throw new CompilerError(cleanedError);
}
this.logger.info(`Compilation successful: ${exeFile}`);
}
/**
* Makes the compiled executable file executable (chmod +x)
*/
async makeExecutable(exeFile: string): Promise<void> {
await chmod(exeFile, 0o755);
this.logger.debug(`Made executable: ${exeFile}`);
}
/**
* Sets up output directory: checks existence, creates if needed, removes stale exe
*/
private async setupOutputDirectory(exeFile: string): Promise<void> {
const outputDir = dirname(exeFile);
try {
await access(outputDir);
this.logger.debug(`Output directory exists: ${outputDir}`);
} catch {
this.logger.info(`Output directory missing, creating: ${outputDir}`);
try {
await mkdir(outputDir, { recursive: true, mode: 0o755 });
const dirStat = await stat(outputDir);
if (!dirStat.isDirectory()) {
throw new Error(`Created path is not a directory: ${outputDir}`);
}
this.logger.debug(`Created output directory with proper permissions: ${outputDir}`);
} catch (error_) {
const msg = error_ instanceof Error ? error_.message : String(error_);
this.logger.error(`Failed to create output directory: ${msg}`);
throw error_;
}
}
try {
await rm(exeFile, { force: true });
this.logger.debug(`Cleaned up stale executable: ${exeFile}`);
} catch {
// Ignore - file might not exist yet
}
}
/**
* Executes Arduino CLI compilation step
* Returns void on success, logs warnings on failure (not fatal)
*/
private async runArduinoCli(sketchDir: string, buildDir: string, coverageActive: boolean): Promise<void> {
Iif (coverageActive) {
this.logger.debug("[LocalCompiler] coverage mode – bypassing arduino-cli step");
return;
}
// Ensure the CLI build path and key subdirectories exist
try {
await mkdir(join(buildDir, "sketch"), { recursive: true });
await mkdir(join(buildDir, "core"), { recursive: true });
} catch {}
// Create minimal Arduino sketch for CLI in fresh folder
const cliTemp = join(sketchDir, "cli-temp");
let cliTempReady = false;
try {
const { writeFile } = await import("node:fs/promises");
await mkdir(cliTemp, { recursive: true });
const cliSketch = join(cliTemp, "cli-temp.ino");
await writeFile(cliSketch, "void setup(){}\nvoid loop(){}\n");
cliTempReady = true;
} catch {}
Iif (!cliTempReady) return;
try {
const cliArgs = [
"compile",
"--fqbn",
"arduino:avr:uno",
"--build-path",
buildDir,
cliTemp,
];
this.logger.debug(`spawning arduino-cli ${cliArgs.join(" ")}`);
const result = await this.processExecutor.execute("arduino-cli", cliArgs, {
timeout: this.compileTimeoutMs,
detached: true,
stdio: "pipe",
});
if (result.error) {
throw result.error;
}
} catch (err) {
this.logger.warn(`arduino-cli step failed: ${err instanceof Error ? err.message : err}`);
} finally {
try {
await rm(cliTemp, { recursive: true, force: true });
} catch {}
}
}
/**
* Updates CLI cache with core.a if it's newer than cached version
*/
private async updateCliCache(buildDir: string): Promise<void> {
try {
const suspect = join(buildDir, "core", "core.a");
// Check if suspect exists
try {
await stat(suspect);
} catch {
return;
}
const cachePath = LocalCompiler.CLI_CACHE_PATH;
const suspectStat = await stat(suspect);
let needCopy = false;
// Check if cache exists and compare times
try {
const [cacheStat] = await Promise.allSettled([stat(cachePath)]);
Iif (cacheStat.status === "fulfilled") {
needCopy = suspectStat.mtimeMs > cacheStat.value.mtimeMs;
} else {
needCopy = true;
}
} catch {
needCopy = true;
}
Eif (needCopy) {
const { randomUUID: _cacheUUID } = await import("node:crypto");
const tmpCachePath = cachePath + "." + _cacheUUID() + ".tmp";
try {
const fs = await import("node:fs");
await fs.promises.copyFile(suspect, tmpCachePath);
await fs.promises.rename(tmpCachePath, cachePath);
const newCacheStat = await stat(cachePath);
const sizeKB = (newCacheStat.size / 1024).toFixed(1);
this.logger.info(`[LocalCompiler] CLI cache saved (${sizeKB} KB)`);
} catch (error_) {
const tmpCachePath = cachePath + "." + _cacheUUID() + ".tmp";
try { await rm(tmpCachePath, { force: true }); } catch {}
this.logger.warn(`[LocalCompiler] CLI cache write failed: ${
error_ instanceof Error ? error_.message : error_}`);
}
}
} catch {}
}
/**
* Cleans up compiler error messages for user display
* Removes temporary paths and makes errors more readable
*/
private cleanCompilerErrors(errors: string): string {
return errors
.replaceAll('/sandbox/sketch.cpp', "sketch.ino") // Docker path
.replaceAll(/\/[^\s:/]+(?:\/[^\s:/]+)*\/temp\/[a-f0-9-]+\/sketch\.cpp/gi, "sketch.ino") // Local temp path
.replaceAll('sketch.cpp', "sketch.ino") // Generic .cpp references
.trim();
}
/**
* Kill any active compiler/CLI process
*/
kill(): void {
this.processExecutor.kill("SIGKILL");
}
}
|