clockworklabs/SpacetimeDB · error · anyhow::Error

A SpacetimeDB module already exists in the target directory:

Error message

A SpacetimeDB module already exists in the target directory: {}

What it means

In server-only mode (`--server-only`), init may add a module to an existing project, but the `spacetimedb/` subdirectory that will hold the module must be absent or empty. If `spacetimedb/` already contains files — a previous init, a hand-written module, or leftover artifacts — init treats it as an existing module and aborts so nothing is overwritten.

Source

Thrown at crates/cli/src/subcommands/init.rs:791

        use_local: true,
        native_aot: false,
    })
}

pub fn ensure_empty_directory(_project_name: &str, project_path: &Path, is_server_only: bool) -> anyhow::Result<()> {
    if project_path.exists() {
        if !project_path.is_dir() {
            anyhow::bail!(
                "Path {} exists but is not a directory. A new SpacetimeDB project must be initialized in an empty directory.",
                project_path.display()
            );
        }

        if std::fs::read_dir(project_path).unwrap().count() > 0 {
            if is_server_only {
                let server_dir = project_path.join("spacetimedb");
                if server_dir.exists() && std::fs::read_dir(server_dir).unwrap().count() > 0 {
                    anyhow::bail!(
                        "A SpacetimeDB module already exists in the target directory: {}",
                        project_path.display()
                    );
                }
            } else {
                anyhow::bail!(
                    "Cannot create new SpacetimeDB project in non-empty directory: {}",
                    project_path.display()
                );
            }
        }
    } else {
        fs::create_dir_all(project_path).context("Failed to create directory")?;
    }
    Ok(())
}

async fn get_template_config_interactive(

View on GitHub (pinned to 524b4487d9)

Solutions

  1. Delete or move the existing `spacetimedb/` directory contents, then re-run
  2. Point init at a fresh project path
  3. If the existing module is intentional, keep developing it instead of re-initializing

Example fix

# before — spacetimedb/ already has files
spacetime init --non-interactive --project-name my-app --lang rust --server-only

# after
mv spacetimedb spacetimedb.bak
spacetime init --non-interactive --project-name my-app --lang rust --server-only
Defensive patterns

Strategy: validation

Validate before calling

# Server-only init pre-flight:
if [[ -d spacetimedb && -n $(ls -A spacetimedb) ]]; then
  echo "spacetimedb/ already contains a module — move it or pick a new path" >&2; exit 2
fi
spacetime init --non-interactive --project-name my-app --lang rust --server-only

Prevention

When it happens

Trigger: `spacetime init --server-only` into a project whose `spacetimedb/` folder is non-empty; re-running init after a partially completed scaffold; adding a second module where one already exists.

Common situations: Retrying init after an earlier failure left files behind; teams scaffolding into an existing repo that already has a spacetimedb directory; stale output from deleted modules.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@524b4487d9 (2026-08-16). Data as JSON: /api/errors/6c510f41a712fc4e. Report an issue: GitHub.