Skip to content

Commit

Permalink
fix: honor the current working directory when importing pdm_build.py
Browse files Browse the repository at this point in the history
Close #245

Signed-off-by: Frost Ming <[email protected]>
  • Loading branch information
frostming committed Jun 17, 2024
1 parent 9915cb6 commit ba98c85
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/pdm/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ def get_hooks(self) -> Iterable[BuildHookInterface]:
local_hook = self.config.build_config.custom_hook
if local_hook is not None:
yield cast(
BuildHookInterface, import_module_at_path(self.location / local_hook)
BuildHookInterface,
import_module_at_path(
self.location / local_hook, context=self.location
),
)

def call_hook(
Expand Down
6 changes: 5 additions & 1 deletion src/pdm/backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,18 @@ def replace_func(match: Match[str]) -> str:


def import_module_at_path(
src_path: str | Path, module_name: str = "_local"
src_path: str | Path, module_name: str = "_local", context: Path | None = None
) -> types.ModuleType:
"""Import a module from a given path."""
spec = importlib.util.spec_from_file_location(module_name, src_path)
if spec is None:
raise ValueError(f"Could not import module {module_name} from {src_path}")
if context is not None:
sys.path.insert(0, str(context.absolute()))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore
if context is not None:
sys.path.pop(0)
return module


Expand Down

0 comments on commit ba98c85

Please sign in to comment.