resolveExecutionRoot function

String resolveExecutionRoot(
  1. WorkspaceNavigationArgs navArgs, {
  2. required String currentDir,
})

Determine the execution root based on navigation args.

In workspace mode with bare -R, returns the detected workspace root. In workspace mode with -R <path>, validates and returns that path. With -T/--top-repo, finds topmost git repo (requires -i or -o). In project mode, returns the current directory.

Throws ArgumentError if the specified root path is invalid or if -T is used without git traversal mode.

Implementation

String resolveExecutionRoot(
  WorkspaceNavigationArgs navArgs, {
  required String currentDir,
}) {
  // Handle -T/--top-repo first (requires git traversal mode)
  if (navArgs.topRepo) {
    if (!navArgs.innerFirstGit && !navArgs.outerFirstGit) {
      throw ArgumentError(
        '-T/--top-repo requires git traversal mode (-i/--inner-first-git or -o/--outer-first-git)',
      );
    }
    final finder = GitRepoFinder();
    final topRepo = finder.findTopRepo(currentDir);
    if (topRepo == null) {
      throw ArgumentError(
        'No git repository found in directory tree above: $currentDir',
      );
    }
    return topRepo;
  }

  if (navArgs.bareRoot) {
    // Bare -R: find workspace root
    return findWorkspaceRoot(currentDir);
  } else if (navArgs.root != null) {
    // -R <path>: validate specified workspace
    final specifiedPath = p.isAbsolute(navArgs.root!)
        ? navArgs.root!
        : p.join(currentDir, navArgs.root!);
    final resolved = truepath(specifiedPath);

    if (!exists(resolved) || !isDirectory(resolved)) {
      throw ArgumentError(
        'Specified workspace does not exist: ${navArgs.root}',
      );
    }
    if (!isWorkspaceBoundary(resolved)) {
      throw ArgumentError(
        'Specified path is not a workspace (no $kBuildkitMasterYaml): ${navArgs.root}',
      );
    }
    return resolved;
  }

  // Project mode or no -R: use current directory
  return currentDir;
}