Skip to content

Fix AST viewer and contextual query caches when refreshing test databases - #4599

Open
josefs wants to merge 1 commit into
mainfrom
josefs/fix-ast-viewer-test-db-refresh
Open

josefs wants to merge 1 commit into
mainfrom
josefs/fix-ast-viewer-test-db-refresh

Conversation

@josefs

@josefs josefs commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #3758

When an imported .testproj test database is reimported after running codeql test run --keep-databases, the old database item is removed and the updated database is copied into the same storage path (producing the same codeql-zip-archive:// source file URIs).

Previously, View CFG updated properly because TemplatePrintCfgProvider only cached the resolved query path and template parameters while re-evaluating PrintCfg.ql on every invocation. In contrast, TemplatePrintAstProvider (as well as TemplateQueryDefinitionProvider and TemplateQueryReferenceProvider) cached evaluated results in CachedOperation keyed by fileUri.toString():

  1. Their caches were never cleared when a database item was removed (DatabaseEventKind.Remove), so View AST continued returning stale AST results after refreshing a test database.
  2. When a file's AST or definitions/references were already cached, provideAst / provideDefinition / provideReferences returned the cached entry without running a query, bypassing DatabaseManager.maybeReimportTestDatabase and never prompting to reimport an outdated test database.

Changes

  • Added CachedOperation.has() to check whether a key is currently cached.
  • Registered a dbm.onDidChangeDatabaseItem listener on TemplatePrintAstProvider, TemplateQueryDefinitionProvider, TemplateQueryReferenceProvider, and TemplatePrintCfgProvider to reset their caches on DatabaseEventKind.Remove.
  • Checked dbm.maybeReimportTestDatabase before serving cached results in TemplatePrintAstProvider, TemplateQueryDefinitionProvider, and TemplateQueryReferenceProvider.
  • Added tests in template-provider.test.ts and cached-operation.test.ts.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@josefs
josefs marked this pull request as ready for review October 14, 2026 12:23
@josefs
josefs requested a review from a team as a code owner October 14, 2026 12:23
Copilot AI balanced review requested due to automatic review settings October 14, 2026 12:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Concurrent cache hits can trigger overlapping destructive database reimports.

1 open finding
What changed in this PR

Fixes stale contextual-query results after test database reimports.

Changes:

  • Invalidates provider caches when databases are removed.
  • Checks for outdated test databases before returning cached results.
  • Adds cache and provider tests.
File Description
template-provider.ts Adds invalidation and reimport checks.
cached-operation.ts Adds cache-key inspection.
template-provider.test.ts Tests provider cache refresh behavior.
cached-operation.test.ts Tests cache lookup and reset behavior.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

);
const db = dbm.findDatabaseItemBySourceArchive(sourceArchiveUri);
if (db) {
await dbm.maybeReimportTestDatabase(db.databaseUri);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The cache is hit so seldomly, I don't imagine this ever being a problem.

This branch has not been deployed

No deployments
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.

View AST not working when refreshing test DB

2 participants