Repository navigation
Report the line of a GSP page an error is on - #17180
jdaugherty wants to merge 6 commits into
Conversation
The scanner and the writer of the GSP compiler counted the lines of a
page wrongly in several cases, so the line that a compile error, a tag
error and an expression error name, and the line that a stack trace of
a page is mapped to, were wrong:
- An error on the last line of a page that does not end with a line
break was reported on line 1, and so was what is only found where a
page ends, such as a tag that is never closed.
- The scanner removes a comment from the text it scans and kept the
places its lines began at as they were before, so everything after a
<%-- --%> or %{-- --}% comment was reported on an earlier line.
- The table of lines of a compiled page had no entry for the line
breaks inside an expression, a scriptlet or a tag attribute, so a
later line was named after one of several lines. It also lost an
entry each time it grew, at 1,000, 2,000 and 4,000 lines of the
generated class.
- An expression that is never closed was reported on the line the text
in front of it starts on.
- A bracket that closes nothing, as in ${a]}, was an
EmptyStackException of the scanner. It is left for the compiler,
which names the line.
The table now counts the line breaks of the generated class as the
Groovy compiler on the class path counts them. Versions of the compiler
differ on a carriage return that no line feed follows, so the compiler
is asked once.
What a page renders is not changed.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An error inside a scriptlet is reported on the line the scriptlet opens, not the line the error is on.
Each line of the generated class that held code of a scriptlet or an
expression of several lines was recorded with the line of the page the
scriptlet or expression opens on, so an error on its fourth line, and
a stack trace through it, named the first.
The writer now counts the line feeds of the code it prints from the
page: each line of the generated class is recorded with the line of the
page its code is on. The line the code starts on is that of its first
character, after the white space the parser trims in front of it.
A %{ }% scriptlet is left as it was: its white space is printed as one
space, so it is one line of the generated class.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An error inside a percent scriptlet is still reported on the line the scriptlet opens.
The code of a %{ }% scriptlet was recorded with the line of the page
the scriptlet opens on, so an error in code that starts on a later
line, after a line break or blank lines, named the line of the %{, and
so did a stack trace through it.
Each line of the generated class that has code of such a scriptlet is
now recorded with the line of the page its code starts on. A line break
of HTML in it, such as <br>, is printed as a line feed, so the code
after it is on a line of its own, recorded with the line that code
starts on.
A line break of the page in the code of a %{ }% scriptlet is still a
space, so its code of several lines is one line of code and an error
in it names the line that code starts on. What a page renders is not
changed.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An error inside a percent scriptlet is still reported on the line the scriptlet opens.
jdaugherty
left a comment
There was a problem hiding this comment.
@jamesfredley, review 6079682627: as of 6cd36d4 the code of a %{ }% scriptlet names the line of the page that its code starts on; details in the thread.
A line break of the page in the code of a %{ }% scriptlet was printed
as one space, so the code of its lines was one line of the generated
class, and an error on its second line, and a stack trace through it,
named the line that code starts on.
Such a line break now ends the line of the generated class with a space
and a backslash. Groovy takes a backslash in front of a line break for
white space in code and leaves both out of a string, so the code means
what it means with the one space, and each line of the page has a line
of its own, recorded with the line its code starts on.
A line break is still one space after code that ends with a backslash,
and after a / or assert on the line, up to the end of that line: a /
can start a comment that a line break would end, and an assertion that
fails quotes its code, which would have the backslash in it. What a
page renders is not changed.
jdaugherty
left a comment
There was a problem hiding this comment.
@jamesfredley, review 6093349821 is answered in its thread: fixed in a5e8529.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An error inside a percent scriptlet is still reported on the line the scriptlet opens.
Only the keyword assert, not a name such as assertValid or reassert, keeps the next line of the page on the line of its code.
jdaugherty
left a comment
There was a problem hiding this comment.
@jamesfredley, review 6094572913 is answered in its thread: fixed in 843532e.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An error inside a percent scriptlet is still reported on the line the scriptlet opens.
| while (ix >= 0) { | ||
| int end = ix + "assert".length(); | ||
| boolean wordStart = ix == 0 || !Character.isJavaIdentifierPart(code.charAt(ix - 1)); | ||
| boolean wordEnd = end == code.length() || !Character.isJavaIdentifierPart(code.charAt(end)); |
There was a problem hiding this comment.
A percent scriptlet opens on line 2 and names a missing class on line 3. The error should name line 3. It names line 2, because a quoted name that contains assert is treated as an assertion. Name line 3.
There was a problem hiding this comment.
It names line 2, because a quoted name that contains assert is treated as an assertion. Name line 3.
This one stays as it is. The check that keeps the code of a %{ }% scriptlet on one line matches the word assert anywhere in the code of that line: as the keyword, in a string such as "assert", or in a quoted name such as m.'assert'. To tell them apart, the GSP compiler would have to tell strings, comments and names apart, which means parsing Groovy. A / in a string, such as a URL, has the same effect.
None of these cases is worse than before. On 7.0.x the page in this thread names line 2 too: see the table in matrei's review, which has this case and def s = "assert";. For such a line the PR keeps the line that 7.0.x names, where it could have named the later one. What matters is the other direction: a line break that the check lets through has to mean what the one space meant, and a check that errs towards keeping the code on one line keeps that true. What such a page writes is the same either way.
As of eea39c6, GspLineNumberSpec pins both cases. The rows "on the line after a string that is the word assert, which is the line of the string" and "on the line after a quoted name that is the word assert, which is the line of the name" name line 2, and two rows of the #what feature check what those pages write. The upgrade note, the upgrade skill and the Javadoc of the check say "the word assert, also in a string or a quoted name".
matrei
left a comment
There was a problem hiding this comment.
@jdaugherty, thanks for splitting this out. The fix is right, the %{ }% commits included. I'd stop where it is now: James's last point can't be fixed without parsing Groovy, and it only keeps the line 7.0.x names. Two small points about the documentation, and notes for the merge-up.
What I ran
On 7.0.x, at 843532e and on the base, c1ce8c3:
:grails-gsp-core:test(176) and:grails-gsp:test(560, 9 skipped) pass with the PR.GspLineNumberSpecfails 58 of its 132 cases on the base. Its 30 cases of what a page writes all pass on the base too, so%{ }%pages write what they did before.GSPWriterSpecneeds the new constructor, so it doesn't run there.- A probe of 15 more
%{ }%pages renders the same on both: power assertions over two lines, after a continued line and in a closure (the same messages), a call whose argument is on the next line,<over two lines,<br>, a URL in a string, a map, a ternary, an elvis, safe navigation and a method chain over lines, a tab before a line break in a string, a GString with$nbefore a line break, and an escaped backslash before one. - A probe of other pages:
| Page | 7.0.x | With the PR |
|---|---|---|
A <% %> scriptlet that opens on line 2 and doesn't compile on line 4 |
2 | 4 |
| The same scriptlet failing on line 4 when it runs | 2 | 4 |
| The same, inside a closure | 5 | 4 |
A %{ }% scriptlet that opens on line 2 and doesn't compile on line 3 |
2 | 3 |
The same after m.'assert' = 1; or def s = "assert"; on line 2 |
2 | 2 |
1. James's last point
James's last point, at GroovyPageParser.java:1454, is a quoted name with assert in it. That's a case where the guard keeps the lines of a %{ }% scriptlet together although it needn't. A string such as "assert" does the same, and so does a / in a URL. Each of those only keeps the line that 7.0.x names, as the table shows. The page writes the same either way.
Telling all of them apart means telling strings, comments and names apart, which is parsing Groovy in the GSP compiler. That's too much for a patch release, and for an error line in a syntax the guide doesn't document. What matters is the other direction: a line break the guard lets through must mean what the one space meant. I found no case where it doesn't. So I'd answer James that this one stays, and say so in the note.
The note says "the keyword assert … (a name that contains assert, such as assertValid, is not the keyword)". As the guard also matches assert in quotes, I'd write "the word assert" instead. Then the note covers James's case.
2. The upgrade skill
The note asks applications to change their tests: "A test that asserts the line of a GSP error asserts the line of the page instead." The skill's README says that "a pull request that changes what an application has to do, or what a call returns, updates the skill in the same pull request". So the same entry belongs under # Grails 7.0.18 in grails-skills/upgrade/skills/grails-upgrade/SKILL.md.
3. The branch
7.0.x is right. Under #17061 this is a bug fix like any other: a page that fails still fails, and only the line it names changes. That includes ${a]}, which now fails with a compile error instead of an EmptyStackException. A page that works writes what it wrote, which the 30 cases above check for %{ }%. So one approval is enough.
4. For the merge-up into 8.0.x
- I cherry-picked the five commits onto #17181's head. Besides the note, there are two conflicts:
- In
printlnToResponse, 8.0.x checks!GrailsStringUtils.hasLength(s)where 7.0.x hasisEmpty(s). That check moves into the new three-argument method. - In
GroovyPageParser,continuesOnNextLine()andcontainsAssertKeyword()land where 8.0.x haschecksumOf()andgetSourceChecksum(). Keep both sides.
- In
- With those,
:grails-gsp-core:test(472) and:grails-gsp:test(749, 7 skipped) pass, together with #17181's specs. - 8.0.0 has these bugs too, so 8.0.x needs the note under Grails 8.0.1 in
upgrading80x.adoc, and in the skill. "More GSP tags HTML-encode the attribute values they write" is in both guides the same way.
… note to the upgrade skill
The guard that keeps the code of a %{ }% scriptlet on one line also
matches assert in a string or a quoted name, as in m.'assert'. An error
after it names the line it names on 7.0.x. The upgrade note and the
Javadoc now say so, and GspLineNumberSpec pins both cases and what such
pages write.
The upgrade skill gets the note under Grails 7.0.18.
jdaugherty
left a comment
There was a problem hiding this comment.
@matrei, thanks for the review. Done in eea39c6:
- The upgrade note says "the word
assert", also in a string or a quoted name such asm.'assert', and so does the Javadoc of the check.GspLineNumberSpechas rows for both cases (each names line 2, the line 7.0.x names) and for what such pages write. I answered James in his thread: this case stays as it is. - The note is in the upgrade skill too, as "GSP error lines" under
# Grails 7.0.18, after "GSP tag attributes". - Thanks for the merge-up notes. When this goes up to 8.0.x I'll resolve the two conflicts as you describe and add the note under Grails 8.0.1 in
upgrading80x.adocand in the skill.
@jamesfredley, your last point is answered in its thread.
| while (ix >= 0) { | ||
| int end = ix + "assert".length(); | ||
| boolean wordStart = ix == 0 || !Character.isJavaIdentifierPart(code.charAt(ix - 1)); | ||
| boolean wordEnd = end == code.length() || !Character.isJavaIdentifierPart(code.charAt(end)); |
There was a problem hiding this comment.
It names line 2, because a quoted name that contains assert is treated as an assertion. Name line 3.
This one stays as it is. The check that keeps the code of a %{ }% scriptlet on one line matches the word assert anywhere in the code of that line: as the keyword, in a string such as "assert", or in a quoted name such as m.'assert'. To tell them apart, the GSP compiler would have to tell strings, comments and names apart, which means parsing Groovy. A / in a string, such as a URL, has the same effect.
None of these cases is worse than before. On 7.0.x the page in this thread names line 2 too: see the table in matrei's review, which has this case and def s = "assert";. For such a line the PR keeps the line that 7.0.x names, where it could have named the later one. What matters is the other direction: a line break that the check lets through has to mean what the one space meant, and a check that errs towards keeping the code on one line keeps that true. What such a page writes is the same either way.
As of eea39c6, GspLineNumberSpec pins both cases. The rows "on the line after a string that is the word assert, which is the line of the string" and "on the line after a quoted name that is the word assert, which is the line of the name" name line 2, and two rows of the #what feature check what those pages write. The upgrade note, the upgrade skill and the Javadoc of the check say "the word assert, also in a string or a quoted name".
✅ All tests passed ✅🏷️ Commit: eea39c6 Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A scriptlet written as %{ }% is still rendered as text when the closing mark is inside a comment.
| } | ||
| } | ||
| text = text.substring(0, --end1) + text.substring(ix + 4); | ||
| int removedFrom = --end1; |
There was a problem hiding this comment.
A scriptlet is %{ // example: }%
out.print(42) }%. The page should print 42. It prints the rest as text, because the mark inside the comment is read as the end. Read the scriptlet to its real end.
Split out of #17114, as the review there asked: these bugs are in the GSP compiler that every application uses, and are on 7.0.x too.
The line that a GSP error names was wrong for many pages:
<%-- --%>or%{-- --}%comment was reported on an earlier line;%{ }%scriptlet, whose line breaks end no statement, the code after a/or the wordassert, also in a string or a quoted name, or after a trailing backslash, still shares the line of that code);${was reported on the line the text in front of it starts on;${a]}was anEmptyStackExceptioninstead of a compile error.This is the line in
GroovyPagesExceptionandGrailsTagException(the dev error page and the logs), and the line a runtime error in a view is mapped to byGroovyPageView. What a page renders is not changed.GroovyEachParseTestsnow expects line 2 for a tag that is on line 2 of its page.The upgrade note is under Grails 7.0.18 in
upgrading60x.adocand in the upgrade skill (grails-skills/upgrade/skills/grails-upgrade/SKILL.md).The escape fix of #17114 (
\${at the 64,000-character boundary) is not here: that bug came with #16144 and is only on 8.0.x, so it has its own PR there.Tested locally:
:grails-gsp-core:test(with the newGspLineNumberSpecandGSPWriterSpec), the compiler and page tests of:grails-gsp, andcodeStyle.