Skip to content

Commit 8554c4e

Browse files
Refactor import path resolution and cleanup debug print statements
1 parent c2daeb7 commit 8554c4e

File tree

3 files changed

+25
-52
lines changed

3 files changed

+25
-52
lines changed

jac/jaclang/compiler/absyntree.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,11 +1013,9 @@ def resolve_relative_path(self, target_item: Optional[str] = None) -> str:
10131013
target = self.dot_path_str
10141014
if target_item:
10151015
target += f".{target_item}"
1016-
print("in resolve_relative_path")
10171016
base_path = (
10181017
os.getenv("JACPATH") or os.path.dirname(self.loc.mod_path) or os.getcwd()
10191018
)
1020-
print("base_path", base_path)
10211019
parts = target.split(".")
10221020
traversal_levels = self.level - 1 if self.level > 0 else 0
10231021
actual_parts = parts[traversal_levels:]
@@ -1029,7 +1027,6 @@ def resolve_relative_path(self, target_item: Optional[str] = None) -> str:
10291027
if os.path.exists(relative_path + ".jac")
10301028
else relative_path
10311029
)
1032-
print(f"relative: {relative_path}")
10331030
return relative_path
10341031

10351032
def normalize(self, deep: bool = False) -> bool:

jac/jaclang/compiler/passes/main/import_pass.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,12 @@ def import_jac_module(self, node: ast.ModulePath) -> None:
151151
i, self.import_jac_mod_from_file(from_mod_target)
152152
)
153153
else:
154-
print("I'm before attach")
155154
self.attach_mod_to_node(node, self.import_jac_mod_from_file(target))
156155

157156
def import_jac_mod_from_dir(self, target: str) -> ast.Module | None:
158157
"""Import a module from a directory."""
159158
with_init = os.path.join(target, "__init__.jac")
160159
if os.path.exists(with_init):
161-
print(f"Importing in the dir ")
162160
return self.import_jac_mod_from_file(with_init)
163161
else:
164162
return ast.Module(
@@ -177,11 +175,8 @@ def import_jac_mod_from_file(self, target: str) -> ast.Module | None:
177175
from jaclang.compiler.compile import jac_file_to_pass
178176
from jaclang.compiler.passes.main import SubNodeTabPass
179177

180-
print(f"Importing {target}")
181178
if not os.path.exists(target):
182-
print("I'm before error")
183179
self.error(f"Could not find module {target}")
184-
print("I'm after error")
185180
return None
186181
if target in self.import_table:
187182
return self.import_table[target]

jac/jaclang/runtimelib/importer.py

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -316,63 +316,43 @@ def run_import(
316316
"""Run the import process for Jac modules."""
317317
unique_loaded_items: list[types.ModuleType] = []
318318
module = None
319-
320-
# Construct search paths
321-
search_paths = []
322-
# Process base_path and include all its parent directories
323-
if isinstance(spec.base_path, str):
324-
base_path = os.path.abspath(spec.base_path)
325-
while base_path and base_path not in search_paths:
326-
search_paths.append(base_path)
327-
base_path = os.path.dirname(base_path)
328-
elif isinstance(spec.base_path, list):
329-
for path_item in spec.base_path:
330-
base_path = os.path.abspath(path_item)
331-
while base_path and base_path not in search_paths:
332-
search_paths.append(base_path)
333-
base_path = os.path.dirname(base_path)
334-
335-
# Add caller_dir
336-
search_paths.append(os.path.abspath(spec.caller_dir))
337-
338-
# Add directories from JACPATH
319+
# Gather all possible search paths
339320
jacpaths = os.environ.get("JACPATH", "")
321+
search_paths = [spec.caller_dir]
340322
if jacpaths:
341323
for p in jacpaths.split(os.pathsep):
342324
p = p.strip()
343-
if p and os.path.isdir(p) and os.path.abspath(p) not in search_paths:
344-
search_paths.append(os.path.abspath(p))
345-
346-
# Locate the module
347-
found_path = None
348-
target_path_components = spec.target.split(".")
349-
for search_path in search_paths:
350-
candidate = os.path.join(search_path, *target_path_components)
351-
if os.path.isdir(candidate) or os.path.isfile(candidate + ".jac"):
352-
found_path = candidate
353-
break
354-
355-
if not found_path:
356-
raise ImportError(
357-
f"Unable to locate module '{spec.target}' in {search_paths}"
358-
)
359-
360-
spec.full_target = os.path.abspath(found_path)
361-
# Determine the module name using `get_sys_mod_name`
325+
if p and p not in search_paths:
326+
search_paths.append(p)
327+
328+
# Attempt to locate the module file or directory
329+
found_path = None
330+
target_path_components = spec.target.split(".")
331+
for search_path in search_paths:
332+
candidate = os.path.join(search_path, "/".join(target_path_components))
333+
# Check if the candidate is a directory or a .jac file
334+
if (os.path.isdir(candidate)) or (os.path.isfile(candidate + ".jac")):
335+
found_path = candidate
336+
break
337+
338+
# If a suitable path was found, update spec.full_target; otherwise, raise an error
339+
if found_path:
340+
spec.full_target = os.path.abspath(found_path)
341+
else:
342+
raise ImportError(
343+
f"Unable to locate module '{spec.target}' in {search_paths}"
344+
)
362345
if os.path.isfile(spec.full_target + ".jac"):
363346
module_name = self.get_sys_mod_name(spec.full_target + ".jac")
347+
module_name = spec.override_name if spec.override_name else module_name
364348
else:
365349
module_name = self.get_sys_mod_name(spec.full_target)
366350

367-
module_name = spec.override_name if spec.override_name else module_name
368-
369-
# Check if module is already loaded
370351
module = self.jac_machine.loaded_modules.get(module_name)
371352

372-
# Load module if not already loaded or if reload is requested
373353
if not module or module.__name__ == "__main__" or reload:
374354
if os.path.isdir(spec.full_target):
375-
module = self.handle_directory(module_name, spec.full_target)
355+
module = self.handle_directory(spec.module_name, spec.full_target)
376356
else:
377357
spec.full_target += ".jac" if spec.language == "jac" else ".py"
378358
module = self.create_jac_py_module(
@@ -388,6 +368,7 @@ def run_import(
388368
reload=reload if reload else False,
389369
)
390370

371+
# Since this is a compile time error, we can safely raise an exception here.
391372
if not codeobj:
392373
raise ImportError(f"No bytecode found for {spec.full_target}")
393374

0 commit comments

Comments
 (0)