Skip to content
Source
Sign inSign up
pingdotgg/t3code

Multi-repo worktree silently checks out main when the release base is a full origin ref #48178

OpenKiritoXDone(GitHub profile, opens in a new tab) opened this issue 0 comments

A multi-repository worktree can report success while checking out the default branch in a repository that has the requested release branch. This happens when the base is written as a full origin tracking ref. I reproduced it with the production vcs.createWorktree RPC on upstream 7b7ca65f9a (Node 26.13.1, macOS 26.2).

Reproduction

  1. Add a plain project directory containing the Git repositories api and web. Both have main.
  2. In api, publish a release commit with different contents to a local bare remote named origin. Leave only the remote-tracking branch in the checkout. The web repository has no release branch.
  3. Ask the running server to create a new worktree with origin/release as the base. Repeat with a different new branch name and refs/remotes/origin/release as the base.
  4. Inspect the file contents and run git rev-parse HEAD in each checkout. Git resolves both spellings to the same commit before either request.
Base passed to the serverActual API checkoutActual web checkout
origin/releaserelease, 569d46133726main, 350af754b623
refs/remotes/origin/releasemain, 350af754b623main, 350af754b623

The API checkout should start at 569d46133726 in both cases. The documented fallback to the default branch is appropriate for the web checkout. Both calls return a worktree path without an error. Multi-repository worktree behavior

Reproduce with Node and Git, including the raw RPC calls

Save as reproduce.mjs and run node /path/to/reproduce.mjs from a checkout with dependencies installed. The script uses a temporary data directory, a loopback server and a local Git remote. It starts no provider sessions.

import { execFileSync, spawn } from 'node:child_process';
import { once } from 'node:events';
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { createServer } from 'node:net';
import { tmpdir } from 'node:os';
import { delimiter, dirname, join } from 'node:path';
import { setTimeout as delay } from 'node:timers/promises';

const fixture = mkdtempSync(join(tmpdir(), 't3-worktree-base-'));
const state = join(fixture, 'state');
const project = join(fixture, 'project');
const api = join(project, 'api');
const web = join(project, 'web');
const cli = 'apps/server/src/bin.ts';
const env = { PATH: [dirname(process.execPath), '/usr/bin', '/bin'].join(delimiter), DO_NOT_TRACK: '1' };
const run = (...args) => execFileSync(process.execPath, [cli, ...args, '--base-dir', state], { env, encoding: 'utf8', timeout: 30000 });
let server, ws, stopped;
try {
  mkdirSync(project);
  function git(cwd, ...args) {
    return execFileSync('git', ['-c','user.name=Local Fixture','-c','user.email=fixture@example.invalid',...args],
      {cwd, env:{...env,GIT_CONFIG_NOSYSTEM:'1',GIT_CONFIG_GLOBAL:'/dev/null',GIT_AUTHOR_DATE:'2026-01-01T00:00:00Z',GIT_COMMITTER_DATE:'2026-01-01T00:00:00Z'},encoding:'utf8'}).trim();
  }
  for (const repo of [api,web]) {
    mkdirSync(repo);
    git(repo,'init','-q','-b','main');
    writeFileSync(join(repo,'version'),'main');
    git(repo,'add','version'); git(repo,'commit','-qm','main baseline');
  }
  git(api,'switch','-qc','release');
  writeFileSync(join(api,'version'),'release');
  git(api,'commit','-qam','release changes');
  git(api,'switch','-q','main');
  const id = /Added project (\S+)/.exec(run('project', 'add', project))?.[1];
  if (!id) throw Error('Project creation failed');
  const lease = createServer().listen(0,'127.0.0.1');
  await once(lease,'listening');
  const {port} = lease.address();
  await new Promise(resolve => lease.close(resolve));
  server = spawn(process.execPath, [cli, 'serve', '--host', '127.0.0.1', '--port', String(port), '--base-dir', state], {env, stdio:'ignore'});
  stopped = once(server,'exit');
  const runtime = join(state,'userdata','server-runtime.json');
  for (let attempt=0; !existsSync(runtime) && attempt<300 && server.exitCode === null; attempt++) await delay(100);
  const {origin} = JSON.parse(readFileSync(runtime,'utf8'));
  const credential = run('auth','session','issue','--ttl','5m','--token-only').trim();
  const response = await fetch(origin+'/api/auth/websocket-ticket',{method:'POST',headers:{Authorization:'Bearer '+credential}});
  if (!response.ok) throw Error('Ticket request: '+response.status);
  const {ticket} = await response.json();
  const url = new URL('/ws',origin.replace('http:', 'ws:'));
  url.searchParams.set('orchestrationProtocol','2'); url.searchParams.set('wsTicket',ticket);
  ws = new WebSocket(url);
  await once(ws,'open',{signal:AbortSignal.timeout(10000)});
  let next = 0;
  async function checkout(label,base) {
    const requestId = String(++next);
    ws.send(JSON.stringify({_tag:'Request', id:requestId, tag:'vcs.createWorktree',
      payload:{cwd:project,refName:base,baseRefName:base,newRefName:'task-'+requestId,path:null},headers:[]}));
    for (;;) {
      const [event] = await once(ws,'message',{signal:AbortSignal.timeout(30000)});
      const reply = [JSON.parse(event.data)].flat().find(row => row._tag==='Exit' && row.requestId===requestId);
      if (!reply) continue;
      if (reply.exit._tag!=='Success') {console.log(label+' : '+JSON.stringify(reply.exit));return;}
      const where=reply.exit.value.worktree.path;
      const content=repo=>readFileSync(join(where,repo,'version'),'utf8');
      console.log(label+' : api='+content('api')+'@'+git(join(where,'api'),'rev-parse','--short=12','HEAD')
        +', web='+content('web')+'@'+git(join(where,'web'),'rev-parse','--short=12','HEAD'));
      return;
    }
  }
  await checkout('control: release branch','release');
  const originRepo = join(fixture,'origin.git');
  git(fixture, 'clone', '-q', '--bare', api, originRepo);
  git(api, 'remote', 'add', 'origin', originRepo);
  git(api, 'fetch', '-q', 'origin');
  git(api,'branch','-D','release');
  console.log('Git resolves both base names to:',git(api,'rev-parse','--short=12','origin/release'),git(api,'rev-parse','--short=12','refs/remotes/origin/release'));
  await checkout('short remote branch','origin/release');
  await checkout('full remote branch','refs/remotes/origin/release');
} finally {
  ws?.close();
  if (server && server.exitCode===null && server.signalCode===null) server.kill('SIGTERM');
  if (stopped) await stopped;
  rmSync(fixture,{recursive:true,force:true});
}
  1. tanstack-source added labelsbug

Sign in to join this conversation.