Skip to content

Commit 4da5698

Browse files
committed
🤖 feat: Add comprehensive logging to init hook execution
- Added log.debug() for init hook detection and script execution - Added log.info() for init hook start and completion with exit codes - Added log.error() for init hook failures - Added logging to BashExecutionService for streaming command execution - Added process error logging for bash execution failures This improves debuggability when init hooks don't work as expected.
1 parent d0170f2 commit 4da5698

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/services/bashExecutionService.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { spawn } from "child_process";
22
import type { ChildProcess } from "child_process";
3+
import { log } from "./log";
34

45
/**
56
* Configuration for bash execution
@@ -114,6 +115,11 @@ export class BashExecutionService {
114115
config: BashExecutionConfig,
115116
callbacks: StreamingCallbacks
116117
): DisposableProcess {
118+
log.debug(`BashExecutionService: Executing streaming command in ${config.cwd}`);
119+
log.debug(
120+
`BashExecutionService: Script: ${script.substring(0, 100)}${script.length > 100 ? "..." : ""}`
121+
);
122+
117123
const spawnCommand = config.niceness !== undefined ? "nice" : "bash";
118124
const spawnArgs =
119125
config.niceness !== undefined
@@ -130,6 +136,8 @@ export class BashExecutionService {
130136
detached: config.detached ?? true,
131137
});
132138

139+
log.debug(`BashExecutionService: Spawned process with PID ${child.pid}`);
140+
133141
// Line-by-line streaming with incremental buffers
134142
let outBuf = "";
135143
let errBuf = "";
@@ -160,6 +168,7 @@ export class BashExecutionService {
160168
});
161169

162170
child.on("close", (code: number | null) => {
171+
log.debug(`BashExecutionService: Process exited with code ${code}`);
163172
// Flush any remaining partial lines
164173
if (outBuf.trim().length > 0) {
165174
callbacks.onStdout(outBuf);
@@ -170,6 +179,10 @@ export class BashExecutionService {
170179
callbacks.onExit(code ?? 0);
171180
});
172181

182+
child.on("error", (error: Error) => {
183+
log.error(`BashExecutionService: Process error:`, error);
184+
});
185+
173186
return new DisposableProcess(child);
174187
}
175188
}

src/services/ipcMain.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "node:assert/strict";
22
import type { BrowserWindow, IpcMain as ElectronIpcMain } from "electron";
33
import { spawn, spawnSync } from "child_process";
4+
import * as fs from "fs";
45
import * as fsPromises from "fs/promises";
56
import * as path from "path";
67
import type { Config, ProjectConfig } from "@/config";
@@ -65,15 +66,22 @@ export class IpcMain {
6566
// Non-blocking fire-and-forget; errors are reported via chat stream
6667
try {
6768
const hookPath = path.join(params.projectPath, ".cmux", "init");
68-
// Check if hook exists
69+
70+
log.debug(`Checking for init hook at ${hookPath}`);
71+
72+
// Check if hook exists and is executable
6973
const exists = await fsPromises
70-
.access(hookPath)
74+
.access(hookPath, fs.constants.X_OK)
7175
.then(() => true)
7276
.catch(() => false);
77+
7378
if (!exists) {
79+
log.debug(`No init hook found at ${hookPath}`);
7480
return; // Nothing to do
7581
}
7682

83+
log.info(`Running init hook for workspace ${params.workspaceId}: ${hookPath}`);
84+
7785
const session = this.getOrCreateSession(params.workspaceId);
7886

7987
// Emit start event via chat stream
@@ -108,6 +116,9 @@ export class IpcMain {
108116
});
109117
},
110118
onExit: (exitCode) => {
119+
log.info(
120+
`Init hook completed for workspace ${params.workspaceId} with exit code ${exitCode}`
121+
);
111122
session.emitChatEvent({
112123
type: "init-end",
113124
exitCode,
@@ -117,6 +128,7 @@ export class IpcMain {
117128
}
118129
);
119130
} catch (error) {
131+
log.error(`Failed to run init hook for workspace ${params.workspaceId}:`, error);
120132
const session = this.getOrCreateSession(params.workspaceId);
121133
session.emitChatEvent({
122134
type: "init-output",

0 commit comments

Comments
 (0)