feat(serve): use deterministic port when running inside a git worktree - #5140
Open
DavideCarvalho wants to merge 1 commit into
Open
feat(serve): use deterministic port when running inside a git worktree#5140DavideCarvalho wants to merge 1 commit into
DavideCarvalho wants to merge 1 commit into
Conversation
Author
|
Moved into #5136 to keep both features in a single PR. |
|
Just pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When running
node ace serveinside a git worktree, the development server now automatically uses a deterministic port derived from the worktree name, so multiple worktrees of the same application can run in parallel without port conflicts.For example, running the same app in
~/worktrees/feature-loginand~/worktrees/feature-checkoutwill automatically bind to different ports, always the same for the same worktree.Why this is needed
Working with multiple git worktrees of the same project is the standard way to develop several features/branches side by side. The problem: every worktree reads the same
.env(or the default port 3333), so startingnode ace servein a second worktree fails withEADDRINUSE— you have to manually change the port (editing.envor exportingPORT) every time, and that change is not shared/predictable across teammates.A deterministic port derived from the worktree name solves this without touching
.env:--no-worktree-port.So the feature exists to answer: "start the dev server in this worktree, on a port that is unique to this worktree, without manual configuration."
How it works
.gitfile (linked worktrees have a.gitfile pointing to the main repo, while the main checkout has a.gitdirectory)..gitfile (walking up when the app lives in a nested directory).basePort + (hash(worktreeName) % 1000), wherebasePortis read from the app dot-env files (PORT) and defaults to3333. The hash is stable, so the same worktree always resolves to the same port.process.env.PORTbefore starting the dev server — the assembler already prefersprocess.env.PORT, so no.envfile is touched (git stays clean).Usage
Changes
commands/serve.ts— worktree detection + port override +--no-worktree-portflag + help textsrc/helpers/worktree.ts—getWorktreeName,getBasePort,computeWorktreePorttests/helpers/worktree.spec.ts— unit tests for the helperstests/commands/serve.spec.ts— integration tests for the serve behaviorNotes
@adonisjs/env(already a dependency of core) and the built-innode:crypto/node:fs.