Skip to content

Fix module resolution for relative subdirectory paths#237

Open
pedjaradenkovic wants to merge 1 commit into
mainfrom
fix/relative-module-path
Open

Fix module resolution for relative subdirectory paths#237
pedjaradenkovic wants to merge 1 commit into
mainfrom
fix/relative-module-path

Conversation

@pedjaradenkovic

Copy link
Copy Markdown
Contributor

Problem

Running the CLI with a relative path that contains a subdirectory fails:

$ python plain2code.py examples/example_hello_world_python/hello_world_python.plain
Error: Module does not exist (examples/example_hello_world_python/hello_world_python).

Absolute paths and bare filenames work; only relative subdirectory paths break. This is a regression on main.

Root cause

The top module is opened via open_from(template_dirs, module_name + ".plain"), and get_template_directories always sets template_dirs[0] = dirname(filename). So module_name must be the bare stem.

bc6563b ("prevent stripping module subdirectory") changed the derivation to keep the relative subdirectory prefix for non-absolute paths:

module_name = stem if is_absolute() else with_suffix("").as_posix()

For a relative invocation this double-joins with template_dirs[0]:

  • template_dirs[0] = examples/example_hello_world_python
  • module_name = examples/example_hello_world_python/hello_world_python
  • join → examples/example_hello_world_python/examples/example_hello_world_python/hello_world_python.plain → not found

Since sibling import/requires modules must resolve relative to template_dirs[0], keeping a subdir in module_name is fundamentally incompatible with that design. Reverting to always-stem is the correct fix.

Fix

  • plain_file.py: module_name = plain_source_file_path.stem (always).
  • tests/test_plainfileparser.py: test_relative_subdirectory_path_resolves replicates the CLI's path decomposition (full relative path + its dir as template_dirs[0]). Verified it fails on the old code and passes on the fix.

Verification

  • Full suite: 216 passed.
  • Relative subfolder CLI invocation now works (both dry-run and parse).
  • Subfolder requires confirmed working end-to-end: top module in a subfolder with a sibling requires, and requires: [nested/leaf] in a deeper subfolder. Negative control (requires: [nested/DOES_NOT_EXIST]) errors on exactly that path, proving nested resolution is active.

Behavior note

Top-module build/conformance folders are keyed by the stem again (e.g. build/main) — the pre-bc6563b behavior. This reverts the subdir-nested build path introduced by bc6563b (which had no test and broke the primary CLI path).

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant