Skip to content

Fix java.lang.RuntimeException: Document is locked by write PSI operations errors #960

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

Closed
wants to merge 3 commits into from

Conversation

facboy
Copy link
Contributor

@facboy facboy commented Aug 27, 2023

Also update to google-java-format 1.17.0

@plumpy plumpy self-requested a review August 28, 2023 15:39
Copy link
Collaborator

@plumpy plumpy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is great, thank you so much for fixing this!

Copy link
Member

@cpovirk cpovirk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got asked if I had the power to un-block this. I'm not actually sure if I do, but one question before I try:

We read the document text at line 62. If we later find that the document is blocked, we perform and postponed operations, and then we write output that was based on the document text we read earlier. Does that mean that we could undo the effects of any of the operations that we'd just postponed? If so, should we be reading the document text again (maybe retrying the whole of processFile) and operating on that? If that's difficult, would it be better to simply do nothing? I see some other formatter implementations that choose to do nothing, e.g., this code in ktfmt.

copybara-service bot pushed a commit that referenced this pull request Aug 28, 2023
…ations` errors

Also update to `google-java-format` 1.17.0

Fixes #960

FUTURE_COPYBARA_INTEGRATE_REVIEW=#960 from facboy:master 10cef1b
PiperOrigin-RevId: 560727815
@facboy
Copy link
Contributor Author

facboy commented Aug 29, 2023

Yeah, that's a good spot. I've changed it to unblock and apply if the document text hasn't changed (not sure how likely that is), otherwise do nothing. Intellij's own ImportOptimizers do all their work in the returned Runnable (i.e. on the Swing thread, i assume), but I'm not sure how much more heavyweight google-java-format is compared to that, so I guess we'll just give up and wait for the next run if there is a mismatch.

Copy link
Member

@cpovirk cpovirk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Let's see if I can in fact get this converted into a change in our internal repo, and then we'll see if anyone with permissions is up for getting it submitted.

copybara-service bot pushed a commit that referenced this pull request Aug 29, 2023
…ations` errors

Also update to `google-java-format` 1.17.0

Fixes #960

FUTURE_COPYBARA_INTEGRATE_REVIEW=#960 from facboy:master 89f7902
PiperOrigin-RevId: 560727815
@facboy
Copy link
Contributor Author

facboy commented Sep 1, 2023

i changed the import optimizer to not set document.text if

  1. it hasn't actually formatted anything
  2. it detects that document.text has changed since it ran its formatting

@cpovirk
Copy link
Member

cpovirk commented Sep 5, 2023

i changed the import optimizer to not set document.text if

  1. it hasn't actually formatted anything
  2. it detects that document.text has changed since it ran its formatting

I see this warning that getText() may be expensive.

Now, presumably it doesn't add a lot to the cost of setting the text when we're modifying the imports. And even if there is nothing to set (and so we're paying only the cost of getting the text), you make a good point that IntelliJ's own import optimizer does what looks like lots of stuff (implementation?) on the main thread, so How Bad Can It Be?

Still, do you think we could get most of the value by including only the first of your two new checks, the one that happens in whatever background thread does the work, since that one uses the result of the getText() call that we already had to make?

@facboy
Copy link
Contributor Author

facboy commented Sep 5, 2023

it seems to suggest using getCharsSequence() instead, i can take a look at whether it's possible instead of getText(). as far as the checks go

  • the first one i see as 'less' important, it's really an optimization.
  • the second is more important, i think it's the reason the formatter sometimes seems to not work - the formatter correctly formats the text and it gets set on the document, then the import optimizer comes along, having built its text in the background, and overwrites the text that the formatter has set on the document..

…f formatter results are unchanged, or if `document.text` has changed

This seemed to be responsible for some of the issues with the formatter seemingly not formatting a file.
@cpovirk
Copy link
Member

cpovirk commented Sep 5, 2023

Oh, right, sorry, I'd somehow been thinking that the second check was a new thing and not the fix you'd previously explained.

getCharSequence looks like it makes a nice, easy optimization in any case.

copybara-service bot pushed a commit that referenced this pull request Sep 6, 2023
…ations` errors

Also update to `google-java-format` 1.17.0

Fixes #960

FUTURE_COPYBARA_INTEGRATE_REVIEW=#960 from facboy:master e0925a2
PiperOrigin-RevId: 562932625
@copybara-service copybara-service bot closed this in 28b199c Sep 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants