Skip to content

"Fixed" tracing bug if module name and current working directory are identically named - #137

Merged
Essoz merged 5 commits into
mainfrom
daniel/module-name-bug
Jun 23, 2026
Merged

"Fixed" tracing bug if module name and current working directory are identically named#137
Essoz merged 5 commits into
mainfrom
daniel/module-name-bug

Conversation

@danielryckman

Copy link
Copy Markdown
Collaborator

For Python projects named reponame/packagename where reponame equals packagename (i.e. torchtitan/torchtitan), .split() will improperly split at the first instance in the string. The path after the root module will then still have module name on it. For example, it splits "/home/daniel/torchtitan/torchtitan/config/manager.py" into ['/home/daniel', 'torchtitan/config/manager.py'] instead of ['/home/daniel/torchtitan', 'config/manager.py']. Thus, after prepending the module name, we get torchtitan/torchtitan/config/manager.py, which doesn't exist.

@danielryckman
danielryckman requested a review from EssozJune 4, 2026 18:25
@Essoz

Essoz commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

✅ The fix solves the immediate problem, but I share your concern about potential edge cases.

Potential issue:
This still relies on string matching for the module separator pattern f"/{root_module}/". If a root module has the same name as a submodule or class within it (e.g., a package structure like mylib.submodule.mylib), the rsplit could still grab the wrong segment.

Better approach: Instead of parsing the file path string directly, we:

Determine the absolute path of the root module (using importlib.util.find_spec() or pathlib)
Use pathlib.Path.relative_to() to remove the root module path from the file path
Convert the resulting relative path to a module path
This avoids string-based parsing altogether and is much more robust to naming collisions.

Example Code (you can probably use a coding agent to help you implement this, should be a pretty lightweight change).

frompathlibimportPathfromimportlib.utilimportfind_specdefget_module_path_from_file_path(file_path: str, root_module: str) ->str|None:
""" Get the module path from a file path using importlib and pathlib. """try:
# Find the root module's locationspec=find_spec(root_module)
ifspecisNoneorspec.originisNone:
returnNoneroot_module_path=Path(spec.origin).parentfile_path_obj=Path(file_path).resolve()
# Get the relative path from root module to filetry:
relative_path=file_path_obj.relative_to(root_module_path)
exceptValueError:
# file_path is not under the root modulereturnNone# Convert to module pathmodule_name=str(relative_path.with_suffix(''))
module_path=f"{root_module}.{module_name.replace('/', '.')}"returnmodule_pathexceptException:
returnNone

This approach is import-aware and doesn't rely on fragile string parsing.

Comment threadtraincheck/instrumentor/tracer.py Outdated
@danielryckman
danielryckmanforce-pushed the daniel/module-name-bug branch from 173297e to b6b0e5bCompareJune 9, 2026 09:52
@danielryckman
danielryckman requested a review from EssozJune 15, 2026 19:08
@Essoz
Essoz merged commit 6c4f006 into mainJun 23, 2026
0 of 2 checks passed
Sign up for freeto 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.

2 participants

@danielryckman@Essoz