fix: prevent CLAUDE.md overwrite by using imports#937
fix: prevent CLAUDE.md overwrite by using imports#937ben-vargas wants to merge 5 commits intoeyaltoledano:nextfrom
Conversation
🦋 Changeset detectedLatest commit: 121cd7a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Crunchyman-ralph
left a comment
There was a problem hiding this comment.
small questions, but essentially great PR, lets merge
| "task-master-ai": minor | ||
| --- | ||
|
|
||
| Fix: Prevent CLAUDE.md overwrite by using Claude Code's import feature |
There was a problem hiding this comment.
Don't prefix with Fix, if its a fix, just change from minor to patch on line 2 of this file
There was a problem hiding this comment.
Done.. "unfixed" 😂
| - Preserves existing user content in CLAUDE.md files | ||
| - Removed duplicate profile initialization that was causing files to be written twice | ||
|
|
||
| **Breaking Change**: Task Master instructions for Claude Code are now stored in `.taskmaster/CLAUDE.md` and imported into the main CLAUDE.md file. Users who previously had Task Master content directly in their CLAUDE.md will need to run `task-master rules remove claude` followed by `task-master rules add claude` to migrate to the new structure. No newline at end of file |
scripts/modules/commands.js
Outdated
| if (typeof profileConfig.onAddRulesProfile === 'function') { | ||
| const assetsDir = path.join(projectRoot, 'assets'); | ||
| profileConfig.onAddRulesProfile(projectRoot, assetsDir); | ||
| } |
There was a problem hiding this comment.
why remove this ? just curious
There was a problem hiding this comment.
Discussed on discord, but just for documentation sake...
o3 confirmation/explanation -
Yes – the extra block in scripts/modules/commands.js is really doing the same work a second time, and that is what makes CLAUDE.md (and the other profile “integration-guide” files) get copied twice.
Why it happens
-
convertAllRulesToProfileRules(projectRoot, profileConfig)
• For every profile it ends by calling the profile’sonPostConvertRulesProfile.
• Every profile implementsonPostConvertRulesProfileas a thin wrapper that just callsonAddRulesProfile.
• So, after this single call, the file copy (e.g.AGENTS.md → CLAUDE.md) has already happened once. -
Immediately afterwards, lines 4019-4023 call
onAddRulesProfileagain:
if (typeof profileConfig.onAddRulesProfile === 'function') {
const assetsDir = path.join(projectRoot, 'assets'); // <-- wrong path, too
profileConfig.onAddRulesProfile(projectRoot, assetsDir);
}That means:
• For “simple” profiles (claude, codex, etc.) the copy runs twice – once with the correct package-internal assets directory (step 1) and again with <projectRoot>/assets, which usually doesn’t exist and can create a spurious empty folder.
• For the bigger profiles (roo, windsurf, …) you also get the duplicate execution, potentially copying the same files twice.
What the block was probably meant for
Before convertAllRulesToProfileRules grew the “post-convert” hook, CLI code had to call onAddRulesProfile itself. After the refactor, the explicit call became obsolete but was never removed.
Will removing it break “re-setup” use-cases?
No. Users re-run the copy via either
task-master rules add <profile>
# or
task-master rules --setup (interactive)
Both paths already invoke convertAllRulesToProfileRules, which in turn fires onPostConvertRulesProfile → onAddRulesProfile. Taking out the duplicate call does not stop a user from restoring the integration file; it only prevents the second, redundant invocation (and the wrong-directory copy).
So the change Claude suggested – deleting lines 4020-4023 – is correct and safe:
- if (typeof profileConfig.onAddRulesProfile === 'function') {
- const assetsDir = path.join(projectRoot, 'assets');
- profileConfig.onAddRulesProfile(projectRoot, assetsDir);
- }After this removal:
• CLAUDE.md (or the equivalent guide file for other profiles) is created exactly once.
• The assets directory path remains correct (<package>/assets/...).
• All existing CLI commands for adding / re-adding profiles continue to work.
There was a problem hiding this comment.
I concur. These lines are duplicative and can be removed. Tested on my end.
1422cf8 to
12124e8
Compare
12124e8 to
e4d67db
Compare
… functions - Remove unused copyRecursiveSync function (not needed for single file operations) - Remove unused removeDirectoryRecursive function (not needed for single file removal) - Keep only essential changes for non-destructive CLAUDE.md import feature - All tests pass, functionality remains intact
…d helper functions" This reverts commit bdedc08.
|
Hi! 👋 Do we have this solution implemented by another PR? This enhancement is a very welcome one! |
|
@vedovelli - See #949 - It was ready but wasn't merged in 0.20.0 but it has since been merged into |
Thank you for your reply! I keep my TM always updated so this enhancement should reach me soon enough! Have a great weekend. |
Summary
This PR fixes the issue where Task Master overwrites users' existing CLAUDE.md files by implementing a non-destructive approach using Claude Code's import feature.
Fixes #929
Changes
.taskmaster/CLAUDE.mdinstead of overwriting the user'sCLAUDE.mdBreaking Change
Task Master instructions for Claude Code are now stored in
.taskmaster/CLAUDE.mdand imported into the main CLAUDE.md file. Users who previously had Task Master content directly in their CLAUDE.md will need to run:to migrate to the new structure.
Example
When running
task-master rules add claude, the user's existing CLAUDE.md:Will be updated to:
And Task Master's instructions will be placed in
.taskmaster/CLAUDE.md.Test Plan