MCPcopy
hub / github.com/codeaashu/claude-code / onCwdChangedForHooks

Function onCwdChangedForHooks

src/utils/hooks/fileChangedWatcher.ts:133–175  ·  view source on GitHub ↗
(
  oldCwd: string,
  newCwd: string,
)

Source from the content-addressed store, hash-verified

131}
132
133export async function onCwdChangedForHooks(
134 oldCwd: string,
135 newCwd: string,
136): Promise<void> {
137 if (oldCwd === newCwd) return
138
139 // Re-evaluate from the current snapshot so mid-session hook changes are picked up
140 const config = getHooksConfigFromSnapshot()
141 const currentHasEnvHooks =
142 (config?.CwdChanged?.length ?? 0) > 0 ||
143 (config?.FileChanged?.length ?? 0) > 0
144 if (!currentHasEnvHooks) return
145 currentCwd = newCwd
146
147 await clearCwdEnvFiles()
148 const hookResult = await executeCwdChangedHooks(oldCwd, newCwd).catch(e => {
149 const msg = errorMessage(e)
150 logForDebugging(`CwdChanged hook failed: ${msg}`, {
151 level: 'error',
152 })
153 notifyCallback?.(msg, true)
154 return {
155 results: [] as HookOutsideReplResult[],
156 watchPaths: [] as string[],
157 systemMessages: [] as string[],
158 }
159 })
160 dynamicWatchPaths = hookResult.watchPaths
161 dynamicWatchPathsSorted = hookResult.watchPaths.slice().sort()
162 for (const msg of hookResult.systemMessages) {
163 notifyCallback?.(msg, false)
164 }
165 for (const r of hookResult.results) {
166 if (!r.succeeded && r.output) {
167 notifyCallback?.(r.output, true)
168 }
169 }
170
171 // Re-resolve matcher paths against the new cwd
172 if (initialized) {
173 restartWatching()
174 }
175}
176
177function dispose(): void {
178 if (watcher) {

Callers 1

execFunction · 0.85

Calls 6

clearCwdEnvFilesFunction · 0.85
executeCwdChangedHooksFunction · 0.85
logForDebuggingFunction · 0.85
restartWatchingFunction · 0.85
errorMessageFunction · 0.50

Tested by

no test coverage detected