When behavior depends on environment variables or config files (e.g. git’s core.hooksPath, GIT_DIR, GIT_CONFIG*), never assume an in-process reimplementation of the resolution rules is complete. Config can be layered and overridden through many sources — env vars, ~/%(prefix)% expansion, system/global/local config, -c flags, wrapper scripts — and a native reimplementation will silently diverge from the real tool on edge cases.
Apply these rules:
process.env. Callers may filter environment (e.g. stripping GIT_DIR-family vars) before passing it down, so accept and thread an explicit env parameter through every subprocess/config lookup.GIT_CONFIG_PARAMETERS, GIT_CONFIG_GLOBAL), and in those cases delegate to the authoritative external tool (spawn the real binary) instead of guessing, rather than only partially supporting such overrides./c/... paths on Windows, but accept legitimate UNC paths like //server/share/...).Example pattern:
async function resolveConfigPath(cwd, env, signal) {
if (hasUnsupportedOverrides(env)) {
// Defer to the authoritative tool when env/config overrides exceed in-process support.
return await askRealTool(cwd, env, signal);
}
return inProcessResolve(cwd, env);
}