Use CWD as default for new project creation (vibe-kanban) (#814)

* Perfect! I've successfully implemented the fix to change the default directory for new project creation from home directory to current working directory.

**Frontend Changes:**
- **ProjectFormDialog.tsx**: Removed hardcoded `~` path concatenation, now sends parent path or empty string for new repos
- **project-form-fields.tsx**: Updated UI text from "Home" to "Current Directory"
- **Removed `folderName` state**: No longer needed since backend handles name sanitization

**Backend Changes:**
- **projects.rs**: Added conditional path construction based on `use_existing_repo` flag
  - Existing repos: Use full path as-is
  - New repos: Construct from parent + sanitized name, defaulting to `std::env::current_dir()`
- **Added `sanitize_project_name()` helper**: Converts project names to valid folder names

**Key Benefits:**
 New projects now default to current working directory instead of home
 Clean separation between existing vs new repo flows
 Backend owns all path resolution logic
 Maintains backward compatibility
 Both TypeScript and Rust compilation pass

The implementation ensures that when users create new projects without specifying a parent directory, they'll be created in their current working directory rather than their home directory, making the workflow more intuitive.

You raise an excellent point about API design. The current implementation has some issues that separate request structs would solve elegantly.

**Overloaded `git_repo_path` field:**
```rust
// Current CreateProject struct
pub struct CreateProject {
    git_repo_path: String,  // Sometimes full path, sometimes parent directory!
    use_existing_repo: bool, // Flag to determine meaning of above field
    // ...
}
```

**Problems:**
1. `git_repo_path` has different semantics based on `use_existing_repo` flag
2. For new projects, it's not actually a "git repo path" - it's a parent directory
3. The struct is ambiguous about field meanings
4. Easy to misuse the API

```rust
pub struct CreateProjectFromRepoRequest {
    name: String,
    git_repo_path: String,          // Full path to existing repo (clear!)
    setup_script: Option<String>,
    dev_script: Option<String>,
    cleanup_script: Option<String>,
    copy_files: Option<String>,
}

pub struct CreateBlankProjectRequest {
    name: String,
    parent_directory: Option<String>, // Optional parent, defaults to cwd (clear!)
    setup_script: Option<String>,
    dev_script: Option<String>,
    cleanup_script: Option<String>,
    copy_files: Option<String>,
}
```

**Type Safety & Clarity:**
-  Each field has single, clear meaning
-  `git_repo_path` vs `parent_directory` - no ambiguity
-  Can't accidentally send wrong data for wrong use case
-  API is self-documenting

**Better API Design:**
-  No boolean flags needed
-  Each struct has exactly the fields it needs
-  More discoverable API
-  Follows single responsibility principle

**Option 1: Separate Endpoints**
```rust
POST /api/projects/from-repo     // CreateProjectFromRepoRequest
POST /api/projects/blank         // CreateBlankProjectRequest
```

**Option 2: Single Endpoint with Tagged Union**
```rust
pub enum CreateProjectRequest {
    #[serde(rename = "from_repo")]
    FromRepo(CreateProjectFromRepoRequest),
    #[serde(rename = "blank")]
    Blank(CreateBlankProjectRequest),
}
```

**Yes, separate structs would be much cleaner.** The current approach works but violates the principle of explicit, unambiguous APIs. The overloaded `git_repo_path` field is a code smell.

**Suggested approach:**
1. Use separate request structs
2. Single endpoint with tagged union (less breaking change)
3. Frontend sends `{ "type": "blank", ...data }` or `{ "type": "from_repo", ...data }`
4. Backend pattern matches on the enum

This would be a more significant refactor but results in much better API design, type safety, and developer experience.

Perfect! Here's the clean, simple solution that leverages how relative paths naturally work:

**Frontend Changes:**
- When creating new projects, frontend now sends either:
  - Full path: `~/code/my-project` (when parent directory is specified)
  - Just folder name: `my-project` (when no parent directory specified)
- Folder name is auto-generated from project name using standard sanitization

**Backend Changes:**
- Reverted to simple approach: `let path = expand_tilde(&git_repo_path);`
- No conditional logic needed - relative paths resolve naturally to current working directory
- Removed the complex path construction logic

**How it Works:**

1. **User runs server in projects directory:** `cd ~/code && vibe-kanban`
2. **User creates new project "My Awesome App":**
   - Frontend sends: `git_repo_path: "my-awesome-app"`
   - Backend calls: `std::fs::create_dir_all("my-awesome-app")`
   - Creates: `~/code/my-awesome-app/` 

3. **User specifies parent directory:** `~/projects`
   - Frontend sends: `git_repo_path: "~/projects/my-awesome-app"`
   - Backend calls: `std::fs::create_dir_all("/Users/me/projects/my-awesome-app")`
   - Creates: `~/projects/my-awesome-app/` 

 **Unix standard behavior** - matches `mkdir`, `git clone`, etc.
 **Minimal code** - removed complexity instead of adding it
 **Natural semantics** - relative paths just work
 **No special cases** - file system handles path resolution
 **Clean API** - `git_repo_path` always means path to git repo

This is exactly how command-line tools work - when you don't specify an absolute path, they create files relative to where you ran the command. Simple and intuitive!

* Make path absolute
This commit is contained in:
Alex Netsch
2025-09-23 18:09:26 +01:00
committed by GitHub
parent b63b171ade
commit 2351743405
3 changed files with 9 additions and 11 deletions

View File

@@ -61,8 +61,7 @@ pub async fn create_project(
tracing::debug!("Creating project '{}'", name);
// Validate and setup git repository
// Expand tilde in git repo path if present
let path = expand_tilde(&git_repo_path);
let path = std::path::absolute(expand_tilde(&git_repo_path))?;
// Check if git repo path is already used by another project
match Project::find_by_git_repo_path(&deployment.db().pool, path.to_string_lossy().as_ref())
.await

View File

@@ -107,12 +107,11 @@ export const ProjectFormDialog = NiceModal.create<ProjectFormDialogProps>(
try {
let finalGitRepoPath = gitRepoPath;
if (repoMode === 'new') {
// Use home directory (~) if parentPath is empty
const effectiveParentPath = parentPath.trim() || '~';
finalGitRepoPath = `${effectiveParentPath}/${folderName}`.replace(
/\/+/g,
'/'
);
const effectiveParentPath = parentPath.trim();
const cleanFolderName = folderName.trim();
finalGitRepoPath = effectiveParentPath
? `${effectiveParentPath}/${cleanFolderName}`.replace(/\/+/g, '/')
: cleanFolderName;
}
// Auto-populate name from git repo path if not provided
const finalName =

View File

@@ -349,7 +349,7 @@ export function ProjectFormFields({
type="text"
value={parentPath}
onChange={(e) => setParentPath(e.target.value)}
placeholder="Home"
placeholder="Current Directory"
className="flex-1 placeholder:text-secondary-foreground placeholder:opacity-100"
/>
<Button
@@ -371,8 +371,8 @@ export function ProjectFormFields({
</Button>
</div>
<p className="text-xs text-muted-foreground">
Leave empty to use your home directory, or specify a custom
path.
Leave empty to use your current working directory, or specify a
custom path.
</p>
</div>
</div>