From 53aed1d6c7ae4c1f139549bdcbbeddd0b8f49390 Mon Sep 17 00:00:00 2001 From: Predrag Radenkovic Date: Thu, 2 Jul 2026 23:05:15 +0200 Subject: [PATCH] Fix module resolution for relative subdirectory paths The top module is opened via template_dirs[0] (the file's own directory), so its module_name must be the bare stem. bc6563b kept the relative subdirectory prefix, which double-joined with template_dirs[0] and made 'python plain2code.py sub/dir/foo.plain' fail with 'Module does not exist'. Add a regression test replicating the CLI's path decomposition. --- plain_file.py | 6 +----- tests/test_plainfileparser.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/plain_file.py b/plain_file.py index c116d33..317bf1d 100644 --- a/plain_file.py +++ b/plain_file.py @@ -651,11 +651,7 @@ def plain_file_parser( # noqa: C901 f"Plain syntax error: Invalid plain file extension: {plain_source_file_path.suffix}. Expected: {PLAIN_SOURCE_FILE_EXTENSION}." ) - module_name = ( - plain_source_file_path.stem - if plain_source_file_path.is_absolute() - else plain_source_file_path.with_suffix("").as_posix() - ) + module_name = plain_source_file_path.stem code_variables = {} diff --git a/tests/test_plainfileparser.py b/tests/test_plainfileparser.py index 09f8b5f..45938a5 100644 --- a/tests/test_plainfileparser.py +++ b/tests/test_plainfileparser.py @@ -27,6 +27,19 @@ def test_regular_plain_source(get_test_data_path): } +def test_relative_subdirectory_path_resolves(get_test_data_path): + # Regression: the CLI passes the full path as the filename and its directory as template_dirs[0]. + # For a relative path with subdirectories, module_name must be the bare stem; otherwise the + # subdirectory prefix double-joins with template_dirs[0] and the module is reported as missing. + data_dir = get_test_data_path("data/plainfileparser") + rel_file = os.path.relpath(os.path.join(data_dir, "regular_plain_source.plain")) + assert os.sep in rel_file # ensure the relative path actually has a subdirectory prefix + + module_name, _, _ = plain_file.plain_file_parser(rel_file, [os.path.dirname(rel_file)]) + + assert module_name == "regular_plain_source" + + def test_unknown_section(): plain_source = """ ***definitions***