worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Hooks names and purpose are not optimal

Open ortonomy opened this issue 8 months ago • 6 comments

Ah @max-sixty - I feel like I wasn't being clear in my previous issue: https://github.com/max-sixty/worktrunk/issues/1571

  • my point was that copying caches should be done before installing dependencies. My current set up:
[post-create]
copy = "wt step copy-ignored"
install = "mkdir -p .tmp && pnpm install > .tmp/wt-install.log 2>&1; rc=$?; cat .tmp/wt-install.log; exit $rc"

(ignore my overcomplicated pnpm install - this is just because I'm using NX mono repo and WT hangs due to the waiting for EOF from the postinstall NX hook)

The post-start copy of caches doesn't make any real sense...

Image

I also don't love post-start as a name - starting what? It only seems to fire once (not on every switch)?

ortonomy avatar Mar 17 '26 06:03 ortonomy

let's continue in the other issue

max-sixty avatar Mar 17 '26 06:03 max-sixty

Thanks for the follow-up and for clarifying what you meant in #1571!

On copy-ignored placement: You're right that the docs default to showing copy-ignored in post-start, which is misleading for the most common Node.js workflow. When post-create runs npm install (or pnpm install), the cache copy needs to happen before install — otherwise the copy is pointless. Your setup with both in post-create is the correct approach:

[post-create]
copy = "wt step copy-ignored"
install = "pnpm install"

The current docs do have a note saying "Use post-create instead if subsequent hooks or --execute command need the copied files immediately" — but leading with post-start as the default example buries that. For Node.js projects (likely the most common use case), the post-create placement should be the primary recommendation, not a footnote.

On the post-start name: The mental model is "post-create but background" — it fires once after post-create completes, runs detached, and is meant for tasks that don't need to block (dev servers, large copies where timing doesn't matter). The name "start" refers to "starting work on this worktree." It could be clearer, but renaming would be a breaking change to existing configs — so any rename would need a deprecation path.

I'll leave both points for a maintainer to evaluate. The docs improvement (leading with post-create for copy+install workflows) seems straightforward; the naming question is a bigger decision.

worktrunk-bot avatar Mar 17 '26 06:03 worktrunk-bot

@worktrunk-bot if this doesn't work with npm, then we should indeed switch away from npm there. Can you explain why it doesn't work, and propose a different server command to run in that example; and maybe make an npm pattern that put wt step ignored in the `post-create.

but the bar is high for post-create, because it blocks creating the worktree, so we should be quite confident

max-sixty avatar Mar 17 '26 06:03 max-sixty

The name "start" refers to "starting work on this worktree." It could be clearer, but renaming would be a breaking change to existing configs — so any rename would need a deprecation path.

but post-switch is also background and also starting working on this worktree 🤔

ortonomy avatar Mar 17 '26 06:03 ortonomy

@max-sixty The current post-start example shows copy-ignored alongside npm run dev, but post-create has npm ci. Since post-start runs after post-create, the node_modules/ copy hasn't happened yet when npm ci runs from scratch — so the install can't reuse cached packages.

The fix is straightforward: move copy-ignored to post-create, before install:

[post-create]
copy = "wt step copy-ignored"
install = "npm ci"

[post-start]
server = "npm run dev -- --port {{ branch | hash_port }}"

The server command (npm run dev) is fine in post-start — it's a long-running background process, exactly what post-start is for. The issue was only with copy being there when npm ci depends on having node_modules/ already present.

For the guidance: post-create is the right default for copy-ignored when subsequent hooks need the files (Node.js being the most common case). post-start is better when the copy is large and nothing depends on it immediately (Rust target/, large build caches). Yes, post-create blocks — but copying node_modules/ with reflink is fast (~20s for large projects), and npm ci from scratch without a cache takes longer than that saved time.

PR: #1581

@ortonomy Fair point on post-switch — it is also background and also fires when "starting work on a worktree." The distinction is scope: post-start fires once (creation only), post-switch fires every time (including creation). As max-sixty noted in #1571, the naming has prior art from devcontainers.

worktrunk-bot avatar Mar 17 '26 06:03 worktrunk-bot

let's continue in the other issue

Sorry about that.

ortonomy avatar Mar 17 '26 06:03 ortonomy