Skip to content

Conversation

@LeoZheng1738
Copy link
Contributor

@LeoZheng1738 LeoZheng1738 commented Nov 6, 2025

Unify Project Name Generation Logic to Align with Claude Code

Purpose

To ensure the project name generation logic is consistent with the rules used when creating projects in Claude Code, and to avoid potential issues (such as mismatched project names or conflicts) caused by differences in naming logic. This modification optimizes the way project names are generated by replacing all non-alphanumeric characters in folder paths with hyphens (-).

Changes Made

The original code only replaced slashes (/) in the path with hyphens. After modification, all non-alphanumeric characters ([^a-zA-Z0-9]) in the path are replaced with hyphens.

  • Original code:

    const projectName = absolutePath.replace(/\//g, '-');
  • Modified code:

    const projectName = absolutePath.replace(/[^a-zA-Z0-9]/g, '-');

Example Comparisons

The following typical path cases compare the project name generation results before and after the modification:

Case 1: Basic path with only slashes

  • Original path: /user/docs/project
  • Result with original logic: -user-docs-project (only slashes replaced)
  • Result with new logic: -user-docs-project (consistent with original result, as only slashes are present)

Case 2: Path containing underscores and spaces

  • Original path: /workspace/my_project/version 1.0
  • Result with original logic: -workspace-my_project-version 1.0 (only slashes replaced; underscores and spaces retained)
  • Result with new logic: -workspace-my-project-version-1-0 (underscores and spaces replaced with -)

Case 3: Path containing special symbols (!, @, #)

  • Original path: /tmp/test!demo@v1#final
  • Result with original logic: -tmp-test!demo@v1#final (only slashes replaced; special symbols retained)
  • Result with new logic: -tmp-test-demo-v1-final (all special symbols replaced with -)

Case 4: Path containing Chinese characters and punctuation

  • Original path: /本地项目/测试_文件夹?v2.1
  • Result with original logic: -本地项目-测试_文件夹?v2.1 (only slashes replaced; Chinese characters and punctuation retained)
  • Result with new logic: -——————-v2-1 (Chinese characters, underscores, and question marks replaced with -)

Impact Description

  • After this modification, project names will only retain letters (uppercase and lowercase) and numbers, with all other characters converted to -, fully aligning with Claude Code's project naming logic.
  • For paths without special characters, the generated results are consistent with the original logic, with no destructive impact.
  • For paths containing special characters, names will be more standardized, reducing cross-system compatibility issues (such as file system naming restrictions, URL encoding problems, etc.).

Testing Status

Verified with the above example paths, the project names generated by the new logic meet expectations and are completely consistent with the naming results when creating projects in Claude Code.

Please review and merge. Thank you!

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of special characters in project names. Non-alphanumeric characters are now consistently replaced with hyphens for proper storage and path resolution.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 6, 2025

Walkthrough

The character encoding logic in the addProjectManually function was modified to replace all non-alphanumeric characters with hyphens, broadening the scope beyond just forward slashes. This affects how project names are encoded when manually added to the system.

Changes

Cohort / File(s) Summary
Character Encoding Update
server/projects.js
Modified encoding logic in addProjectManually to replace all non-alphanumeric characters with '-' instead of only replacing '/' characters.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Potential areas requiring attention:
    • Impact on existing project naming and path resolution with previously stored projects
    • Backward compatibility considerations for projects with names containing special characters
    • Validation that the new regex pattern behaves as intended across various character sets

Poem

A rabbit hops through projects new,
With names now clean, so fresh and true—
No slashes left, no symbols stay,
Just alphanumeric way! 🐰✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: converting special characters (non-alphanumeric) to hyphens in project names, which directly matches the code modification from /\//g to /[^a-zA-Z0-9]/g.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 289c233 and e477791.

📒 Files selected for processing (1)
  • server/projects.js (1 hunks)


// Generate project name (encode path for use as directory name)
const projectName = absolutePath.replace(/\//g, '-');
const projectName = absolutePath.replace(/[^a-zA-Z0-9]/g, '-');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Critical: Name collision risk and broken decoding logic.

This change introduces two critical issues:

  1. Name collisions: Different project paths can now map to the same projectName, causing conflicts. For example:

    • /workspace/my_project-workspace-my-project
    • /workspace/my-project-workspace-my-project

    The collision check at line 1001 would incorrectly reject the second (legitimately different) project.

  2. Broken fallback decoding: Multiple locations in the codebase decode projectName back to paths using replace(/-/g, '/'), assuming the old encoding scheme where only slashes were replaced:

    • Line 237 in generateDisplayName
    • Lines 286, 323, 347, 360, 364 in extractProjectDirectory
    • Line 469 in getProjects

    With the new encoding, this decoding produces incorrect paths. For example, if projectName is -workspace-my-project-version-1-0, decoding yields /workspace/my/project/version/1/0 instead of the original path.

While originalPath is stored for newly added manual projects (line 1011), the fallback logic will fail when originalPath is unavailable or for projects encoded with the old scheme.

Recommended solutions:

  1. Maintain bidirectional mapping: Store a mapping of encoded names to original paths in the config, or use a reversible encoding scheme (e.g., URL encoding or base64).

  2. Update all decoding sites: Replace all instances of projectName.replace(/-/g, '/') with logic that retrieves the path from config's originalPath field.

  3. Prevent collisions: Before adding a project, check if any existing config entry has a different originalPath but the same encoded projectName.

  4. Migration strategy: Consider how existing projects with the old encoding will coexist with new ones, or implement a one-time migration to re-encode all existing projects.

Example diff for collision prevention:

  // Check if project already exists in config
  const config = await loadProjectConfig();
  const projectDir = path.join(process.env.HOME, '.claude', 'projects', projectName);

  if (config[projectName]) {
-    throw new Error(`Project already configured for path: ${absolutePath}`);
+    // Check if it's truly the same project or a collision
+    if (config[projectName].originalPath !== absolutePath) {
+      throw new Error(`Project name collision: A different project at '${config[projectName].originalPath}' already uses the encoded name '${projectName}'`);
+    }
+    throw new Error(`Project already configured for path: ${absolutePath}`);
  }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In server/projects.js around line 995, the new projectName generation (replacing
all non-alphanumerics with '-') causes name collisions and breaks existing
decoding that expects '-'→'/' replacements; fix by switching to a reversible
encoding or storing the original path: update project creation to save an
originalPath (or use base64/encodeURIComponent for projectName) in the config,
change all decoding sites to prefer config.originalPath when present (falling
back to any legacy decoding only if originalPath is missing), add a pre-add
collision check that looks up any existing entry with the same projectName but a
different originalPath and reject/rename accordingly, and plan a one-time
migration or compatibility logic to re-encode or populate originalPath for
legacy entries so old and new encodings don’t conflict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant