Skip to content

Commit cbe72be

Browse files
phernandezclaude
andcommitted
fix: make set_default_project also activate project for current session to fix #37
This change makes the 'basic-memory project default <name>' command both: 1. Set the default project for future invocations (persistent change) 2. Activate the project for the current session (immediate change) Added tests to verify this behavior, which resolves issue #37 where the project name and path weren't changing properly when the default project was changed. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0ade6b0 commit cbe72be

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

src/basic_memory/cli/commands/project.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,22 @@ def remove_project(
9292
def set_default_project(
9393
name: str = typer.Argument(..., help="Name of the project to set as default"),
9494
) -> None:
95-
"""Set the default project."""
95+
"""Set the default project and activate it for the current session."""
9696
config_manager = ConfigManager()
9797

9898
try:
99+
# Set the default project
99100
config_manager.set_default_project(name)
100-
console.print(f"[green]Project '{name}' set as default[/green]")
101+
102+
# Also activate it for the current session by setting the environment variable
103+
os.environ["BASIC_MEMORY_PROJECT"] = name
104+
105+
# Reload configuration to apply the change
106+
from importlib import reload
107+
from basic_memory import config as config_module
108+
reload(config_module)
109+
110+
console.print(f"[green]Project '{name}' set as default and activated[/green]")
101111
except ValueError as e: # pragma: no cover
102112
console.print(f"[red]Error: {e}[/red]")
103113
raise typer.Exit(1)

tests/cli/test_project_commands.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515
@pytest.fixture
1616
def temp_home(monkeypatch):
1717
"""Create a temporary directory for testing."""
18+
# Save the original environment variable if it exists
19+
original_env = os.environ.get("BASIC_MEMORY_PROJECT")
20+
21+
# Clear environment variable for clean test
22+
if "BASIC_MEMORY_PROJECT" in os.environ:
23+
del os.environ["BASIC_MEMORY_PROJECT"]
24+
1825
with TemporaryDirectory() as tempdir:
1926
temp_home = Path(tempdir)
2027
monkeypatch.setattr(Path, "home", lambda: temp_home)
@@ -24,6 +31,12 @@ def temp_home(monkeypatch):
2431
config_dir.mkdir(parents=True, exist_ok=True)
2532

2633
yield temp_home
34+
35+
# Cleanup: restore original environment variable if it existed
36+
if original_env is not None:
37+
os.environ["BASIC_MEMORY_PROJECT"] = original_env
38+
elif "BASIC_MEMORY_PROJECT" in os.environ:
39+
del os.environ["BASIC_MEMORY_PROJECT"]
2740

2841

2942
@pytest.fixture
@@ -123,17 +136,33 @@ def test_project_default(cli_runner, temp_home):
123136
# Set as default
124137
result = cli_runner.invoke(app, ["project", "default", "test"])
125138
assert result.exit_code == 0
126-
assert "Project 'test' set as default" in result.stdout
139+
assert "Project 'test' set as default and activated" in result.stdout
127140

128141
# Verify default was set
129142
config_manager = ConfigManager()
130143
assert config_manager.default_project == "test"
144+
145+
# Extra verification: check if the environment variable was set
146+
assert os.environ.get("BASIC_MEMORY_PROJECT") == "test"
131147

132148

133149
def test_project_current(cli_runner, temp_home):
134150
"""Test showing the current project."""
151+
# Create a bare-bones config.json with main as the default project
152+
config_file = temp_home / DATA_DIR_NAME / CONFIG_FILE_NAME
153+
config_data = {
154+
"projects": {
155+
"main": str(temp_home / "basic-memory"),
156+
},
157+
"default_project": "main",
158+
}
159+
config_file.write_text(json.dumps(config_data))
160+
161+
# Create the main project directory
162+
main_dir = temp_home / "basic-memory"
163+
main_dir.mkdir(parents=True, exist_ok=True)
135164

136-
# Set as default
165+
# Now check the current project
137166
result = cli_runner.invoke(app, ["project", "current"])
138167
assert result.exit_code == 0
139168
assert "Current project: main" in result.stdout
@@ -156,3 +185,24 @@ def test_project_option(cli_runner, temp_home, monkeypatch):
156185

157186
# Verify environment variable was set
158187
assert env_vars.get("BASIC_MEMORY_PROJECT") == "test"
188+
189+
190+
def test_project_default_activates_project(cli_runner, temp_home, monkeypatch):
191+
"""Test that setting the default project also activates it in the current session."""
192+
# Create a test environment
193+
env = {}
194+
monkeypatch.setattr(os, "environ", env)
195+
196+
# Create two test projects
197+
config_manager = ConfigManager()
198+
config_manager.add_project("project1", str(temp_home / "project1"))
199+
200+
# Set project1 as default using the CLI command
201+
result = cli_runner.invoke(app, ["project", "default", "project1"])
202+
assert result.exit_code == 0
203+
assert "Project 'project1' set as default and activated" in result.stdout
204+
205+
# Verify the environment variable was set
206+
# This is the core of our fix - the set_default_project command now also sets
207+
# the BASIC_MEMORY_PROJECT environment variable to activate the project
208+
assert env.get("BASIC_MEMORY_PROJECT") == "project1"

0 commit comments

Comments
 (0)