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.

chore(devex): stop the worktree helper doing redundant work (#92290)

Julian Bezf3f9d253a3db
diff --git a/.flox/env/on-activate.sh b/.flox/env/on-activate.sh
index f0aec9ae4144..8d57247896de 100755
--- a/.flox/env/on-activate.sh
+++ b/.flox/env/on-activate.sh
@@ -305,10 +305,15 @@ _PNPM_CURRENT=$(_sha256_file "$_PNPM_LOCK")
_UV_CURRENT=$(_sha256_file "$_UV_LOCK")
_PHROCS_CURRENT=$(_sha256_file "$_PHROCS_BIN")
+# A stamp says the lockfile matches the bake, not that this checkout has the
+# outputs. A fresh worktree matches every stamp and has neither, so require the
+# local tree too or activation skips the install and leaves the worktree empty.
+# These probe for what a real install leaves behind rather than a bare directory,
+# since both tools create the parent early and populate it afterwards.
_PNPM_SKIP=0
-[[ -n "$_PNPM_BAKED" && -n "$_PNPM_CURRENT" && "$_PNPM_BAKED" == "$_PNPM_CURRENT" ]] && _PNPM_SKIP=1
+[[ -n "$_PNPM_BAKED" && -n "$_PNPM_CURRENT" && "$_PNPM_BAKED" == "$_PNPM_CURRENT" && -d "$FLOX_ENV_PROJECT/node_modules/.pnpm" ]] && _PNPM_SKIP=1
_UV_SKIP=0
-[[ -n "$_UV_BAKED" && -n "$_UV_CURRENT" && "$_UV_BAKED" == "$_UV_CURRENT" ]] && _UV_SKIP=1
+[[ -n "$_UV_BAKED" && -n "$_UV_CURRENT" && "$_UV_BAKED" == "$_UV_CURRENT" && -x "$UV_PROJECT_ENVIRONMENT/bin/python" ]] && _UV_SKIP=1
_PHROCS_SKIP=0
[[ -n "$_PHROCS_BAKED" && -n "$_PHROCS_CURRENT" && "$_PHROCS_BAKED" == "$_PHROCS_CURRENT" ]] && _PHROCS_SKIP=1
diff --git a/.husky/post-checkout b/.husky/post-checkout
index 6ccf8a3d2ca2..30564f32f794 100755
--- a/.husky/post-checkout
+++ b/.husky/post-checkout
@@ -7,8 +7,9 @@
# Detect worktree: .git is a file (not a directory) pointing to the main repo
[ -f .git ] || exit 0
-# 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
+# Borrow the main clone's node_modules when lockfiles match, so the commit
+# hooks run without a local install. The borrow is a PATH export that dies
+# with this process, so it does not populate the worktree. 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.
@@ -19,7 +20,11 @@ if [ -f "$HOOK_DIR/../bin/helpers/worktree-borrow.sh" ]; then
fi
if [ "$POSTHOG_BORROW_NODE" = "1" ]; then
- echo "husky: worktree detected, borrowing node_modules from main clone"
+ # Being install-free is the point of a light worktree, but "borrowing
+ # node_modules" reads as "deps are here". They are not: the borrow is a PATH
+ # export scoped to one hook process, so say what the worktree can do.
+ echo "husky: light worktree. Commit hooks borrow the main clone's toolchain."
+ echo "husky: it has no node_modules or venv of its own. Run 'flox activate' to typecheck, test or run the app."
else
if ! command -v pnpm > /dev/null 2>&1; then
echo "husky: pnpm not found, skipping worktree bootstrap"
diff --git a/bin/helpers/worktree-borrow.sh b/bin/helpers/worktree-borrow.sh
index 3bab9418f332..3ba0abdbf63b 100644
--- a/bin/helpers/worktree-borrow.sh
+++ b/bin/helpers/worktree-borrow.sh
@@ -58,7 +58,6 @@ posthog_worktree_borrow() {
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
@@ -79,7 +78,6 @@ posthog_worktree_borrow() {
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
diff --git a/bin/phw b/bin/phw
index 55f4a6c553ae..d29e81d07b89 100644
--- a/bin/phw
+++ b/bin/phw
@@ -190,4 +190,3 @@ elif [ -n "$ZSH_VERSION" ]; then
fi
fi
-echo "PostHog worktree helper loaded. Use 'phw' command."
diff --git a/bin/posthog-worktree b/bin/posthog-worktree
index fcc5a99ab694..4a5578cd8589 100755
--- a/bin/posthog-worktree
+++ b/bin/posthog-worktree
@@ -39,39 +39,9 @@ done
# Worktree base directory (configurable via POSTHOG_WORKTREE_BASE)
WORKTREE_BASE="${POSTHOG_WORKTREE_BASE:-${HOME}/.worktrees/posthog}"
-# Copy helper that uses copy-on-write clones when available (APFS/Btrfs)
-# Returns 0 on success, 1 if CoW not available and require_cow is true
-clone_path() {
- local source_path="$1"
- local destination_path="$2"
- local require_cow="${3:-false}" # If true, fail when CoW unavailable
- local cp_args=("-a")
-
- if [[ "$(uname -s)" == "Darwin" ]]; then
- # macOS: cp -c fails if cloning not supported
- if cp -c "${cp_args[@]}" "$source_path" "$destination_path" 2>/dev/null; then
- return 0
- fi
- else
- # Linux: --reflink=auto silently falls back to regular copy,
- # so use --reflink=always when CoW is required
- if [[ "$require_cow" == "true" ]]; then
- if cp --reflink=always "${cp_args[@]}" "$source_path" "$destination_path" 2>/dev/null; then
- return 0
- fi
- else
- if cp --reflink=auto "${cp_args[@]}" "$source_path" "$destination_path" 2>/dev/null; then
- return 0
- fi
- fi
- fi
-
- # CoW not available or failed - either fall back to regular copy or skip
- if [[ "$require_cow" == "true" ]]; then
- return 1
- fi
-
- cp "${cp_args[@]}" "$source_path" "$destination_path"
+# Copy a single untracked config file into the worktree.
+copy_config_file() {
+ cp -a "$1" "$2"
}
# Clean up a stale worktree directory left over from a previous failed run.
@@ -266,18 +236,23 @@ setup_worktree_environment() {
# Setup Flox environment for this worktree
cd "${worktree_path}"
- # Copy .flox env files and .envrc for direnv
+ # Overwrite the checked-out Flox environment with the main clone's copy.
+ # These files are tracked, so `git worktree add` already wrote the target
+ # branch's versions -- and that is the point: `flox activate --trust` below
+ # runs on-activate.sh as a bash hook, after this function has copied the
+ # developer's .env files in. For `phw pr` the branch is someone else's, so
+ # taking its hook would execute an untrusted contributor's shell with those
+ # secrets present. Copy the trusted versions over it.
mkdir -p "${worktree_path}/.flox/env"
- clone_path "${main_repo}/.flox/env/manifest.toml" "${worktree_path}/.flox/env/"
+ copy_config_file "${main_repo}/.flox/env/manifest.toml" "${worktree_path}/.flox/env/"
[[ -f "${main_repo}/.flox/env/on-activate.sh" ]] && \
- clone_path "${main_repo}/.flox/env/on-activate.sh" "${worktree_path}/.flox/env/"
- clone_path "${main_repo}/.envrc" "${worktree_path}/"
+ copy_config_file "${main_repo}/.flox/env/on-activate.sh" "${worktree_path}/.flox/env/"
# Copy .vscode/settings.json if it exists
if [[ -f "${main_repo}/.vscode/settings.json" ]]; then
print_color blue "Copying .vscode/settings.json…"
mkdir -p "${worktree_path}/.vscode"
- clone_path "${main_repo}/.vscode/settings.json" "${worktree_path}/.vscode/"
+ copy_config_file "${main_repo}/.vscode/settings.json" "${worktree_path}/.vscode/"
fi
# Copy .env and .env.* files if they exist
@@ -287,61 +262,36 @@ setup_worktree_environment() {
env_files_found=true
print_color blue "Copying .env files…"
fi
- clone_path "${env_file}" "${worktree_path}/"
+ copy_config_file "${env_file}" "${worktree_path}/"
done < <(find "${main_repo}" -maxdepth 1 \( -name ".env" -o -name ".env.*" \) -type f -print0)
- # Clone flox cache venv using copy-on-write if available (the real Python venv used by flox)
- if [[ -d "${main_repo}/.flox/cache/venv" && ! -e "${worktree_path}/.flox/cache/venv" ]]; then
- mkdir -p "${worktree_path}/.flox/cache"
- if clone_path "${main_repo}/.flox/cache/venv" "${worktree_path}/.flox/cache/venv" true; then
- print_color blue "Cloned .flox/cache/venv (copy-on-write)"
- # Fix VIRTUAL_ENV path in activate scripts — the cloned venv has the
- # source repo's path hardcoded, which puts the wrong bin/ on PATH.
- local venv_bin="${worktree_path}/.flox/cache/venv/bin"
- local old_venv="${main_repo}/.flox/cache/venv"
- local new_venv="${worktree_path}/.flox/cache/venv"
- local sed_inplace=(-i)
- [[ "$(uname -s)" == "Darwin" ]] && sed_inplace=(-i '')
- for activate_script in "${venv_bin}"/activate*; do
- [[ -f "$activate_script" ]] && LC_ALL=C sed "${sed_inplace[@]}" "s|${old_venv}|${new_venv}|g" "$activate_script"
- done
- print_color blue "Patched venv activate scripts for worktree path"
- else
- print_color yellow "Skipping venv copy (CoW not available) — flox activate will create it"
- fi
- fi
-
- # Clone node_modules using copy-on-write if available
- if [[ -d "${main_repo}/node_modules" && ! -e "${worktree_path}/node_modules" ]]; then
- if clone_path "${main_repo}/node_modules" "${worktree_path}/node_modules" true; then
- print_color blue "Cloned node_modules (copy-on-write)"
- else
- print_color yellow "Skipping node_modules copy (CoW not available) — pnpm install will create it"
- fi
- fi
-
- # Fix rdkafka dylib paths for worktree portability (macOS only)
- if [[ "$(uname -s)" == "Darwin" && -d "${worktree_path}/node_modules" ]]; then
- (cd "${worktree_path}" && bin/fix-rdkafka-paths)
- fi
+ # Dependencies are not copied here. `flox activate` below installs them, and
+ # measuring both showed a copy-on-write clone of node_modules is slower than
+ # `pnpm install` and uses more disk, because pnpm materializes only what the
+ # lockfile names while the clone carries whatever accumulated in the source.
+ # Installing also keeps first-party workspace links and editable installs
+ # pointing at this worktree instead of the main clone.
# Initialize Flox in the new worktree
cd "${worktree_path}"
print_color blue "Initializing Flox environment (this will share packages from cache)…"
- # Check if direnv is installed and set up
+ # Arm direnv for later `cd`s, when it is installed.
if command_exists direnv; then
print_color blue "Detected direnv - allowing automatic activation…"
# Clean up stale environment variables before direnv activation
env -u VIRTUAL_ENV -u PYTHONPATH -u CONDA_DEFAULT_ENV direnv allow .
- # direnv will automatically activate flox and run uv sync
- else
- # Manual activation if direnv not present
- print_color blue "Installing Python dependencies via uv sync (this may take a minute)…"
- # Clean up any stale environment variables that might conflict
- unset VIRTUAL_ENV PYTHONPATH CONDA_DEFAULT_ENV
- flox activate --trust -- uv sync
fi
+
+ # Activate regardless. `direnv allow` only adds .envrc to the allow list, it
+ # does not evaluate it, and the shell hook may not be configured at all. The
+ # environment's on-activate hook is what installs the Python and Node
+ # dependencies, so without this the worktree would have none and setup would
+ # still report success.
+ print_color blue "Installing dependencies via flox activate (this may take a minute)…"
+ # Clean up any stale environment variables that might conflict
+ unset VIRTUAL_ENV PYTHONPATH CONDA_DEFAULT_ENV
+ flox activate --trust -- true
success "✅ Worktree setup complete!"
echo ""
diff --git a/docs/published/handbook/engineering/flox-multi-instance-workflow.md b/docs/published/handbook/engineering/flox-multi-instance-workflow.md
index 54f4561a1a91..b337c446beab 100644
--- a/docs/published/handbook/engineering/flox-multi-instance-workflow.md
+++ b/docs/published/handbook/engineering/flox-multi-instance-workflow.md
@@ -24,6 +24,8 @@ This guide explains how to create isolated PostHog development environments usin
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.
+- The borrow lasts for one hook process. The worktree gets no `node_modules` or venv of its own, so editors, typecheck and tests need `flox activate` first.
+- Borrowed tooling runs the main clone's copy of first-party code. `hogli` and `@posthog/*` resolve there rather than to your branch, because editable installs and workspace links point at whichever clone ran the install. Run `flox activate` in the worktree before changing `hogli` or a workspace package.
- 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.