google-gemini/gemini-cli · error

Command requires a workspace.

Error message

Command requires a workspace.

What it means

The InitCommand (which generates a GEMINI.md file via performInit) requires a workspace target directory. If context.agentContext.config.getTargetDir() returns a falsy value, the command cannot determine where to write GEMINI.md and throws. The command declares requiresWorkspace = true.

Source

Thrown at packages/cli/src/acp/commands/init.ts:27

import { performInit } from '@google/gemini-cli-core';
import type {
  Command,
  CommandContext,
  CommandExecutionResponse,
} from './types.js';

export class InitCommand implements Command {
  name = 'init';
  description = 'Analyzes the project and creates a tailored GEMINI.md file';
  requiresWorkspace = true;

  async execute(
    context: CommandContext,
    _args: string[] = [],
  ): Promise<CommandExecutionResponse> {
    const targetDir = context.agentContext.config.getTargetDir();
    if (!targetDir) {
      throw new Error('Command requires a workspace.');
    }

    const geminiMdPath = path.join(targetDir, 'GEMINI.md');
    const result = performInit(fs.existsSync(geminiMdPath));

    switch (result.type) {
      case 'message':
        return {
          name: this.name,
          data: result,
        };
      case 'submit_prompt':
        fs.writeFileSync(geminiMdPath, '', 'utf8');

        if (typeof result.content !== 'string') {
          throw new Error('Init command content must be a string.');
        }

View on GitHub (pinned to 5024443c72)

Solutions

  1. Start the CLI from within a project directory so CWD resolves to a target dir.
  2. Set the target directory in configuration before running init.
  3. Ensure the agent context's config.getTargetDir() is initialized before dispatching the init command.
Defensive patterns

Strategy: validation

Validate before calling

function hasWorkspace(context: CommandContext): boolean {
  return Boolean(context.agentContext.config.getTargetDir());
}

// Before dispatching init:
if (!hasWorkspace(context)) {
  return { name: 'init', data: { type: 'message', messageType: 'error', content: 'Open a project directory first.' } };
}

Prevention

When it happens

Trigger: Running the /init command when the CLI was started without a target directory set, or when getTargetDir() has not been initialized in the agent context.

Common situations: CLI started in a mode without a workspace; target directory not configured in settings; running init from a context where no project root or CWD was resolved; ACP session initialized without a workspace path.

Related errors


AI-assisted analysis of google-gemini/gemini-cli@5024443c72 (2026-08-12). Data as JSON: /api/errors/9f7d84dfdeac69ca. Report an issue: GitHub.