Skip to content

Commit 159bceb

Browse files
committed
Update HatchboxManager to avoid copying .env file and setting PORT for existing worktrees
1 parent 40157a7 commit 159bceb

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

src/lib/HatchboxManager.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,87 @@ describe('HatchboxManager', () => {
11781178
expect(mockGitWorktree.createWorktree).not.toHaveBeenCalled()
11791179
})
11801180

1181+
it('should NOT copy .env file or set PORT when reusing existing worktree for issue', async () => {
1182+
const input: CreateHatchboxInput = {
1183+
type: 'issue',
1184+
identifier: 39,
1185+
originalInput: '39',
1186+
}
1187+
1188+
const existingWorktree = {
1189+
path: '/test/worktree-issue-39',
1190+
branch: 'issue-39-test',
1191+
commit: 'abc123',
1192+
bare: false,
1193+
detached: false,
1194+
locked: false,
1195+
}
1196+
1197+
vi.mocked(mockGitHub.fetchIssue).mockResolvedValue({
1198+
number: 39,
1199+
title: 'Test Issue',
1200+
body: 'Test description',
1201+
state: 'open',
1202+
labels: [],
1203+
assignees: [],
1204+
url: 'https://github.com/test/repo/issues/39',
1205+
})
1206+
vi.mocked(mockGitWorktree.findWorktreeForIssue).mockResolvedValue(existingWorktree)
1207+
vi.mocked(mockCapabilityDetector.detectCapabilities).mockResolvedValue({
1208+
capabilities: ['web'],
1209+
binEntries: {},
1210+
})
1211+
1212+
await manager.createHatchbox(input)
1213+
1214+
// BUG: These operations should NOT be called when reusing an existing worktree
1215+
// The .env file was already set up during initial worktree creation
1216+
expect(mockEnvironment.copyEnvFile).not.toHaveBeenCalled()
1217+
expect(mockEnvironment.setPortForWorkspace).not.toHaveBeenCalled()
1218+
expect(mockEnvironment.setEnvVar).not.toHaveBeenCalled()
1219+
})
1220+
1221+
it('should NOT copy .env file or set PORT when reusing existing worktree for PR', async () => {
1222+
const input: CreateHatchboxInput = {
1223+
type: 'pr',
1224+
identifier: 42,
1225+
originalInput: 'pr/42',
1226+
}
1227+
1228+
const existingWorktree = {
1229+
path: '/test/worktree-feat-test_pr_42',
1230+
branch: 'feat/test-feature',
1231+
commit: 'def456',
1232+
bare: false,
1233+
detached: false,
1234+
locked: false,
1235+
}
1236+
1237+
vi.mocked(mockGitHub.fetchPR).mockResolvedValue({
1238+
number: 42,
1239+
title: 'Test PR',
1240+
body: 'Test description',
1241+
state: 'open',
1242+
branch: 'feat/test-feature',
1243+
baseBranch: 'main',
1244+
url: 'https://github.com/test/repo/pull/42',
1245+
isDraft: false,
1246+
})
1247+
vi.mocked(mockGitWorktree.findWorktreeForPR).mockResolvedValue(existingWorktree)
1248+
vi.mocked(mockCapabilityDetector.detectCapabilities).mockResolvedValue({
1249+
capabilities: ['web'],
1250+
binEntries: {},
1251+
})
1252+
1253+
await manager.createHatchbox(input)
1254+
1255+
// BUG: These operations should NOT be called when reusing an existing worktree
1256+
// The .env file was already set up during initial worktree creation
1257+
expect(mockEnvironment.copyEnvFile).not.toHaveBeenCalled()
1258+
expect(mockEnvironment.setPortForWorkspace).not.toHaveBeenCalled()
1259+
expect(mockEnvironment.setEnvVar).not.toHaveBeenCalled()
1260+
})
1261+
11811262
it('should still call moveIssueToInProgress for issue reuse', async () => {
11821263
const input: CreateHatchboxInput = {
11831264
type: 'issue',

src/lib/HatchboxManager.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,11 @@ export class HatchboxManager {
505505
// 2. Detect capabilities (quick, no installation)
506506
const { capabilities, binEntries } = await this.capabilityDetector.detectCapabilities(worktreePath)
507507

508-
// 3. Setup environment for existing worktrees too (copy .env + set PORT)
508+
// 3. Calculate port for existing worktree (but DON'T copy .env or set PORT)
509+
// The .env file was already set up when the worktree was first created
509510
let port = 3000
510511
if (capabilities.includes('web')) {
511-
port = await this.setupEnvironment(worktreePath, input)
512+
port = this.calculatePort(input)
512513
}
513514

514515
// 4. Skip database branch creation for existing worktrees

0 commit comments

Comments
 (0)