Skip to content

Commit ba98c85

Browse files
committed
fix: honor the current working directory when importing pdm_build.py
Close #245 Signed-off-by: Frost Ming <me@frostming.com>
1 parent 9915cb6 commit ba98c85

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/pdm/backend/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ def get_hooks(self) -> Iterable[BuildHookInterface]:
134134
local_hook = self.config.build_config.custom_hook
135135
if local_hook is not None:
136136
yield cast(
137-
BuildHookInterface, import_module_at_path(self.location / local_hook)
137+
BuildHookInterface,
138+
import_module_at_path(
139+
self.location / local_hook, context=self.location
140+
),
138141
)
139142

140143
def call_hook(

src/pdm/backend/utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,18 @@ def replace_func(match: Match[str]) -> str:
152152

153153

154154
def import_module_at_path(
155-
src_path: str | Path, module_name: str = "_local"
155+
src_path: str | Path, module_name: str = "_local", context: Path | None = None
156156
) -> types.ModuleType:
157157
"""Import a module from a given path."""
158158
spec = importlib.util.spec_from_file_location(module_name, src_path)
159159
if spec is None:
160160
raise ValueError(f"Could not import module {module_name} from {src_path}")
161+
if context is not None:
162+
sys.path.insert(0, str(context.absolute()))
161163
module = importlib.util.module_from_spec(spec)
162164
spec.loader.exec_module(module) # type: ignore
165+
if context is not None:
166+
sys.path.pop(0)
163167
return module
164168

165169

0 commit comments

Comments
 (0)