feat(serve): use deterministic port when running inside a git worktree - #5140
feat(serve): use deterministic port when running inside a git worktree#5140DavideCarvalho wants to merge 3 commits into
Conversation
|
Moved into #5136 to keep both features in a single PR. |
|
Just pass |
Don't think so. In a AI pilled era, more tools a framework is able to give to AI models and harnesses the better. Laravel even has a section of their docs about how Laravel can integrate with your AI tools. Rails has an eval suite so it can benchmark which AI works better with the framework NextJS cli is giving more and more cli commands so AI can get logs, stacktraces without dev intervention That's just examples of where we're heading, and batteries included frameworks like Laravel and Rails are already going towards it |
|
Hello @DavideCarvalho Thanks for the PR. Would you like to update the parts of this PR which uses the recently added helpers inside the Also, I will prefer if we can also expose this command via the |
|
@DavideCarvalho @thetutlage my concern has nothing to do with using AI tools. I just don't think that's behaviour that belongs in the framework it belongs in your development environment. There are many different version control systems. Git is not the only one. It also goes against the logic of the 12 factor application whereby the confuguration should be injected in to the app, not the other way around. If you want your work-trees to run a certain way you should should set them up that way. Not encode this logic in the framework. The reason for that is that there are a million different ways to run the application. for example:
You shouldn't be trying to accomodate every possible way of running an application built on Adonis. That's the whole point of ENV vars. There's nothing in this PR that can't be handled in one line of bash in your worktree setup script.
|
Updating it! |
… port via BaseCommand - Replace the hand-rolled linked worktree detection with the getGitWorktree helper added in @poppinss/utils 7.1.0 - Expose a getWorktreePort method on the BaseCommand (and ListCommand) so any command can resolve the deterministic worktree port - Update the serve command to consume the BaseCommand method - Setup real git worktrees inside the tests instead of faking the .git file, since getGitWorktree shells out to git Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Well a lot of things can be done with just a small bash script, what we have to think - specially the maintainers of adonis - is if we want the framework to have these small bash scripts built About having different db or sharing a single db, I believe that depends on the dev on how it wants to setup the .env on the worktrees. If adonis had something like Laravel Sail this would be even easier because the sail could setup the environment based on the worktree as well |
Done! 🙌
|
# Conflicts: # package-lock.json
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.