Page MenuHomePhabricator

Improve "Operation on an invalid type" error to attribute source
Closed, ResolvedPublic

Description

When Less code contains a mistake, most errors produce a message that includes an attribution to which Less input file and line number the mistake happened on.

When an operation like plus, minus, or multiply is done in Less code, and one of the values is not a number ("dimension") or color, we throw error "Operation on an invalid type", but this lacks attribution to help developers find the problem.

Example

@foo: 'hello';
@bar: 'world';
div {
 content: @foo * @bar;
}
PHP Fatal error:  Uncaught Less_Exception_Compiler: Operation on an invalid type in /less.php/lib/Less/Tree/Operation.php:42
Stack trace:
#0 /less.php/lib/Less/Tree/Expression.php(45): Less_Tree_Operation->compile(Object(Less_Environment))
#1 /less.php/lib/Less/Tree/Value.php(24): Less_Tree_Expression->compile(Object(Less_Environment))
#2 /less.php/lib/Less/Tree/Rule.php(82): Less_Tree_Value->compile(Object(Less_Environment))

Compare that to:

div {
  content: @bar;
}
PHP Fatal error:  Uncaught Less_Exception_Compiler: variable @bar is undefined in file test.less on line 2, column 12
1| div {
2|   content: @bar;
3| }
4|  in /less.php/lib/Less/Tree/Variable.php:52
Stack trace:
#0 /less.php/lib/Less/Tree/Expression.php(45): Less_Tree_Variable->compile(Object(Less_Environment))
#1 /less.php/lib/Less/Tree/Value.php(24): Less_Tree_Expression->compile(Object(Less_Environment))
#2 /less.php/lib/Less/Tree/Rule.php(82): Less_Tree_Value->compile(Object(Less_Environment))

Imported from https://github.com/wikimedia/less.php/issues/84:

This library needs to provide a little more feedback on this fatal compilation error: "Operation on an invalid type." I'm thinking what .less file and which line.

The .less files I have, they compile nicely with less-plugin-clean-css for Node.js. But give this error with less.php.

To determine why it does not compile with less.php we need at least a hint.

Reference: Codesearch

Event Timeline

Restricted Application added a subscriber: Aklapper. · View Herald Transcript

My understanding is as follows:

  • The exception class in Less.php supports a parameter for the user input file name and position. This is included in the exception output if it is set. We already pass these parameters if we find user mistakes during the parse phase.
  • The above error happens during compilation, not parsing. It's a bit harder to know the current position in that case. The way we usually handle that is by catching the exception, and letting Less_Parser adding the properties retroactively.

I suspect the latter is either not happening in this case for some reason, or it does happen, but doesn't have the intended effect.

Krinkle triaged this task as Medium priority.Sep 11 2023, 1:23 PM

Change 976671 had a related patch set uploaded (by Hokwelum; author: Hokwelum):

[mediawiki/libs/less.php@master] Improve "Operation on an invalid type" error to attribute source

https://gerrit.wikimedia.org/r/976671

Change 976671 merged by jenkins-bot:

[mediawiki/libs/less.php@master] Less_Tree_Rule: Add source to "Operation on an invalid type" error

https://gerrit.wikimedia.org/r/976671