Skip to content

constructor is dependent on load order, and works extremely inconsistently #1007

Closed

Description

@NyakoFox

With the 0.22.0 release, there are suddenly an extreme amount of warnings throughout our codebase which did not exist before; strange warnings such as:

local MyClass = class("MyClass") -- class uses the constructor attribute properly

return MyClass
--- different file
local myInstance = MyClass() -- WARNING: Cannot call expression of type `EditorDockTree`

and:

---@class MyOtherClass
---@field __instance MyClass
local MyOtherClass = class("MyOtherClass")

MyOtherClass.__instance = MyClass() -- WARNING: Cannot assign `self` to `MyClass`

They seem to be related to the order in which things are loaded by EmmyLua; Minimal reproducible examples are very hard for us to create as these issues are very ephemeral.

It seems like the problem occurs when the "constructor defining function" (what uses @[constructor()] is "loaded" after an actual call to it.

Nonetheless, attached are two MREs which solely differ in filename; one that always has the problem, and one that never has the problem.

warning.zip
no_warning.zip

Do note that, when testing, only the "main" file should be opened in your IDE; opening one of other two make the issue not occur.

Code_-_Insiders_Q9uvpUDt9Z.mp4

It does seem like these issues have existed since the "constructor" attribute was added, but warnings for "Cannot call expression" did not exist prior. This system has never worked consistently for us, which is frustrating when our project relies heavily on it. We have noticed that, before this update, the constructor attribute would refuse to work most of the time. It has just not triggered warnings until now.

Thank you.

Activity

  1. self-assigned this
    on Apr 3, 2026
  2. added theissue type on Apr 3, 2026
  3. xuhuanzy commented on Apr 3, 2026

    @xuhuanzy
    Member

    This is an issue with the order of analysis. When I designed it, I never considered declaring meta as a global function, so the current order makes sense to me. Fixing this would take a great deal of effort, and it isn’t where my development focus lies.

    I recommend declaring it as a local function and then require-ing it.

  4. added a commit that references this issue on Apr 5, 2026
    bd1dcec
  5. linked a pull request that will close this issueupdate #1017on Apr 5, 2026
  6. xuhuanzy commented on Apr 5, 2026

    @xuhuanzy
    Member

    Since the next release may come quite a bit later, you can pull #1017 and build it yourself to fix this issue.

  7. NyakoFox commented on Apr 6, 2026

    @NyakoFox
    Author

    Testing it, I have noticed the following things:

    • Everything by default is now nil unless return_mode is set to "self"; meaning "default" does not properly "fall back" to "self" I think.
    • When set to "self", it does work for most cases!
    • Class inheritence for constructors seems to no longer work. See the following example:
    ---@class MyInheritedClass : MyClass
    local MyInheritedClass = meta("MyInheritedClass")
    
    return MyInheritedClass
    local MyClass = require("2_myclass")
    local MyInheritedClass = require("2.5_myinheritedclass")
    
    local instance = MyInheritedClass("Test")

    MyInheritedClass will say "Cannot call expression of type MyInheritedClass.", as it inherits the init function from MyClass. Adding an explicit init function to MyInheritedClass does fix this, but this is not desirable behavior to have useless functions like that, in my opinion.

  8. xuhuanzy commented on Apr 6, 2026

    @xuhuanzy
    Member

    Example

  9. NyakoFox commented on Apr 6, 2026

    @NyakoFox
    Author

    The error seems to come from init having content, for example a print:

    Code_-_Insiders_VkM10Lli3Z.mp4

    MRE: mre.zip

  10. xuhuanzy commented on Apr 6, 2026

    @xuhuanzy
    Member
    1. The rollback issue has been fixed.
    2. Constructors are not inherited by default.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions