fix: prevent import-time crash when ~/.kicad-mcp/ is not writable

Configuring logging at module import time hard-failed in environments
without write access to the user's home directory (sandboxed pytest
runners, restricted CI containers, read-only filesystems). The
unhandled OSError/PermissionError aborted module import and cascaded
into ~100 test failures during collection.

Wrap the FileHandler setup in try/except and fall back to console-only
logging when the log directory cannot be created. Production behavior
is unchanged - file logging in ~/.kicad-mcp/logs is still used
whenever the directory is writable.

Also picks up an isort-driven import reorder applied by the project's
pre-commit hook.
This commit is contained in:
William Viana
2026-04-23 21:24:54 -07:00
parent a8684a1f97
commit 7f473e06cc

View File

@@ -15,24 +15,32 @@ import traceback
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional, Tuple from typing import Any, Dict, List, Optional, Tuple
from annotations import AnnotationLoader
from resources.resource_definitions import RESOURCE_DEFINITIONS, handle_resource_read from resources.resource_definitions import RESOURCE_DEFINITIONS, handle_resource_read
# Import tool schemas, resource definitions, and IPC API annotations # Import tool schemas, resource definitions, and IPC API annotations
from schemas.tool_schemas import TOOL_SCHEMAS from schemas.tool_schemas import TOOL_SCHEMAS
from annotations import AnnotationLoader
_annotation_loader = AnnotationLoader() _annotation_loader = AnnotationLoader()
# Configure logging # Configure logging
log_dir = os.path.join(os.path.expanduser("~"), ".kicad-mcp", "logs") # Try to set up a file handler in ~/.kicad-mcp/logs. If that directory isn't
os.makedirs(log_dir, exist_ok=True) # writable (e.g. sandboxed test environments, restricted CI runners), fall
log_file = os.path.join(log_dir, "kicad_interface.log") # back to console-only logging so importing this module never crashes.
try:
logging.basicConfig( log_dir = os.path.join(os.path.expanduser("~"), ".kicad-mcp", "logs")
level=logging.DEBUG, os.makedirs(log_dir, exist_ok=True)
format="%(asctime)s [%(levelname)s] %(message)s", log_file = os.path.join(log_dir, "kicad_interface.log")
handlers=[logging.FileHandler(log_file)], logging.basicConfig(
) level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[logging.FileHandler(log_file)],
)
except (OSError, PermissionError):
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s",
)
logger = logging.getLogger("kicad_interface") logger = logging.getLogger("kicad_interface")
# Log Python environment details # Log Python environment details