Skip to content

Commit 4938c18

Browse files
committed
Don't fail if GLOBAL_CONFIG cannot be calculated
1 parent 71f859e commit 4938c18

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

pylsp_black/plugin.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
import logging
22
import os
33
from pathlib import Path
4-
from typing import Dict
4+
from typing import Dict, Optional
55

66
import black
77
import toml
88
from pylsp import hookimpl
99

1010
logger = logging.getLogger(__name__)
1111

12-
GLOBAL_CONFIG: Path
13-
if os.name == "nt":
14-
GLOBAL_CONFIG = Path.home() / ".black"
15-
elif "XDG_CONFIG_HOME" in os.environ:
16-
GLOBAL_CONFIG = Path(os.environ["XDG_CONFIG_HOME"]) / "black"
17-
else:
18-
GLOBAL_CONFIG = Path.home() / ".config" / "black"
12+
GLOBAL_CONFIG: Optional[Path] = None
13+
try:
14+
if os.name == "nt":
15+
GLOBAL_CONFIG = Path.home() / ".black"
16+
elif "XDG_CONFIG_HOME" in os.environ:
17+
GLOBAL_CONFIG = Path(os.environ["XDG_CONFIG_HOME"]) / "black"
18+
else:
19+
GLOBAL_CONFIG = Path.home() / ".config" / "black"
20+
except Exception as e:
21+
logger.error("Error determining black global config file path: %s", e)
1922

2023

2124
@hookimpl(tryfirst=True)
@@ -92,7 +95,7 @@ def load_config(filename: str) -> Dict:
9295
pyproject_filename = root / "pyproject.toml"
9396

9497
if not pyproject_filename.is_file():
95-
if GLOBAL_CONFIG.exists():
98+
if GLOBAL_CONFIG is not None and GLOBAL_CONFIG.exists():
9699
pyproject_filename = GLOBAL_CONFIG
97100
logger.info("Using global black config at %s", pyproject_filename)
98101
else:

0 commit comments

Comments
 (0)