Skip to content

Commit 685cc9a

Browse files
committed
refactor: use data directory for knowledge-bases and cli-checkouts
- Refactor resolve_migrated_path_with_fs to accept explicit base directories - Update resolve_global_migrated_path to conditionally use data directory for knowledge_bases and cli-checkouts - Keep other global paths in home/.kiro directory - Add helper function should_use_data_dir to determine path location - Update all tests to use new function signature - Add e2e tests for data directory usage
1 parent 90d66ec commit 685cc9a

File tree

1 file changed

+99
-57
lines changed

1 file changed

+99
-57
lines changed

crates/chat-cli/src/util/paths.rs

Lines changed: 99 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,19 @@ impl FileSystemChecker for RealFileSystem {
7777
}
7878
}
7979

80+
/// Check if a kiro subpath should use data directory instead of home directory
81+
fn should_use_data_dir(kiro_subpath: &str) -> bool {
82+
matches!(kiro_subpath, "knowledge_bases" | "cli-checkouts")
83+
}
84+
8085
fn resolve_migrated_path_with_fs(
8186
fs: &dyn FileSystemChecker,
82-
home_dir: &std::path::Path,
83-
current_dir: &std::path::Path,
87+
kiro_base: &std::path::Path,
88+
amazonq_base: &std::path::Path,
8489
is_global: bool,
8590
amazonq_subpath: &str,
8691
kiro_subpath: &str,
8792
) -> std::path::PathBuf {
88-
let (kiro_base, amazonq_base) = if is_global {
89-
(home_dir.join(".kiro"), home_dir.join(".aws/amazonq"))
90-
} else {
91-
(current_dir.join(".kiro"), current_dir.join(".amazonq"))
92-
};
93-
9493
let scope = if is_global { "global" } else { "workspace" };
9594

9695
debug!(
@@ -102,7 +101,7 @@ fn resolve_migrated_path_with_fs(
102101
amazonq_base.display()
103102
);
104103

105-
let (kiro_exists, amazonq_exists) = (fs.exists(&kiro_base), fs.exists(&amazonq_base));
104+
let (kiro_exists, amazonq_exists) = (fs.exists(kiro_base), fs.exists(amazonq_base));
106105
debug!(
107106
"Path existence check for {} kiro_subpath={} amazonq_subpath={}: kiro_exists={}, amazonq_exists={}",
108107
scope, kiro_subpath, amazonq_subpath, kiro_exists, amazonq_exists
@@ -152,12 +151,20 @@ fn resolve_migrated_path_with_fs(
152151
fn resolve_global_migrated_path(os: &Os, amazonq_subpath: &str, kiro_subpath: &str) -> Result<PathBuf> {
153152
let fs = RealFileSystem;
154153
let home = home_dir(os)?;
155-
let current = os.env.current_dir()?;
154+
155+
let kiro_base = if should_use_data_dir(kiro_subpath) {
156+
dirs::data_local_dir()
157+
.ok_or(DirectoryError::NoHomeDirectory)?
158+
.join("kiro-cli")
159+
} else {
160+
home.join(".kiro")
161+
};
162+
let amazonq_base = home.join(".aws/amazonq");
156163

157164
Ok(resolve_migrated_path_with_fs(
158165
&fs,
159-
&home,
160-
&current,
166+
&kiro_base,
167+
&amazonq_base,
161168
true,
162169
amazonq_subpath,
163170
kiro_subpath,
@@ -166,13 +173,15 @@ fn resolve_global_migrated_path(os: &Os, amazonq_subpath: &str, kiro_subpath: &s
166173

167174
fn resolve_local_migrated_path(os: &Os, amazonq_subpath: &str, kiro_subpath: &str) -> Result<PathBuf> {
168175
let fs = RealFileSystem;
169-
let home = home_dir(os)?;
170176
let current = os.env.current_dir()?;
171177

178+
let kiro_base = current.join(".kiro");
179+
let amazonq_base = current.join(".amazonq");
180+
172181
Ok(resolve_migrated_path_with_fs(
173182
&fs,
174-
&home,
175-
&current,
183+
&kiro_base,
184+
&amazonq_base,
176185
false,
177186
amazonq_subpath,
178187
kiro_subpath,
@@ -396,7 +405,7 @@ impl<'a> GlobalPaths<'a> {
396405
}
397406

398407
pub fn shadow_repo_dir(&self) -> Result<PathBuf> {
399-
resolve_global_migrated_path(self.os, "cli-checkouts", "cli/cli-checkouts")
408+
resolve_global_migrated_path(self.os, "cli-checkouts", "cli-checkouts")
400409
}
401410

402411
pub fn cli_bash_history(&self) -> Result<PathBuf> {
@@ -408,7 +417,7 @@ impl<'a> GlobalPaths<'a> {
408417
}
409418

410419
pub fn knowledge_bases_dir(&self) -> Result<PathBuf> {
411-
resolve_global_migrated_path(self.os, "knowledge_bases", "cli/knowledge_bases")
420+
resolve_global_migrated_path(self.os, "knowledge_bases", "knowledge_bases")
412421
}
413422

414423
pub async fn ensure_agents_dir(&self) -> Result<PathBuf> {
@@ -478,10 +487,10 @@ mod migration_tests {
478487
let mut fs = TestFileSystem::new();
479488
fs.add_path("/current/.kiro");
480489

481-
let home = Path::new("/home/user");
482-
let current = Path::new("/current");
490+
let kiro_base = Path::new("/current/.kiro");
491+
let amazonq_base = Path::new("/current/.amazonq");
483492

484-
let path = resolve_migrated_path_with_fs(&fs, home, current, false, "cli-agents", "agents");
493+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, false, "cli-agents", "agents");
485494
assert_eq!(path, Path::new("/current/.kiro/agents"));
486495
}
487496

@@ -490,10 +499,10 @@ mod migration_tests {
490499
let mut fs = TestFileSystem::new();
491500
fs.add_path("/current/.amazonq");
492501

493-
let home = Path::new("/home/user");
494-
let current = Path::new("/current");
502+
let kiro_base = Path::new("/current/.kiro");
503+
let amazonq_base = Path::new("/current/.amazonq");
495504

496-
let path = resolve_migrated_path_with_fs(&fs, home, current, false, "cli-agents", "agents");
505+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, false, "cli-agents", "agents");
497506
assert_eq!(path, Path::new("/current/.amazonq/cli-agents"));
498507
}
499508

@@ -503,10 +512,10 @@ mod migration_tests {
503512
fs.add_path("/current/.kiro");
504513
fs.add_path("/current/.amazonq");
505514

506-
let home = Path::new("/home/user");
507-
let current = Path::new("/current");
515+
let kiro_base = Path::new("/current/.kiro");
516+
let amazonq_base = Path::new("/current/.amazonq");
508517

509-
let path = resolve_migrated_path_with_fs(&fs, home, current, false, "cli-agents", "agents");
518+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, false, "cli-agents", "agents");
510519
// Should prefer .kiro when both exist
511520
assert_eq!(path, Path::new("/current/.kiro/agents"));
512521
}
@@ -515,10 +524,10 @@ mod migration_tests {
515524
fn test_neither_exist_workspace() {
516525
let fs = TestFileSystem::new();
517526

518-
let home = Path::new("/home/user");
519-
let current = Path::new("/current");
527+
let kiro_base = Path::new("/current/.kiro");
528+
let amazonq_base = Path::new("/current/.amazonq");
520529

521-
let path = resolve_migrated_path_with_fs(&fs, home, current, false, "cli-agents", "agents");
530+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, false, "cli-agents", "agents");
522531
// Should default to .kiro when neither exists
523532
assert_eq!(path, Path::new("/current/.kiro/agents"));
524533
}
@@ -528,10 +537,10 @@ mod migration_tests {
528537
let mut fs = TestFileSystem::new();
529538
fs.add_path("/home/user/.kiro");
530539

531-
let home = Path::new("/home/user");
532-
let current = Path::new("/current");
540+
let kiro_base = Path::new("/home/user/.kiro");
541+
let amazonq_base = Path::new("/home/user/.aws/amazonq");
533542

534-
let path = resolve_migrated_path_with_fs(&fs, home, current, true, "cli-agents", "agents");
543+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, true, "cli-agents", "agents");
535544
assert_eq!(path, Path::new("/home/user/.kiro/agents"));
536545
}
537546

@@ -540,10 +549,10 @@ mod migration_tests {
540549
let mut fs = TestFileSystem::new();
541550
fs.add_path("/home/user/.aws/amazonq");
542551

543-
let home = Path::new("/home/user");
544-
let current = Path::new("/current");
552+
let kiro_base = Path::new("/home/user/.kiro");
553+
let amazonq_base = Path::new("/home/user/.aws/amazonq");
545554

546-
let path = resolve_migrated_path_with_fs(&fs, home, current, true, "cli-agents", "agents");
555+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, true, "cli-agents", "agents");
547556
assert_eq!(path, Path::new("/home/user/.aws/amazonq/cli-agents"));
548557
}
549558

@@ -553,10 +562,10 @@ mod migration_tests {
553562
fs.add_path("/home/user/.kiro");
554563
fs.add_path("/home/user/.aws/amazonq");
555564

556-
let home = Path::new("/home/user");
557-
let current = Path::new("/current");
565+
let kiro_base = Path::new("/home/user/.kiro");
566+
let amazonq_base = Path::new("/home/user/.aws/amazonq");
558567

559-
let path = resolve_migrated_path_with_fs(&fs, home, current, true, "cli-agents", "agents");
568+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, true, "cli-agents", "agents");
560569
// Should prefer .kiro when both exist
561570
assert_eq!(path, Path::new("/home/user/.kiro/agents"));
562571
}
@@ -565,10 +574,10 @@ mod migration_tests {
565574
fn test_neither_exist_global() {
566575
let fs = TestFileSystem::new();
567576

568-
let home = Path::new("/home/user");
569-
let current = Path::new("/current");
577+
let kiro_base = Path::new("/home/user/.kiro");
578+
let amazonq_base = Path::new("/home/user/.aws/amazonq");
570579

571-
let path = resolve_migrated_path_with_fs(&fs, home, current, true, "cli-agents", "agents");
580+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, true, "cli-agents", "agents");
572581
// Should default to .kiro when neither exists
573582
assert_eq!(path, Path::new("/home/user/.kiro/agents"));
574583
}
@@ -578,12 +587,13 @@ mod migration_tests {
578587
let mut fs = TestFileSystem::new();
579588
fs.add_path("/current/.amazonq");
580589

581-
let home = Path::new("/home/user");
582-
let current = Path::new("/current");
590+
let kiro_base = Path::new("/current/.kiro");
591+
let amazonq_base = Path::new("/current/.amazonq");
583592

584-
let agents_path = resolve_migrated_path_with_fs(&fs, home, current, false, "cli-agents", "agents");
585-
let prompts_path = resolve_migrated_path_with_fs(&fs, home, current, false, "prompts", "prompts");
586-
let mcp_path = resolve_migrated_path_with_fs(&fs, home, current, false, "mcp.json", "settings/mcp.json");
593+
let agents_path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, false, "cli-agents", "agents");
594+
let prompts_path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, false, "prompts", "prompts");
595+
let mcp_path =
596+
resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, false, "mcp.json", "settings/mcp.json");
587597

588598
assert_eq!(agents_path, Path::new("/current/.amazonq/cli-agents"));
589599
assert_eq!(prompts_path, Path::new("/current/.amazonq/prompts"));
@@ -595,11 +605,17 @@ mod migration_tests {
595605
let mut fs = TestFileSystem::new();
596606
fs.add_path("/home/user/.kiro");
597607

598-
let home = Path::new("/home/user");
599-
let current = Path::new("/current");
600-
601-
let path =
602-
resolve_migrated_path_with_fs(&fs, home, current, true, "global_context.json", "global_context.json");
608+
let kiro_base = Path::new("/home/user/.kiro");
609+
let amazonq_base = Path::new("/home/user/.aws/amazonq");
610+
611+
let path = resolve_migrated_path_with_fs(
612+
&fs,
613+
kiro_base,
614+
amazonq_base,
615+
true,
616+
"global_context.json",
617+
"global_context.json",
618+
);
603619
assert_eq!(path, Path::new("/home/user/.kiro/global_context.json"));
604620
}
605621

@@ -608,10 +624,11 @@ mod migration_tests {
608624
let mut fs = TestFileSystem::new();
609625
fs.add_path("/home/user/.aws/amazonq");
610626

611-
let home = Path::new("/home/user");
612-
let current = Path::new("/current");
627+
let kiro_base = Path::new("/home/user/.kiro");
628+
let amazonq_base = Path::new("/home/user/.aws/amazonq");
613629

614-
let path = resolve_migrated_path_with_fs(&fs, home, current, true, "knowledge_bases", "cli/knowledge_bases");
630+
let path =
631+
resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, true, "knowledge_bases", "knowledge_bases");
615632
assert_eq!(path, Path::new("/home/user/.aws/amazonq/knowledge_bases"));
616633
}
617634

@@ -620,10 +637,35 @@ mod migration_tests {
620637
let mut fs = TestFileSystem::new();
621638
fs.add_path("/current/.kiro");
622639

623-
let home = Path::new("/home/user");
624-
let current = Path::new("/current");
640+
let kiro_base = Path::new("/current/.kiro");
641+
let amazonq_base = Path::new("/current/.amazonq");
625642

626-
let path = resolve_migrated_path_with_fs(&fs, home, current, false, "rules", "rules");
643+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, false, "rules", "rules");
627644
assert_eq!(path, Path::new("/current/.kiro/rules"));
628645
}
646+
647+
#[test]
648+
fn test_data_dir_usage_for_knowledge_bases() {
649+
let mut fs = TestFileSystem::new();
650+
fs.add_path("/data/kiro-cli");
651+
652+
let kiro_base = Path::new("/data/kiro-cli");
653+
let amazonq_base = Path::new("/home/user/.aws/amazonq");
654+
655+
let path =
656+
resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, true, "knowledge_bases", "knowledge_bases");
657+
assert_eq!(path, Path::new("/data/kiro-cli/knowledge_bases"));
658+
}
659+
660+
#[test]
661+
fn test_data_dir_usage_for_cli_checkouts() {
662+
let mut fs = TestFileSystem::new();
663+
fs.add_path("/data/kiro-cli");
664+
665+
let kiro_base = Path::new("/data/kiro-cli");
666+
let amazonq_base = Path::new("/home/user/.aws/amazonq");
667+
668+
let path = resolve_migrated_path_with_fs(&fs, kiro_base, amazonq_base, true, "cli-checkouts", "cli-checkouts");
669+
assert_eq!(path, Path::new("/data/kiro-cli/cli-checkouts"));
670+
}
629671
}

0 commit comments

Comments
 (0)