Help:Lua debugging: Difference between revisions

Content deleted Content added
new key for Category:Module help: "Debugging" using HotCat
 
(14 intermediate revisions by 6 users not shown)
Line 1:
{{WikiProject Lua header}}
This help-page, '''Help:Lua debugging''', explains issues of writing [[Lua (programming language)|Lua script]] and [[debugging]] the [[source code]], to remove errors or improve performance. Because Lua is a "semi-compiled" interpreted language, it does not prescreen for all common syntax errors, nor detect misspelled variables, which are only found at runtime when seeing the "<span style="color:maroon">{{Script error</span>}}" message. Also, unlike [[wp:template]]s which can be run interactively by edit-preview, the Lua script must be tested by show-preview (or run preview) of another page which uses a template which <code>#invoke</code>'s the Lua module being edited. In general, make one small change at a time, do a run show-preview, and save after a few good changes to have a version to restore, in case terrible errors occur after numerous later changes. Note: the saving of a working copy can be done to an offline text file, rather than saving each version into the module revisions.
 
Other pre-compiled, or semi-compiled, languages can pinpoint the line number, or code phrase, where a syntax error, or undefined variable name, has been detected; however, that processing can require extra time to perform. Instead, it might be possible to use a [[lexical analysis]] tool, specifically for Lua script, to better detect and pinpoint misspelled variables or logic errors in Lua source code.
Line 25:
To see the value of a variable in a single point of the module:
 
====ModifyingTo see the value of a variable by modifying the code====
The <code>error()</code> function (forcan be used to show the value of a variable at any point in the module) can be used. So to know the avalue variableof (nameda herevariable <code>var</code>) value,at onlya addingcertain a linepoint in the modulecode, withadd the line <code>error(var)</code> isat requiredthat point.
 
But inIn the case that the variable wasis a table (let's call the variable <code>tab</code>), in the next explanation) <code>mw.dumpObject(tab)</code> willcan be used or,to ifshow itthe table. If the variable does not have nested tables, <code>table.concat (tab, ',')</code> can also be used as parameter in the <code>error()</code> function, i.e. <code>error(table.concat(tab, ','))</code>.
 
====Without To see the value of a variable without changing the code ====
To obtain variables and values returned from functions (not local in both cases) the "Debug console" can be used. The "Debug console" appears below in the module page (when it is in edit mode). Then <code>mw.log</code>, <code>mw.logObject</code>, and <code>=</code> can be used. Let's see its usefulness in the next example:
{{#tag:syntaxhighlight|
Line 40:
! Request !! Returned value
|-
| {{colorcode|blue2=lua|mw.log(p.Hello)}}|| {{samp|"Hello"}}
|-
| {{colorcode|blue2=lua|mw.log(p.calc(10/2))}} || {{samp|5}}
|-
| {{colorcode|blue2=lua|mw.log(p.sum_mult(3))}} || {{samp|6&nbsp;&nbsp;&nbsp;9}}
|-
| {{colorcode|blue2=lua|mw.log(p.mtable(4))}} || {{samp|table}}
|-
| {{colorcode|blue2=lua|mw.logObject(p.mtable(4))}} || {{sxhl|2=lua|
table#1 {
:4,
:5,
}
}}
|-
| {{bluecode|2=lua|{{=}}p.sum_mult(10)}} || {{samp|20 100}}
|-
| {{bluecode|2=lua|{{=}}p.Hello .. ', World!'}} || {{samp|Hello, World!}}
|}
 
Line 68 ⟶ 69:
 
== Debugging older modules ==
In some situations, a Lua module might be an older effort, perhaps modified by various other editors along the way, with multiple unknown problems. A Lua module can contain numerous logic errors, and even misspelled variables, if the module was not properly tested, for all features, when initially developed. The message "<span style="color:maroon">{{Script error</span>}}" only occurs when the test data triggers an invalid section of Lua code, so an untested module could contain many hidden bugs, only revealed when broader test data activates more areas of the source code, or uses more internal functions.
 
A common, and complex, problem is to try expanding an older module, for newer features, but unaware how some prior, unsolved bugs will only be triggered when the expanded functionality is added. Note how the errors are, often, not in the newly added Lua script, but rather waiting silently in older sections to be triggered (surprise) when the new code activates other parts of the Lua script. A hidden bug can be as simple as an undefined (or misspelled) variable name, which had been processed with a default value, but when used with newly added features, generates completely mysterious results, as though the new code had errors, when actually, the older misspelled variables, in other areas, completely trashed the operation of the new features.
Line 84 ⟶ 85:
 
=== Small caveat: The case of the wrong case ===
Wikipedia has a small perversion regarding page name casing: it will convert the first character of a page name to uppercase, regardless of the way it was entered. To compensate, it also converts the first letter of a template (when transcluded) and module (when invoked) to uppercase. however, this does not extend to the modules stored in User: namespace viewed through the special [[Template sandbox]], because in the sandbox "the first letter" is not the first letter any more. Here is an actual example: for real modules, let's say [[Module:String]], mediawiki software will allow you to use <nowiki>{{#invoke:mlx|String|replace|Hara I am|a|e}}</nowiki> to return "Here I em" (try it). It will also allow you to use <nowiki>{{#invoke:mlx|string|replace|Hara I am|a|e}}</nowiki> to get the same result:
{|class="wikitable"
|-
! wikitext !! result
|-
|<nowiki>{{#invoke:mlx|String | replace | Hara I am |a|e}}</nowiki>||{{#invoke:String | replace | Hara I am |a|e}}
|-
|<nowiki>{{#invoke:mlx|string | replace | Hara I am |a|e}}</nowiki>||{{#invoke:string | replace | Hara I am |a|e}}
|}
 
Line 107 ⟶ 108:
 
The overall effort appears unstable, or unreliable, as if Lua suddenly "gets too tired" and starts complaining by storing "Script error" into the resultant page, where formatted text would have been expected instead. The timeout problem is somewhat rare, and when a formatted article contains "Script error" text inside the page, then perhaps edit the page slightly, to make a small change, to force the replacement of the page with a clean Lua run.
 
== Exasperating bugs ==
{{Main|Help:Lua for beginners#Exasperating bugs}}
 
== Off-wiki tools ==
 
=== IDE ===
Running your code through an IDE is helpful for its text highlighting features, which can help you quickly spot syntax errors. Any IDE with the ability to install a Lua text highlighting plugin should work. This could be as simple as [[Notepad++]] (with Lua selected from the language menu) or as full-featured as [[Visual Studio Code]].
 
=== Step debugger ===
Step debugging is a powerful tool that allows you to step through code one line at a time, and hover over variables to see their contents at the time of execution. IDEs that easily support Lua step debugging include [[ZeroBrane Studio]], and [[IntelliJ IDEA]] with various debugger plugins ([https://plugins.jetbrains.com/plugin/9768-emmylua Emmy Lua] or [https://github.com/Benjamin-Dobell/IntelliJ-Luanalysis Lunalysis]).
 
=== RegEx ===
{{See also|mw:Extension:Scribunto/Lua reference manual#Patterns}}
Lua uses a unique flavor of [[Regular expression|regular expressions]] that is not supported by top RegEx tools. One tool that can be used for checking Lua RegEx is [https://gitspartv.github.io/lua-patterns/ GitSparTV's Lua Patterns].
 
== Support ==
Line 112 ⟶ 128:
 
== See also ==
* [[Wikipedia:Lua]]
* [[wp:Lua in Wikipedia]] - essay about using [[Lua script]] in Wikipedia
* [[Template:Wikipedia help pages]] - large navbox linking to other topics
 
Line 118 ⟶ 134:
[[Category:Wikipedia how-to]]
[[Category:Wikipedia text help]]
[[Category:WikipediaModule moduleshelp|Debugging]]