[V3] Can't eval a print with codeblocks #1928
Labels
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Mrcoolgamer11/Red-DiscordBot#1928
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Command bugs
Command name
[p]evalWhat cog is this command from?
dev_commands
What were you expecting to happen?
I'm expecting the following answer in a message:
What actually happened?
I got the following answer in a message:
How can we reproduce this issue?
--devThe problem occurs because the line with the trailing ``` is completely removed from the executed code.
This can be checked by adding statements before the print. If, for example, you add a second print before the existing one, only the first one will be printed and there will be no error.
This is an issue because forgetting a linefeed produces unexpected results. That last line is visible in the codeblock sent to the bot, yet it isn't executed. It also completely crashes the command if there's only one statement.
Note: Seems to be caused by these lines in the cleanup_code function.
Specifically, the fact that it splits lines, removes the first and last, and recombines the lines.
This also causes issues if someone tries to do something like:
And that's a problem too since those two lines are actually displayed in the message, but wouldn't be executed.
The fact that this even displays properly in discord the way you are demonstrating isn't even a guarantee. The code block specification as used by discord is literally
So, this is just a misuse of the code blocks.
Here's a dumb example of why this isn't even worth attempting to detect.
py is a valid highlight syntax. It's also being used as a variable in the code. Without putting abstract syntax parsing into the eval code, there's not a good way to determine which applies, and that is a lot of extra overhead to try and support users misusing code blocks.
First of all, I have no idea where you get that "used by discord literally" from, but if that was the case, I fail to see why Discord still shows the correct syntax when you do:
If you try that right now on Discord, you'll see the string is indeed highlighted and, more importantly, that the line is actually there.
Secondly, the first line's matching seems to work more like:
This can be checked by trying any valid syntax highlight by your definition and just adding a space (or any non-alphanumeric character for that matter; try with
py=hi(displayed) andpy3(not displayed) as your language specifier) after the syntax specifier.This makes me think Discord actually finds code blocks to display the text in a monospace font and adds the little fancy rectangle around it and then looks for a syntax specifier to decide whether it displays the first line or not. It seems to do this by checking whether or not the first line contains non-alphanumerical characters (as demonstrated by
py=hiandpy3which both don't display colors). When it finally finds a syntax specifier, it tries to look up the actual syntax to decide where to put colors.Thirdly, your "used by Discord literally" doesn't quite work since having exactly zero lines between the syntax specifier and the closing backticks actually displays the syntax specifier. This further reinforces the idea that Discord first finds code blocks just by looking for pairs of triple backticks and then tries to match the syntax. When it does find the language, it removes the line and colors the text, but when it doesn't, it keeps displaying code blocks as usual.
Finally, I'd like to say the command should at the very least provide a reasonable error message (and not a cryptic SyntaxError which happens because of how the code is parsed by the command). After that, there's no loss in trying a little harder to guess what the user expects. I'm pretty sure nobody expects the bot to completely wipe the first and last lines of the code block when they can see those lines in the command's message on Discord.
As a solution, I suggest this little bit of not too complicated code to properly handle the code blocks the way Discord seems to display them:
This is actually not enough to solve all the problems I've talked about in this comment, the command should also check whether the cleaned coded is full of whitespace or not and give an appropriate error message if it's indeed only whitespace.
This also doesn't solve the cases where there's nothing else than the syntax specifier in the code block, but that would be picked up by the aforementioned check.
Note: If you actually try what you wrote in the end about
pybeing used as a variable and the syntax specifier, you can noticepyisn't a valid syntax specifier in that case which means it's only used as a variable which makes sense when you look at the displayed code block. This case is properly handled by the suggested code.I'm saying that just because that's the current behavior of the code block, and that matches that behavior currently, doesn't mean we should support it as what has actually been said by discord is the specification for code blocks (link) would break if they ever started using it strictly.
We should not rely on the behavior being inaccurate to the documentation on it, as it could break at any time should discord choose to parse their code blocks as stated.
I do think raising a less cryptic error is worthwhile, but difficult to accomplish in a reasonable way.
In the link you've given, we see an example without a valid syntax specifier (the haiku one), you can also see that the last line is actually displayed. This does not match the current implementation of cleanup_code.
If you're only concerned about having to change it later, then I say, change it to match the current way those things work (or at the very least change it to actually fit what's in that link since it currently doesn't) and I'll come back to fix it whenever they decide to change it. I really don't think a feature as important as this should be left out just by being scared of it changing in the future.
In the link I showed, anything which isn't a valid language highlight isn't parsed. The ambiguity comes in where there is a valid language highlight where it starts. However, as shown already, we have cases where valid ones don't parse "correctly" according to the documentation. It would be saner to require seperate lines, and raise an error to the user if the content starts with and ends with
but that the first line contains anything other than
or
It's also worth noting that eval doesn't require the code blocks, so your use of them with
Seems to be a rather fringe case when
and
would both also be valid. The point of using code blocks is so that you can view the code as it was handled. To that end, I think trying to work around undocumented behavior is a worse solution than enforcing a known good one.
Since we have very little information in that "documentation" (which I wouldn't even refer to as documentation considering the very low amount of technical details), I don't think either of us can actually extrapolate anything from the two screenshots of the code blocks's input.
We can, however, add an error message to actually tell the user what the command expects.
I hope you'll agree, but we can't know whether the "documentation" requires an empty line at the end of syntax code blocks or not since there's only one example. We also know that they did not add this requirement in their implementation of said "documentation". From that, I think it's very safe to include the last line of the code block instead of wiping the line. This, alongside the error message, would be a pretty big step towards something which currently makes sense.
It's also worth noting that eval was an example and the following command is far from a fringe case when one could do:
My point with that example wasn't the example, it was that the last line is removed even though it can contain code. I should've added a line before the print to illustrate my point a bit better in the example. My bad on that.
I do agree. I actually called it ambiguous already, but I don't think the solution is to do anything which makes assumptions about it beyond what we can know. If you have anything more technical covering how it's handled, I'd be happy to concede that, but otherwise I think enforcing the current behavior with better user-facing errors about what is expected is the "correct" solution.
Just removing the trailing triple backticks however does seem to be safe from any discord provided example. The leading ones, and dealing with those as shown does not.
@ZeLarpMaster Tagging you on this as in interested party to a path towards a potential resolution:
(I've constructed this to display properly in the discord webhook, but the actual source code should just use the triple backticks)
This should work for every case other than the very first thing someone putting in a code block being
pyintended as part of the code, which seems like such an edge case that we should put a note in the doc-string and be done with it.EDIT: Well, assuming, you didn't want to syntax highlight with another language for some reason.
To this end, it would likely have a better error on a syntax error which would put the entirety of what was determined to be code as an embedded block like so to go with:
Looks like a very good solution to me. Thanks a lot!
Resolved by #2017.