Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign ActionView::Template::Handlers::ERB.find_offset to handle edge cases #53657

Merged
merged 1 commit into from
Nov 21, 2024

Conversation

martinemde
Copy link
Contributor

@martinemde martinemde commented Nov 17, 2024

Motivation / Background

The code for finding offsets in the ERB templates outright fails in some cases, causing 500s in the error template. In other cases it will bail out early when it's possible to find and highlight correctly.

Detail

This PR fixes many of the issues that regularly occur and correctly highlights more often. Also adds test coverage of the translate_location method on ActionView::Template, many of which failed before this fix.

Additional information

Problems in better-html gem originally prompted me to dig into this, but this solves many rails-only highlight failures or crashes depending on the template. For example, the following template will cause the request to loop forever in current rails:

<%= goodcode %><%= nomethoderror %>

Any template where the compiled code doesn't contain the template code will also loop forever (for example a bug in the template compiler or something changing spacing).

better-html also reliably causes the error page to 500 because it has duplicate copies of the template code in the compiled template. The only way to fix this in better-html would be to fully patch this method to make it correct, which causes its own problems. The solution I suggest here supports better-html without doing anything explicit to do so. It merely supports the case where a string could occur multiple times in the compiled output which happens in vanilla rails, but less often than in better-html

Fixes: Shopify/better-html#121
Probably Fixes: #53388 (I found the same infinite loop and have added test coverage for most of these cases)

CC: @byroot and @jhawthorn because you have both been looking at related issues. FYI @tenderlove, it looks like you wrote the previous implementation.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot bot added the actionview label Nov 17, 2024
@martinemde martinemde force-pushed the martinemde/find_offset-test-and-fix branch 6 times, most recently from 2e6c967 to 2482827 Compare November 17, 2024 21:08
@martinemde martinemde force-pushed the martinemde/find_offset-test-and-fix branch 4 times, most recently from c6e8a35 to 6541168 Compare November 17, 2024 22:26
@martinemde martinemde force-pushed the martinemde/find_offset-test-and-fix branch from 6541168 to 2e6471a Compare November 17, 2024 23:57
@martinemde
Copy link
Contributor Author

martinemde commented Nov 18, 2024

I have this other pending commit to solve a problem brought up in #48184.

Compare (740e220)

Since I had to change the behavior of the tokenizer, I thought we could decide separately from this PR so I don't delay the rest of this fix.

@martinemde martinemde force-pushed the martinemde/find_offset-test-and-fix branch from 2e6471a to 7006970 Compare November 18, 2024 17:31
@martinemde martinemde changed the title Redesign ActionView::Template::Handlers::ERB to handle edge cases Redesign ActionView::Template::Handlers::ERB.find_offset to handle edge cases Nov 18, 2024
@martinemde martinemde force-pushed the martinemde/find_offset-test-and-fix branch from 7006970 to 0b8ea3e Compare November 18, 2024 17:35
@martinemde martinemde force-pushed the martinemde/find_offset-test-and-fix branch from 0b8ea3e to f590e07 Compare November 18, 2024 17:51
@byroot
Copy link
Member

byroot commented Nov 21, 2024

I'm not very familiar with that code, but the change look good and the extra tests are appreciated. Let me know when CI is green and I'll merge + backport.

@martinemde martinemde force-pushed the martinemde/find_offset-test-and-fix branch 2 times, most recently from 21cd9af to a3fb9cc Compare November 21, 2024 18:43
@martinemde
Copy link
Contributor Author

martinemde commented Nov 21, 2024

Build is passing now. Thanks @byroot!

(EDIT: ack, caught a spelling error, rebuilding)

The code for finding offsets in the ERB templates outright fails in some
cases, causing 500s in the error template. In other cases it will bail
out early when it's possible to find and highlight correctly. This PR
fixes many of the issues that regularly occur and correctly highlights
more often. Also adds test coverage of the translate_location method on
ActionView::Template, many of which failed before this fix.
@martinemde martinemde force-pushed the martinemde/find_offset-test-and-fix branch from a3fb9cc to aaebc86 Compare November 21, 2024 19:24
@byroot byroot merged commit 9add4d9 into rails:main Nov 21, 2024
3 checks passed
byroot added a commit that referenced this pull request Nov 21, 2024
…-and-fix

Redesign ActionView::Template::Handlers::ERB.find_offset to handle edge cases
byroot added a commit that referenced this pull request Nov 21, 2024
…-and-fix

Redesign ActionView::Template::Handlers::ERB.find_offset to handle edge cases
@martinemde martinemde deleted the martinemde/find_offset-test-and-fix branch December 14, 2024 23:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants