Claude Code’s DirectoryAdded Hook for Monorepos
This episode explores how Claude Code’s DirectoryAdded hook helps monorepos dynamically load the right environment for newly registered subdirectories, avoiding toolchain mix-ups like using npm in a bun workspace. It also covers workspace trust, hook exit codes, and a few quality-of-life fixes in the latest release.
Chapter 1
Streamline Monorepos with Claude Codes New DirectoryAdded Hook
Lachlan Reed
So, uh, I was looking at this classic monorepo problem, right? You, you spin up Claude Code in a massive project, and everything is, is sweet until you dynamically load a new subdirectory mid session. Maybe you, you run slash add dir, or, or some SDK register_repo_root control request kicks in. And, and boom, your agent is suddenly completely, like, blind to that local toolchain. It, it has no idea it just walked into a different world.
James Turner
Wait, so why do the usual hooks like SessionStart or CwdChanged not, uh, why do they not just handle that? Like, if the working directory changes, why does it not pick it up?
Lachlan Reed
Ah, well, that is the catch, mate. See, when you add a directory dynamically, the session has already started, right? So SessionStart is completely useless. And, and CwdChanged, well, that only fires if you literally cd to a different path, not when a new root is registered. So the agent is just standing there trying to build a Svelte project using npm, completely unaware that this specific subproject is strictly, uh, strictly a bun setup. I actually had a, a proper nightmare shift once where a global script ran and completely trashed a Svelte workspace lockfile because of this exact blind spot. It, it was a total mess.
James Turner
Oh man, lockfile corruption is the absolute worst. But, okay, so they, they just solved this in version two point one point two one nine, right? They introduced the DirectoryAdded hook?
Lachlan Reed
Spot on. It is a brand new lifecycle hook in your user settings dot json. Basically, the, the moment a new root is registered, this DirectoryAdded hook fires. And if you pair it with, say, direnv and that CLAUDE_ENV_FILE variable, you can automatically load the correct environment variables for that newly added subdirectory, like, instantly.
James Turner
That makes total sense! So the moment slash add dir is called, the hook runs, pulls in the local environment, and Claude suddenly knows, oh, I should be using bun instead of npm here. But wait, what about security? Like, what if I pull down a random open source repo and it has some malicious hook configured in the folder?
Lachlan Reed
Aha, you, you have hit the nail on the head. That is where workspace trust comes in, which, uh, actually got a massive security enforcement boost back in version two point one point two one eight. Claude Code will straight up refuse to run any hooks from folders that you haven't explicitly, you know, granted workspace trust. If it is untrusted, the hook is blocked. And, and speaking of execution, if your hook script fails, you have to be clever with your exit codes. If you exit with, with code two, you are telling the model, hey, this is a blocking error, stop everything. But if you exit zero with some structured JSON, you can pass clean user messages back without, uh, without creating those awful recursive execution loops.
James Turner
Oh, man, that recursive loop is a hilarious mental image. Just Claude trying to fix a broken environment forever, looping on itself. Hey, speaking of quality of life, did you see they also patched that annoying GNU screen copy on select bug in two point one point two one nine?
Lachlan Reed
Oh, mate, finally! No more terminal buffers corrupting into raw base64 garbage when you highlight text. And, and they patched the Vim normal mode left arrow escape issue too. It, it is the little things that keep you sane, honestly.
James Turner
It really is. Though, I mean, it makes you wonder... as these agentic tools get more and more integrated, will we ever get to a point where workspace boundaries and trust are just fully automated? Or are we always going to need a human standing guard at the gate?
Lachlan Reed
That is the, uh, the million dollar question, isn't it? For now, look, I am just happy my lockfiles are safe. Good chatting, mate.
James Turner
Yeah, talk soon.