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

Class definitions get confused with same filenames across folders. #3018

Open
JJSax opened this issue Dec 31, 2024 · 2 comments
Open

Class definitions get confused with same filenames across folders. #3018

JJSax opened this issue Dec 31, 2024 · 2 comments

Comments

@JJSax
Copy link

JJSax commented Dec 31, 2024

How are you using the lua-language-server?

Visual Studio Code Extension (sumneko.lua)

Which OS are you using?

Windows

What is the issue affecting?

Diagnostics/Syntax Checking

Expected Behaviour

I have two files, both named the same but in different paths. Each file has its own class and table structure name, which differs from each other.

class A doesn't have method "MyMethod"
class B does
When creating an instance of the class B, and using MyMethod from class B it says that Class A has undefined field "MyMethod" despite being an instance of class B

Renaming class B's file name to something else stops the undefined field warning.

I expect the classes to have differentiation based on the ---@Class ClassName syntax, and not the file name.

Actual Behaviour

As you might expect from my description, it does give that warning until the file is renamed.

Reproduction steps

  1. Create a file "test/file.lua" with a class that doesn't implement "MyMethod"
  2. Create a file "file.lua" with a class that does implement "MyMethod"
  3. In your main file, require both, create an instance of both, and try to use MyMethod from instance from step 2
  4. Notice warning about class from step 1 not implementing MyMethod, despite not being used.

Additional Notes

In a small scale test, this didn't seem to happen. But in a full project, the program ran the method LLS says is undefined. Renaming the file and nothing else fixed it so it had something to do with that.

Log File

No response

@tomlau10
Copy link
Contributor

tomlau10 commented Jan 1, 2025

  • Have you tried to turn on runtime.pathStrict in your .luarc.json? (It is off by default)
    https://luals.github.io/wiki/settings/#runtimepathstrict
  • How did you require the 2 files?
    By require "file" and require "test.file" ?
  • Would it happen that your project root directory has the same name as your subdirectory? 😕
    • For example project root: C:\test\, and your files: C:\test\file.lua, C:\test\test\file.lua
    • I tested in this situation even though I turn on runtime.pathStrict, using require "test.file" it still matches C:\test\file.lua 🤦‍♂️
    • I don't know if this case is a bug or not, as I don't expect a ?.lua search path would match my parent directory's name...
      image

Suggestions

  • turn on runtime.pathStrict in your .luarc.json
  • and don't name your subdirectory the same as your project root

@JJSax
Copy link
Author

JJSax commented Jan 1, 2025

  • Have you tried to turn on runtime.pathStrict in your .luarc.json? (It is off by default)

I did not, but that alone seems fix the issue I was having, so far at least. Thanks for the quality reply!

  • How did you require the 2 files?
    By require "file" and require "test.file" ?

Yeah, I required them just like that, though it's possible I used parentheses like require("test") In my actual project it was affecting, I did not use parentheses. Additionally both these require calls were made in "main.lua" at the same level as the test folder.

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

No branches or pull requests

2 participants