Skip to content

Fix/duplicate import error throwing#157

Open
ermijeremy wants to merge 10 commits into
trueagi-io:mainfrom
ermijeremy:fix/duplicate_import_error_throwing
Open

Fix/duplicate import error throwing#157
ermijeremy wants to merge 10 commits into
trueagi-io:mainfrom
ermijeremy:fix/duplicate_import_error_throwing

Conversation

@ermijeremy
Copy link
Copy Markdown
Contributor

@ermijeremy ermijeremy commented Apr 3, 2026

Related Issue

Closes #154

Summary

Prevent duplicate import! loads by tracking absolute import paths and ignoring duplicate imports.

What Changed

  • import! now normalizes every import to an absolute path.
    • .py imports resolve to an absolute path and import the Python module.
    • Other imports resolve to an absolute .metta path and load the MeTTa file.
  • Each imported absolute path is stored in a dynamic predicate imported_file/1.
  • If the same absolute path is imported again, it will be ignored`.

Why This Fixes the Issue

Previously, repeated import! calls could reload the same file multiple times, causing duplicated definitions and inconsistent behavior.
By normalizing to a canonical absolute filename and recording it in imported_file/1, any subsequent import of the same file is detected and rejected reliably.

How It Works (Mechanics)

  1. Resolve File to an absolute path.
  2. Check imported_file(AbsPath):
    • If found → ignore the duplicated import.
    • If not → load file and assertz(imported_file(AbsPath)).

Tests

  • Added minimal test that imports the same file twice in one MeTTa file and asserts an exception is raised.

Copy link
Copy Markdown
Collaborator

@patham9 patham9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much!
Only: there should be no import error being generated.
Often libraries depend on same core libraries, it is normal.
It only should ensure that no library is loaded twice.

@ermijeremy ermijeremy requested review from patham9 April 15, 2026 15:33
@ermijeremy
Copy link
Copy Markdown
Contributor Author

Thank you very much! Only: there should be no import error being generated. Often libraries depend on same core libraries, it is normal. It only should ensure that no library is loaded twice.

@patham9 it no longer throws error instead if a duplicate import is found it just ignores it
-> true

@ermijeremy
Copy link
Copy Markdown
Contributor Author

Thank you very much! Only: there should be no import error being generated. Often libraries depend on same core libraries, it is normal. It only should ensure that no library is loaded twice.

@patham9 @pisaev1 any updates!!

@patham9 patham9 added the Import label May 9, 2026
@patham9
Copy link
Copy Markdown
Collaborator

patham9 commented May 9, 2026

Thank you for your patience and I will take a look asap. There are multiple PR's that affect import handling, will try to get an overview first before merging.

patham9 added 2 commits May 9, 2026 03:30
…&add-atom

Allow relative imports / functions in add/remove-atom
Signed-off-by: Patrick Hammer <patham9@gmail.com>
@patham9 patham9 added this to the v1.1 milestone May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shielding against duplicate import!

3 participants