mpeterv / luacheck

A tool for linting and static analysis of Lua code.
MIT License
1.92k stars 322 forks source link

Different warnings reported if different files given on command line #154

Closed Zash closed 6 years ago

Zash commented 6 years ago

So in Prosody as of r6aeed79d9283 this weirdness happens:

trunk$ luacheck --no-cache prosody
Checking prosody                                  6 warnings

    prosody:63:3: (W113) accessing undefined variable log
    prosody:66:4: (W113) accessing undefined variable log
    prosody:74:25: (W113) accessing undefined variable server
    prosody:80:2: (W113) accessing undefined variable log
    prosody:86:1: (W113) accessing undefined variable log
    prosody:89:1: (W113) accessing undefined variable log

Total: 6 warnings / 0 errors in 1 file

Ok, not so weird. But then when also giving the default config file:

trunk$ luacheck --no-cache prosody prosody.cfg.lua.dist
Checking prosody                                  1 warning

    prosody:74:25: (W113) accessing undefined variable server

Checking prosody.cfg.lua.dist                     11 warnings

    prosody.cfg.lua.dist:24:1: (W131) unused global variable admins
    prosody.cfg.lua.dist:38:1: (W131) unused global variable modules_enabled
    prosody.cfg.lua.dist:85:1: (W131) unused global variable modules_disabled
    prosody.cfg.lua.dist:94:1: (W131) unused global variable allow_registration
    prosody.cfg.lua.dist:99:1: (W131) unused global variable c2s_require_encryption
    prosody.cfg.lua.dist:105:1: (W131) unused global variable s2s_require_encryption
    prosody.cfg.lua.dist:114:1: (W131) unused global variable s2s_secure_auth
    prosody.cfg.lua.dist:135:1: (W131) unused global variable authentication
    prosody.cfg.lua.dist:156:1: (W131) unused global variable archive_expires_after
    prosody.cfg.lua.dist:182:1: (W131) unused global variable certificates
    prosody.cfg.lua.dist:188:1: (W113) accessing undefined variable VirtualHost

Total: 12 warnings / 0 errors in 2 files

Only one warning for prosody now?

(At the time of this writing, it is very late. Too late to start with attempting to reduce this to a smaller test case. I may try that another day.)

mpeterv commented 6 years ago

Prosody .luacheckrc has allow_defined_top option enabled, so that all globals assigned in all main chunks are considered defined. This works across files unless global scope is limited using module option. prosody.cfg.lua.dist file sets log so when it's checked together with prosody there are no longer warnings about accessing that global.

allow_defined and allow_defined_top are the only options with behavior working across files. Ideally there should be no such behavior or there should be a config option telling luacheck what files to check, so that there is no result mismatch when checking the whole project (e.g. as a part of CI) vs checking single files (e.g. via an editor plugin).

Zash commented 6 years ago

Does having allow_defined_top only apply to prosody make more sense then?

mpeterv commented 6 years ago

I'm not sure, I haven't looked at the files in detail. If allow_defined_top is set only in prosody, other files will be able to use globals set in prosody without warnings when checked together with it. Is that the intention?

Zash commented 6 years ago

That sounds like it makes some sense, yes. I was unaware that luacheck considered variables across files in this way.