Skip to content

Add a workaround to force VS Code to trigger onDidCloseTextDocument#5886

Merged
DanTup merged 4 commits intomasterfrom
force-close-hack
Jan 20, 2026
Merged

Add a workaround to force VS Code to trigger onDidCloseTextDocument#5886
DanTup merged 4 commits intomasterfrom
force-close-hack

Conversation

@DanTup
Copy link
Member

@DanTup DanTup commented Jan 20, 2026

We have code that handles closing and opening of text documents, but VS Code only actually closes them after 3 mins. Rather than have tests waiting for 3 mins or risking behaving differently to when files are really closed, this spawns dummy files that forces VS Code to clean up in places where we want to be sure that event fires.

We have code that handles closing and opening of text documents, but VS Code only actually closes them after 3 mins. Rather than have tests waiting for 3 mins or risking behaving differently to when files are really closed, this spawns dummy files that forces VS Code to clean up in places where we want to be sure that event fires.
@DanTup DanTup added this to the v3.128.0 milestone Jan 20, 2026
@DanTup DanTup added the is testing Relates to the testing (either manual or automated) of Dart Code label Jan 20, 2026
@DanTup DanTup requested a review from Copilot January 20, 2026 17:08
@DanTup
Copy link
Member Author

DanTup commented Jan 20, 2026

@codex review

/gemini review

@gemini-code-assist
Copy link

Hello DanTup,

I've received your request for a code review via /gemini review. I am now performing the review and will post my findings as a separate review comment shortly.

Please note that @codex review is not a recognized command for gemini-code-assist. If you intended to tag me for a comment response, please use @gemini-code-assist or an alias like Gemini. You can find more information on available commands and how to invoke me by typing /gemini help.

Thank you!

@codecov
Copy link

codecov bot commented Jan 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.37%. Comparing base (e044b09) to head (53b52a5).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5886   +/-   ##
=======================================
  Coverage   67.37%   67.37%           
=======================================
  Files         168      168           
  Lines       12899    12899           
  Branches     2556     2556           
=======================================
  Hits         8691     8691           
+ Misses       3757     3754    -3     
- Partials      451      454    +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a workaround to force VS Code to trigger onDidCloseTextDocument events immediately during testing, rather than waiting up to 3 minutes for VS Code's natural cleanup process. The solution involves opening dummy untitled documents to fill VS Code's text model buffer, forcing it to clean up previously closed documents.

Changes:

  • Implemented forceDocumentCloseEvents() function that opens up to 100 dummy files to trigger document cleanup
  • Refactored closeFile() to use the new workaround and ensure close events fire immediately
  • Removed waitUntilAllTextDocumentsAreClosed() and related slow test configurations
  • Added comprehensive test coverage for the new functionality

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/test/helpers.ts Adds forceDocumentCloseEvents() and closeTabs() functions, refactors closeFile(), removes waitUntilAllTextDocumentsAreClosed()
src/test/dart/utils/helpers.test.ts New test file verifying that closeFile() properly triggers close events through 10 iterations
src/test/dart/refactors/renames.test.ts Updates to use new workaround, removes 3-minute timeout and slow test skip logic
src/test/dart/file_tracking.test.ts Updates subscription tests to use new workaround, removes slow test configuration

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a0e8d9540

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@DanTup DanTup requested a review from Copilot January 20, 2026 17:59
@DanTup
Copy link
Member Author

DanTup commented Jan 20, 2026

@codex review

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a clever workaround to force VS Code to fire onDidCloseTextDocument events during tests, which should significantly improve test reliability and speed by removing the need for long waits. The new forceDocumentCloseEvents helper is a great addition. I've provided a couple of suggestions for src/test/helpers.ts to enhance the maintainability of the new code by addressing a magic number and clarifying some logic. Overall, this is a valuable improvement for the test suite.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 53b52a5e2c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@DanTup DanTup merged commit b40eed6 into master Jan 20, 2026
17 checks passed
@DanTup DanTup deleted the force-close-hack branch January 20, 2026 18:44
@RedCMD
Copy link

RedCMD commented Jan 21, 2026

documents are forceable closed and reopened when the language is changed
https://code.visualstudio.com/api/references/vscode-api#workspace.onDidCloseTextDocument

tho that prob doesn't work well enough for you

@DanTup
Copy link
Member Author

DanTup commented Jan 21, 2026

@RedCMD thanks! I did actually see that yesterday, but also wasn't sure it would help here because some tests are trying to check things for closed files, and I'm trying really to mimic as close to the real behaviour as possible.

Honestly I don't understand why we wan't change the 3min timeout or have some command to force this (particularly for testing). I did also consider (and started implementing) just simulating the close/open events - however it got really messy (for example VS Code is still sending Change events for files modified on disk when the file is closed-but-not-really-closed-yet, so they would need suppressing too).

Unfortuantely all the options are bad, but this one seemd like the least bad (it's isolated to the internals of a single function) and in my testing was also reliable. Unfortunately it's not fast though, opening and closing 60 documents takes quite a few seconds 😞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

is testing Relates to the testing (either manual or automated) of Dart Code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants