fix: root monorepo subfolder imports at the git repo while keeping the first terminal in the subfolder (#6336) (#6574)

Add Project → Browse folder on a monorepo subfolder (e.g. /tmp/monorepo/packages/web)
stored the subfolder as Repo.path because isGitRepo() uses
`git rev-parse --is-inside-work-tree`, which is true for any subdirectory.
Authoritative worktree resolution then snapped activation back to the repo root,
so the import identity mismatched and the first terminal landed at the repo root
rather than the selected subfolder.

Canonicalize local git imports to the actual repo root via getGitRepoRoot()
(mirroring the SSH import path that snaps to check.rootPath, including its
post-resolution dedup), and preserve the user-selected subfolder as a one-shot
initial terminal cwd consumed only by the first activation-created pane.
Selecting the repo root applies no override, and re-importing a subfolder of an
existing project dedupes.

Supersedes community PR #6362.

Fixes #6336

Co-authored-by: Rod Boev <rodboev@users.noreply.github.com>
This commit is contained in:
Neil
2026-06-28 16:33:06 -07:00
committed by GitHub
co-authored by Rod Boev
parent ca90af2450
commit adcf74a9fb
23 changed files with 481 additions and 27 deletions
+21 -1
View File
@@ -3,7 +3,7 @@ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'fs'
import { tmpdir } from 'os'
import * as path from 'path'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { isGitRepo } from './repo'
import { getGitRepoRoot, isGitRepo } from './repo'
function git(cwd: string, args: string[]): string {
return execFileSync('git', args, { cwd, encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] })
@@ -34,4 +34,24 @@ describe('isGitRepo', () => {
expect(isGitRepo(bareRepo)).toBe(true)
})
it('resolves a contained path to the worktree root', () => {
const repoRoot = path.join(tmpDir, 'repo')
const nestedDir = path.join(repoRoot, 'packages', 'web')
mkdirSync(nestedDir, { recursive: true })
git(tmpDir, ['init', '--quiet', repoRoot])
// Why: derive the expected root from git's own --show-toplevel so the
// assertion matches getGitRepoRoot's canonicalization (e.g. macOS resolves
// the /var tmpdir symlink to /private/var) across all platforms.
const expectedRoot = git(repoRoot, ['rev-parse', '--show-toplevel']).trim().replace(/\\/g, '/')
expect(getGitRepoRoot(nestedDir)).toBe(expectedRoot)
})
it('preserves bare repository paths when no worktree root exists', () => {
const bareRepo = path.join(tmpDir, 'bare.git')
git(tmpDir, ['init', '--bare', '--quiet', bareRepo])
expect(getGitRepoRoot(bareRepo)).toBe(bareRepo)
})
})
+22
View File
@@ -5,6 +5,7 @@ import { basename } from 'path'
import { gitExecFileSync, gitExecFileAsync } from './runner'
import type { BaseRefSearchResult } from '../../shared/types'
import { parseGitRevListAheadBehindCounts } from '../../shared/git-rev-list-output'
import { normalizeRuntimePathSeparators } from '../../shared/cross-platform-path'
import {
buildHostedRemoteCommitUrl,
buildHostedRemoteFileUrl,
@@ -89,6 +90,27 @@ export function isGitRepo(path: string): boolean {
}
}
export function getGitRepoRoot(path: string): string {
try {
if (!existsSync(path) || !statSync(path).isDirectory()) {
return path
}
const insideWorkTree = gitExecFileSync(['rev-parse', '--is-inside-work-tree'], {
cwd: path
}).trim()
if (insideWorkTree === 'true') {
return normalizeRuntimePathSeparators(
gitExecFileSync(['rev-parse', '--show-toplevel'], {
cwd: path
}).trim()
)
}
} catch {
// Fall through to preserving the original path.
}
return path
}
/**
* Get a human-readable name for the repo from its path.
*/