Skip to content

[Code scan] MultiSystems.from_dir silently returns empty results for absolute directories #986

Description

@njzjz

This issue is part of a Codex global repository code scan.

MultiSystems.from_dir() prepends ./ to dir_name before globbing. When dir_name is already absolute, the search path becomes relative to the current working directory, so valid absolute directories can silently return an empty MultiSystems.

Affected code:

dpdata/dpdata/system.py

Lines 1478 to 1492 in a7a50bf

def from_dir(
cls,
dir_name: str,
file_name: str,
fmt: str = "auto",
type_map: list[str] | None = None,
):
multi_systems = cls()
target_file_list = sorted(
glob.glob(f"./{dir_name}/**/{file_name}", recursive=True)
)
for target_file in target_file_list:
multi_systems.append(
LabeledSystem(file_name=target_file, fmt=fmt, type_map=type_map)
)

Minimal reproducer from the repository root:

from pathlib import Path
import dpdata

rel = dpdata.MultiSystems.from_dir("tests/poscars", "OUTCAR.h2o.md", fmt="vasp/outcar")
print("relative", len(rel), rel.get_nframes())

absdir = str(Path("tests/poscars").resolve())
abs_ms = dpdata.MultiSystems.from_dir(absdir, "OUTCAR.h2o.md", fmt="vasp/outcar")
print("absolute", len(abs_ms), abs_ms.get_nframes())

Current output:

relative 1 3
absolute 0 0

Expected behavior: absolute and relative directory arguments should search the same target directory and return the same systems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions