Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(devex): let light worktrees borrow toolchain from main clone (#70648)

Julian Bezc248a3dd147d
diff --git a/.husky/post-checkout b/.husky/post-checkout
index 7568d7e8f5bd..6ccf8a3d2ca2 100755
--- a/.husky/post-checkout
+++ b/.husky/post-checkout
@@ -7,21 +7,27 @@
# Detect worktree: .git is a file (not a directory) pointing to the main repo
[ -f .git ] || exit 0
-# Bootstrap husky + lint-staged so pre-commit hooks work in worktrees
-# pnpm may come from flox, corepack, brew, etc. — add flox bin to PATH if available
-MAIN_REPO="$(cd "$(dirname "$0")/.." && pwd)"
-if [ -d "$MAIN_REPO/.flox/run" ]; then
- for p in "$MAIN_REPO/.flox/run"/*/bin; do
- [ -d "$p" ] && export PATH="$p:$PATH"
- done
+# Borrow node_modules from the main clone when lockfiles match, so a freshly
+# created worktree doesn't need its own pnpm install at all. Resolve the
+# helper relative to this hook file, not cwd: core.hooksPath may be absolute
+# (hook file lives in the main clone) or relative (worktree's own checkout),
+# and cwd is always the active worktree, which may not contain the helper.
+HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
+if [ -f "$HOOK_DIR/../bin/helpers/worktree-borrow.sh" ]; then
+ . "$HOOK_DIR/../bin/helpers/worktree-borrow.sh"
+ posthog_worktree_borrow node
fi
-if ! command -v pnpm > /dev/null 2>&1; then
- echo "husky: pnpm not found, skipping worktree bootstrap"
- exit 0
-fi
+if [ "$POSTHOG_BORROW_NODE" = "1" ]; then
+ echo "husky: worktree detected, borrowing node_modules from main clone"
+else
+ if ! command -v pnpm > /dev/null 2>&1; then
+ echo "husky: pnpm not found, skipping worktree bootstrap"
+ exit 0
+ fi
-if [ ! -d node_modules/.pnpm ]; then
- echo "husky: worktree detected, bootstrapping with pnpm install ..."
- pnpm install --frozen-lockfile --filter=.
+ if [ ! -d node_modules/.pnpm ]; then
+ echo "husky: worktree detected, bootstrapping with pnpm install ..."
+ pnpm install --frozen-lockfile --filter=.
+ fi
fi
diff --git a/.husky/pre-commit b/.husky/pre-commit
index 4a0219d3e5a0..4ddf0bf3840e 100755
--- a/.husky/pre-commit
+++ b/.husky/pre-commit
@@ -1,6 +1,17 @@
#!/bin/sh
[ -f "$(dirname "$0")/_/husky.sh" ] && . "$(dirname "$0")/_/husky.sh"
+# Borrow node_modules/venv from the main clone in a light worktree, when
+# lockfiles match -- lets this hook run with zero local installs. Resolve the
+# helper relative to this hook file, not cwd: core.hooksPath may be absolute
+# (hook file lives in the main clone) or relative (worktree's own checkout),
+# and cwd is always the active worktree, which may not contain the helper.
+HOOK_DIR="$(cd "$(dirname "$0")" && pwd)"
+if [ -f "$HOOK_DIR/../bin/helpers/worktree-borrow.sh" ]; then
+ . "$HOOK_DIR/../bin/helpers/worktree-borrow.sh"
+ posthog_worktree_borrow
+fi
+
# Skip lint-staged during merges — staged files include the entire merge diff,
# not just your changes, making it slow and pointless
[ -f "$(git rev-parse --git-path MERGE_HEAD 2>/dev/null)" ] && exit 0
diff --git a/bin/helpers/worktree-borrow.sh b/bin/helpers/worktree-borrow.sh
new file mode 100644
index 000000000000..3bab9418f332
--- /dev/null
+++ b/bin/helpers/worktree-borrow.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+# Functions that let a plain `git worktree add` checkout run lint-staged /
+# hogli / ruff / ty without its own node_modules or venv -- by borrowing the
+# main clone's, but only when the worktree's lockfiles match the main clone's
+# exactly. A mismatch means the worktree's branch changed deps, so borrowing
+# would silently run the wrong versions -- callers fall back to today's
+# behavior (local install) in that case.
+#
+# POSIX sh; sourced by the husky hooks and by bin/hogli (bash). Sourcing this
+# file has no side effects -- callers invoke the functions below. Never
+# `exit`s or `set -e`s since it runs inside the caller's shell.
+
+# Echo the main clone root for the repo root given as $1, or nothing when $1
+# is not a linked worktree. Linked worktrees have a `.git` file (pointing at
+# the main clone's .git dir), not a `.git` directory.
+posthog_main_repo() {
+ [ -f "$1/.git" ] || return 0
+ _ph_common="$(git -C "$1" rev-parse --path-format=absolute --git-common-dir 2>/dev/null)" || return 0
+ _ph_main="$(dirname "$_ph_common")"
+ if [ "$_ph_main" != "$1" ]; then
+ echo "$_ph_main"
+ fi
+ return 0
+}
+
+# Echo the first venv that exists under the root given as $1, or nothing.
+posthog_find_venv() {
+ for _ph_venv in "$1/.flox/cache/venv" "$1/.venv" "$1/env"; do
+ if [ -d "$_ph_venv" ]; then
+ echo "$_ph_venv"
+ return 0
+ fi
+ done
+ return 0
+}
+
+# Borrow the main clone's toolchain into the current shell's environment.
+# Must be called with cwd = repo root (the hooks guarantee that). Scope $1 is
+# `node` or `all` (default): post-checkout only needs the node borrow, so it
+# skips the venv work (including the uv.lock compare, which isn't free).
+posthog_worktree_borrow() {
+ _ph_scope="${1:-all}"
+ _ph_main="$(posthog_main_repo "$(pwd)")"
+ [ -n "$_ph_main" ] || return 0
+
+ # flox-installed pnpm/uv/node live under the main clone's .flox/run --
+ # add them to PATH so borrowing works even from GUI git clients that
+ # don't inherit a flox-activated shell.
+ if [ -d "$_ph_main/.flox/run" ]; then
+ for _ph_bin in "$_ph_main/.flox/run"/*/bin; do
+ if [ -d "$_ph_bin" ]; then
+ PATH="$_ph_bin:$PATH"
+ fi
+ done
+ fi
+
+ if [ ! -d node_modules/.pnpm ] && [ -d "$_ph_main/node_modules/.pnpm" ]; then
+ if cmp -s pnpm-lock.yaml "$_ph_main/pnpm-lock.yaml"; then
+ POSTHOG_BORROW_NODE=1
+ PATH="$_ph_main/node_modules/.bin:$PATH"
+ echo "worktree: borrowing node_modules from $_ph_main (lockfiles match)" >&2
+ else
+ echo "worktree: pnpm-lock.yaml differs from $_ph_main, skipping node_modules borrow -- run 'pnpm install --frozen-lockfile --filter=.'" >&2
+ fi
+ fi
+
+ if [ "$_ph_scope" = "all" ] && [ -z "$(posthog_find_venv "$(pwd)")" ]; then
+ _ph_main_venv="$(posthog_find_venv "$_ph_main")"
+ if [ -n "$_ph_main_venv" ]; then
+ if cmp -s uv.lock "$_ph_main/uv.lock"; then
+ VIRTUAL_ENV="$_ph_main_venv"
+ export VIRTUAL_ENV
+ # uv ignores VIRTUAL_ENV -- UV_PROJECT_ENVIRONMENT is what lets
+ # `uv run --no-sync` find the borrowed venv, and UV_NO_SYNC
+ # keeps any other `uv run` from resyncing it against this
+ # worktree's paths (which would corrupt it for the main clone).
+ UV_PROJECT_ENVIRONMENT="$_ph_main_venv"
+ export UV_PROJECT_ENVIRONMENT
+ UV_NO_SYNC=1
+ export UV_NO_SYNC
+ PATH="$_ph_main_venv/bin:$PATH"
+ echo "worktree: borrowing venv from $_ph_main (lockfiles match)" >&2
+ else
+ echo "worktree: uv.lock differs from $_ph_main, skipping venv borrow -- run 'flox activate'" >&2
+ fi
+ fi
+ fi
+
+ export PATH
+ return 0
+}
diff --git a/bin/hogli b/bin/hogli
index a0164daa86bb..8d45ffeed514 100755
--- a/bin/hogli
+++ b/bin/hogli
@@ -8,13 +8,20 @@ REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PYTHON_CMD="python"
if [ -z "${VIRTUAL_ENV:-}" ]; then
- for venv in "$REPO_ROOT/.flox/cache/venv" "$REPO_ROOT/.venv" "$REPO_ROOT/env"; do
- if [ -d "$venv" ]; then
- source "$venv/bin/activate"
- PYTHON_CMD="$venv/bin/python"
- break
- fi
- done
+ # Only local venvs are considered here. Standalone hogli must never adopt
+ # the main clone's venv: commands like `uv sync --active` would rewrite it
+ # against this worktree. Hooks that need borrowing export VIRTUAL_ENV via
+ # bin/helpers/worktree-borrow.sh before hogli runs.
+ FOUND_VENV=""
+ if [ -f "$REPO_ROOT/bin/helpers/worktree-borrow.sh" ]; then
+ source "$REPO_ROOT/bin/helpers/worktree-borrow.sh"
+ FOUND_VENV="$(posthog_find_venv "$REPO_ROOT")"
+ fi
+
+ if [ -n "$FOUND_VENV" ]; then
+ source "$FOUND_VENV/bin/activate"
+ PYTHON_CMD="$FOUND_VENV/bin/python"
+ fi
else
PYTHON_CMD="$VIRTUAL_ENV/bin/python"
fi
diff --git a/bin/posthog-worktree b/bin/posthog-worktree
index 9cfadd07eaa3..fcc5a99ab694 100755
--- a/bin/posthog-worktree
+++ b/bin/posthog-worktree
@@ -348,6 +348,8 @@ setup_worktree_environment() {
echo "To start working:"
print_color blue " cd ${worktree_path}"
print_color blue " bin/start"
+ echo ""
+ print_color yellow "Tip: only running the app needs this full setup — for edit+commit work, plain 'git worktree add' is enough (hooks borrow the main clone's toolchain)."
}
# Function to list all worktrees
@@ -398,6 +400,12 @@ case "$COMMAND" in
help|*)
echo "PostHog Worktree Manager"
echo ""
+ echo "Sets up a full (\"heavy\") worktree with its own flox env and deps for"
+ echo "running the app (bin/start). For quick edits and commits you don't need"
+ echo "this: a plain 'git worktree add <path> <branch>' gives a light worktree"
+ echo "where pre-commit hooks work automatically (the toolchain is borrowed"
+ echo "from the main clone while lockfiles match)."
+ echo ""
echo "Usage: $0 <command> [arguments]"
echo ""
echo "Commands:"
diff --git a/docs/published/handbook/engineering/flox-multi-instance-workflow.md b/docs/published/handbook/engineering/flox-multi-instance-workflow.md
index d37a5812d656..54f4561a1a91 100644
--- a/docs/published/handbook/engineering/flox-multi-instance-workflow.md
+++ b/docs/published/handbook/engineering/flox-multi-instance-workflow.md
@@ -19,6 +19,14 @@ This guide explains how to create isolated PostHog development environments usin
> **Important:** Only one PostHog instance (`hogli start`) can run at a time since they all use the same ports. The workflow focuses on quickly stopping one instance and starting another.
+## Light worktrees
+
+You only need the full setup below when you want to run the app in a worktree. For edit and commit workflows, a plain `git worktree add <path> <branch>` is enough:
+
+- Pre-commit hooks (lint-staged, hogli, ruff, ty) work out of the box: they borrow the main clone's `node_modules` and Python venv automatically, as long as the worktree's `pnpm-lock.yaml` and `uv.lock` match the main clone's.
+- If a lockfile differs, the hooks tell you what to install locally instead.
+- Use `phw` (below) only when you need to run the app in that worktree.
+
## Prerequisites
1. **Flox installed**: https://flox.dev/docs/install-flox/
diff --git a/package.json b/package.json
index a691cf0f2f49..265e60b81d4d 100644
--- a/package.json
+++ b/package.json
@@ -105,7 +105,7 @@
"!(posthog/hogql/grammar/*|posthog/personhog_client/proto/generated/*|products/*/skills/*/scripts/*)*.{py,pyi}": [
"bin/hogli lint:python:fix",
"bin/hogli format:python",
- "uv run ty check"
+ "uv run --no-sync ty check"
],
"*.{md,mdx}": "bin/hogli format:markdown"
},