clockworklabs/SpacetimeDB · error · anyhow::Error

Cannot create new SpacetimeDB project in non-empty directory

Error message

Cannot create new SpacetimeDB project in non-empty directory: {}

What it means

`spacetime init PATH` refuses to scaffold into a directory that already contains files, unless `--server-only` is used (in which case only the `spacetimedb/` subdirectory must be absent/empty). This guard prevents overwriting existing work with template files.

Solutions

  1. Init into a fresh empty directory and copy files in afterwards
  2. Move or delete the existing contents (`ls -la` to catch hidden files like .git and .DS_Store)
  3. If you only want the module, use `spacetime init PATH --server-only` into a dir where `spacetimedb/` is empty or missing
  4. For adding SpacetimeDB to an existing repo, scaffold elsewhere and merge manually

Example fix

# before
spacetime init .   # fails: directory not empty
# after
spacetime init my-new-project
cd my-new-project
Defensive patterns

Strategy: validation

Validate before calling

# only init when the target is empty (hidden files included)
[ -z "$(ls -A "$DIR" 2>/dev/null)" ] && spacetime init "$DIR" || echo 'dir not empty'

Prevention

When it happens

Trigger: Running `spacetime init .` in a repo that already has a README, .git, or any file; running init twice into the same directory; pointing PATH at a folder with editor/OS droppings like .DS_Store.

Common situations: Trying to add SpacetimeDB to an existing project in place; retrying an init that half-completed; scaffolding inside a git checkout.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@fdd647dfac (2026-08-20). Data as JSON: /api/errors/f51ec414ccf3f84b. Report an issue: GitHub.

Appendix: source

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

    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(
    options: &InitOptions,
    project_name: String,
    project_path: PathBuf,
) -> anyhow::Result<TemplateConfig> {
    let theme = ColorfulTheme::default();

View on GitHub (pinned to fdd647dfac)