{"record":{"id":"6c510f41a712fc4e","repo":"clockworklabs/SpacetimeDB","slug":"a-spacetimedb-module-already-exists-in-the-target","errorCode":null,"errorMessage":"A SpacetimeDB module already exists in the target directory: {}","messagePattern":"A SpacetimeDB module already exists in the target directory: (.+?)","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/cli/src/subcommands/init.rs","lineNumber":791,"sourceCode":"        use_local: true,\n        native_aot: false,\n    })\n}\n\npub fn ensure_empty_directory(_project_name: &str, project_path: &Path, is_server_only: bool) -> anyhow::Result<()> {\n    if project_path.exists() {\n        if !project_path.is_dir() {\n            anyhow::bail!(\n                \"Path {} exists but is not a directory. A new SpacetimeDB project must be initialized in an empty directory.\",\n                project_path.display()\n            );\n        }\n\n        if std::fs::read_dir(project_path).unwrap().count() > 0 {\n            if is_server_only {\n                let server_dir = project_path.join(\"spacetimedb\");\n                if server_dir.exists() && std::fs::read_dir(server_dir).unwrap().count() > 0 {\n                    anyhow::bail!(\n                        \"A SpacetimeDB module already exists in the target directory: {}\",\n                        project_path.display()\n                    );\n                }\n            } else {\n                anyhow::bail!(\n                    \"Cannot create new SpacetimeDB project in non-empty directory: {}\",\n                    project_path.display()\n                );\n            }\n        }\n    } else {\n        fs::create_dir_all(project_path).context(\"Failed to create directory\")?;\n    }\n    Ok(())\n}\n\nasync fn get_template_config_interactive(","sourceCodeStart":773,"sourceCodeEnd":809,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/cli/src/subcommands/init.rs#L773-L809","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Delete or move the existing `spacetimedb/` directory contents, then re-run","Point init at a fresh project path","If the existing module is intentional, keep developing it instead of re-initializing"],"exampleFix":"# before — spacetimedb/ already has files\nspacetime init --non-interactive --project-name my-app --lang rust --server-only\n\n# after\nmv spacetimedb spacetimedb.bak\nspacetime init --non-interactive --project-name my-app --lang rust --server-only","handlingStrategy":"validation","validationCode":"# Server-only init pre-flight:\nif [[ -d spacetimedb && -n $(ls -A spacetimedb) ]]; then\n  echo \"spacetimedb/ already contains a module — move it or pick a new path\" >&2; exit 2\nfi\nspacetime init --non-interactive --project-name my-app --lang rust --server-only","typeGuard":null,"tryCatchPattern":null,"preventionTips":["After a failed init, clean spacetimedb/ before retrying","Use server-only mode deliberately when adding a module to an existing project, and accept its one rule: spacetimedb/ must be empty"],"tags":["init","server-only","directory-not-empty","pre-flight-check"],"backgroundTag":"target-directory-not-empty","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}