@@ -316,63 +316,43 @@ def run_import(
316
316
"""Run the import process for Jac modules."""
317
317
unique_loaded_items : list [types .ModuleType ] = []
318
318
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
339
320
jacpaths = os .environ .get ("JACPATH" , "" )
321
+ search_paths = [spec .caller_dir ]
340
322
if jacpaths :
341
323
for p in jacpaths .split (os .pathsep ):
342
324
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
+ )
362
345
if os .path .isfile (spec .full_target + ".jac" ):
363
346
module_name = self .get_sys_mod_name (spec .full_target + ".jac" )
347
+ module_name = spec .override_name if spec .override_name else module_name
364
348
else :
365
349
module_name = self .get_sys_mod_name (spec .full_target )
366
350
367
- module_name = spec .override_name if spec .override_name else module_name
368
-
369
- # Check if module is already loaded
370
351
module = self .jac_machine .loaded_modules .get (module_name )
371
352
372
- # Load module if not already loaded or if reload is requested
373
353
if not module or module .__name__ == "__main__" or reload :
374
354
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 )
376
356
else :
377
357
spec .full_target += ".jac" if spec .language == "jac" else ".py"
378
358
module = self .create_jac_py_module (
@@ -388,6 +368,7 @@ def run_import(
388
368
reload = reload if reload else False ,
389
369
)
390
370
371
+ # Since this is a compile time error, we can safely raise an exception here.
391
372
if not codeobj :
392
373
raise ImportError (f"No bytecode found for { spec .full_target } " )
393
374
0 commit comments