-
Notifications
You must be signed in to change notification settings - Fork 630
fix(projects): Convert special characters other than English letters and numbers to - #228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe character encoding logic in the Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
|
||
| // Generate project name (encode path for use as directory name) | ||
| const projectName = absolutePath.replace(/\//g, '-'); | ||
| const projectName = absolutePath.replace(/[^a-zA-Z0-9]/g, '-'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Critical: Name collision risk and broken decoding logic.
This change introduces two critical issues:
-
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.
-
Broken fallback decoding: Multiple locations in the codebase decode
projectNameback to paths usingreplace(/-/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
projectNameis-workspace-my-project-version-1-0, decoding yields/workspace/my/project/version/1/0instead of the original path. - Line 237 in
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:
-
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).
-
Update all decoding sites: Replace all instances of
projectName.replace(/-/g, '/')with logic that retrieves the path from config'soriginalPathfield. -
Prevent collisions: Before adding a project, check if any existing config entry has a different
originalPathbut the same encodedprojectName. -
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.
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:
Modified code:
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
/user/docs/project-user-docs-project(only slashes replaced)-user-docs-project(consistent with original result, as only slashes are present)Case 2: Path containing underscores and spaces
/workspace/my_project/version 1.0-workspace-my_project-version 1.0(only slashes replaced; underscores and spaces retained)-workspace-my-project-version-1-0(underscores and spaces replaced with-)Case 3: Path containing special symbols (!, @, #)
/tmp/test!demo@v1#final-tmp-test!demo@v1#final(only slashes replaced; special symbols retained)-tmp-test-demo-v1-final(all special symbols replaced with-)Case 4: Path containing Chinese characters and punctuation
/本地项目/测试_文件夹?v2.1-本地项目-测试_文件夹?v2.1(only slashes replaced; Chinese characters and punctuation retained)-——————-v2-1(Chinese characters, underscores, and question marks replaced with-)Impact Description
-, fully aligning with Claude Code's project naming logic.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