Skip to content

Fix/duplicate import error throwing#157

Open
ermijeremy wants to merge 6 commits intotrueagi-io:mainfrom
ermijeremy:fix/duplicate_import_error_throwing
Open

Fix/duplicate import error throwing#157
ermijeremy wants to merge 6 commits intotrueagi-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

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.

Shielding against duplicate import!

2 participants