Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] importing .mojopkg runs its main() instead of only importing #1987

Closed
toiletsandpaper opened this issue Mar 20, 2024 · 1 comment
Closed
Assignees
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-repo Tag all issues with this label mojo-tooling Tag for all issues related to repl, lldb, lsp, vscode extension.

Comments

@toiletsandpaper
Copy link
Contributor

Bug description

When importing and calling something from .mojopkg - mojo runs only package's main, and not real main.

Steps to reproduce

I've written a minimal repro:

.
├── birds_package.mojopkg
├── main.mojo
└── simple_package
    ├── __init__.mojo
    └── birds.mojo

Where simple_package/birds.mojo is:

struct Bird:
    var name: String

    fn __init__(inout self, name: String):
        self.name = name

    fn fly(self):
        print("The bird " + self.name + " is flying")

fn main():
    var bird = Bird(name="Package")
    bird.fly()

and main.mojo:

from birds_package.birds import Bird

fn main():
    print('before pigeon')
    var b = Bird('pigeon')
    b.fly()
    print('after pigeon')

When I'm trying to import birds_package.mojopkg in main.mojo — my output is:

The bird Package is flying

And when simple_package as folder:

before pigeon
The bird pigeon is flying
after pigeon

Mojo package was created with

mojo package simple_package -o birds_package.mojopkg

and __init__.mojo is empty file

System information

❯ mojo -v
mojo 24.1.1 (0ab415f7)
❯ modular -v
modular 0.5.2 (6b3a04fd)
❯ modular host-info
  Host Information
  ================

  Target Triple: aarch64-unknown-macosx
  CPU: apple-m1
  CPU Features: aes, complxnum, crc, dotprod, fp-armv8, fp16fml, fullfp16, jsconv, lse, neon, pauth, ras, rcpc, rdm, sha2, sha3
@toiletsandpaper toiletsandpaper added bug Something isn't working mojo Issues that are related to mojo labels Mar 20, 2024
@ematejska ematejska added the mojo-tooling Tag for all issues related to repl, lldb, lsp, vscode extension. label Mar 21, 2024
@River707
Copy link
Contributor

River707 commented Mar 28, 2024

Thanks for the report! Mojo doesn't really have well defined support for defining a main inside of a package. We'll add an error an a follow-up release to just prevent this for now until we work through the ramifications of it.

@River707 River707 self-assigned this Mar 28, 2024
patrickdoc pushed a commit that referenced this issue May 2, 2024
The semantics of this are not well defined for mojo right now,
so it's cleaner to just disallow it. We can loosen this up when
we have cleaner semantics around entry points in packages.

Closes #1987

MODULAR_ORIG_COMMIT_REV_ID: 3203283b41bfd984cd183ba1a314c0c3ba13925d
@ematejska ematejska added the mojo-repo Tag all issues with this label label May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-repo Tag all issues with this label mojo-tooling Tag for all issues related to repl, lldb, lsp, vscode extension.
Projects
None yet
Development

No branches or pull requests

3 participants