basic-memory icon indicating copy to clipboard operation
basic-memory copied to clipboard

[BUG] BASIC_MEMORY_HOME env var is not respected

Open phernandez opened this issue 1 year ago • 4 comments

Bug Description

Configuring the BASIC_MEMORY_HOME env var does not set the root dir for basic memory

Example:

"basic-memory": {
      "command": "uvx",
      "args": ["basic-memory", "mcp"],
      "env": {
        "BASIC_MEMORY_HOME": "~/project/.basic-memory"
      }
    }

files are still created in ~/basic-memory

phernandez avatar Jul 10 '25 01:07 phernandez

@claude why isn't the env var working in the config.py?

phernandez avatar Jul 10 '25 01:07 phernandez

Claude finished @phernandez's task —— View job • claude/issue-219-20250710_012005 • Create PR ➔


Fixed BASIC_MEMORY_HOME Environment Variable Issue ✅

Todo List:

  • [x] Read config.py to understand environment variable handling
  • [x] Check how home directory is determined
  • [x] Identify the bug causing BASIC_MEMORY_HOME to be ignored
  • [x] Implement fix for the issue
  • [x] Add comprehensive tests for the fix
  • [x] Commit and push changes

Root Cause Identified

The issue was in the ConfigManager.load_config() method (src/basic_memory/config.py:177-192). When a config file existed, BasicMemoryConfig(**data) was called with data from the JSON file, which bypassed Pydantic's environment variable parsing. The BASIC_MEMORY_HOME env var only worked when there was no existing config file.

The Fix

Modified ConfigManager.load_config() to explicitly check for BASIC_MEMORY_HOME environment variable after loading from file and override the main project path if set:

# Allow BASIC_MEMORY_HOME environment variable to override the main project path
env_home = os.getenv("BASIC_MEMORY_HOME")
if env_home:
    config.projects["main"] = str(Path(env_home))

This ensures that BASIC_MEMORY_HOME takes precedence over the file-based configuration for the main project path, allowing users to override their configuration temporarily using environment variables.

Tests Added

Added comprehensive tests in TestConfigManager class:

  1. test_load_config_respects_basic_memory_home_with_existing_file - verifies BASIC_MEMORY_HOME overrides file-based config
  2. test_load_config_without_basic_memory_home_uses_file_config - verifies fallback to file-based config when env var is not set

Changes Made

  • Modified: src/basic_memory/config.py - Added environment variable override logic
  • Added: New test class TestConfigManager in tests/test_config.py

claude[bot] avatar Jul 10 '25 01:07 claude[bot]

Oh Claude, at least you tried...

@phernandez @groksrc

The root issue here is that the FileService object that actually writes the files to disk, uses the value of the path field in the project table in the database to decide where to write the file. The value in the Config is not used. We can't just change the value of the path in the database, because then any existing files would not longer be able to be found.

So, one possible way forward is to:

  1. Remove the BASIC_MEMORY_HOME env var and the config
  2. Create a cli command for changing the root path of a BM project which will update the path for the project in the database, ~~and move all the notes for all the projects to the new project dir.~~
    1. On paul's suggestion, just prompt the user to move the files from the old location to their new location (BM cli just updates the database)
  3. Since project paths are no longer in the config file, it might be helpful if we had a tool or cli command to show the projects and their file system locations, since that would no longer be available in the config file.

jope-bm avatar Jul 16 '25 16:07 jope-bm

Nice. the project info is available via

➜  ~ bm project list
                                          Basic Memory Projects
┏━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━━━┓
┃ Name             ┃ Path                                                             ┃ Default ┃ Active ┃
┡━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━━━┩
│ main             │ ~/basic-memory                                                   │ ✓       │ ✓      │
│ personal         │ ~/Library/Mobile Documents/iCloud~md~obsidian/Documents/personal │         │        │
│ chat-test        │ ~/chat-test                                                      │         │        │
│ basic-memory-llc │ ~/dev/basicmachines/basic-memory-llc                             │         │        │
└──────────────────┴──────────────────────────────────────────────────────────────────┴─────────┴────────┘
➜  ~

phernandez avatar Jul 16 '25 20:07 phernandez