Skip to content

fix(query): a ", \ or [[...]] in a title no longer breaks the query; a malformed query gets a query error - #13617

Closed
yuxi-liu-wired wants to merge 4 commits into
logseq:masterfrom
yuxi-liu-wired:fix/query-tag-title-quote
Closed

yuxi-liu-wired wants to merge 4 commits into
logseq:masterfrom
yuxi-liu-wired:fix/query-tag-title-quote

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes logseq/db-test#1374.

A ", \ or [[...]] in a page or tag title

A tag Project, a query {{query (tags [[Project]])}}, then the tag renamed to Project" on its page. The query, never edited, shows "Query error" with (tags [[Project"]]), and the worker logs Unmatched delimiter ].. A title with a \ gives Unsupported escape character, one with "#... gives No reader function for tag, and one with a [[...]] inside, such as Project[[Gremlin Garden]], is cut at the first ]].

pre-transform in deps/db/src/logseq/db/frontend/query_dsl.cljs turns each [[page]] of the query into an EDN string, "[[page]]":

  • It did not escape the title, so a " in the title ended the string early and a \ started an escape. The title is now escaped (\ and ") before it is quoted.
  • It found refs with \[\[(.*?)\]\], which stops at the first ]]. Refs are now found by a scan that counts [[ / ]] pairs, so a nested pair stays inside the title; strings in the query are still copied through untouched, escapes included.
  • A later step, which protects # inside strings, matched strings with "[^"]+"; it now skips escaped quotes.

The test tags-query-with-a-quote-or-backslash-in-the-tag-title in src/test/frontend/db/query_dsl_test.cljs covers this: 4 tags renamed to Project", back\slash, say "#hi" and Project[[Gremlin Garden]], each found by (tags [[...]]). It also checks the pre-transform output for an escaped title, for a nested pair, and for a [[...]] inside a string, which is left as it was. On master it fails with the reader errors above; with the change it passes.

A malformed query gets a query error

A simple query with a malformed form failed deep inside build-query or datascript with a raw error such as alpha is not ISeqable, Insufficient bindings or All clauses in 'or' must use same set of free vars. parse now checks the read form against a malli schema of the query grammar before translating it, and a bad query throws one ex-info built from the malli explanation:

  • Invalid query: alpha is not a query form
  • Invalid query: between needs two dates, or created-at or updated-at and one or two dates

query-operators in frontend.worker.query-dsl is now the one list of operators: each entry holds the operator's argument schema, its error message, its build-* fn, and whether it queries blocks. build-query dispatches through the table instead of a cond branch per operator, and query-schema is built from it. Operators: and, or, not, task/todo, priority, tags, page, property, private-property, between, sample. A string is a [[page]] ref or a full-text search.

Behavior changes, for review:

  • Operators that build-query never translated in DB graphs (page-tags, page-ref, page-property, namespace, sort-by, full-text-search, all-page-tags) returned nothing silently. They are now rejected as "not a query form".
  • Raw datalog clauses ([?b ...], [(pred ...)], ((...))) are not part of the grammar.
  • pre-transform treats an empty "" as a string. Before, a #tag after one was left unexpanded and the reader threw No reader function for tag ....

Tests: invalid-queries runs the reported queries (alpha, TODO, (and todo [[A]]), [(or)], (not [((page-tags #nope))]), (between [[A]] B), (and (and 3.14) "x"), (or ((task)) "x"), (between (page "") (sample (priority #nope "alpha")))) plus (sort-by created-at), and checks each message; pre-transform-test has a case for "" followed by a tag. frontend.db.query-dsl-test: 27 tests, 125 assertions, 0 failures. The namespaces that run DSL queries (query-api, datalog, db-core, render-resource, db-worker, logseq.api.db) pass. clj-kondo and the large-vars lint are clean.

yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 6, 2026
@yuxi-liu-wired yuxi-liu-wired changed the title fix(query): a " or \ in a page or tag title no longer breaks the query fix(query): a ", \ or [[...]] in a page or tag title no longer breaks the query Oct 6, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 6, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 8, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 11, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 13, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Oct 16, 2026
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/query-tag-title-quote branch from 453d158 to 46df96f Compare October 16, 2026 18:34
@yuxi-liu-wired yuxi-liu-wired changed the title fix(query): a ", \ or [[...]] in a page or tag title no longer breaks the query fix(query): a ", \ or [[...]] in a title no longer breaks the query; a malformed query gets a query error Oct 17, 2026
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/query-tag-title-quote branch 5 times, most recently from e331d11 to 461858a Compare October 24, 2026 11:01
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/query-tag-title-quote branch from 461858a to 44876ab Compare October 25, 2026 11:01
pre-transform quoted [[title]] as an EDN string without escaping it, so
a tag renamed to Project" broke every (tags [[...]]) query on it with a
reader error (db-test logseq#1374). The title is now escaped; the string match
that protects # inside strings skips escaped quotes.
The page-ref match \[\[(.*?)\]\] stopped at the first ]], so a tag
renamed to Project[[Gremlin Garden]] broke its queries (db-test logseq#1374).
Refs are now found by a scan that counts [[ / ]] pairs, outside strings.
A malformed simple query failed deep in build-query or datascript with a
raw error (alpha is not ISeqable, Insufficient bindings, ...). parse now
validates the read form against a malli schema of the query grammar and
throws one "Invalid query: ..." error naming the bad form or operator.

Raw datalog clauses are no longer part of the simple query grammar, so
build-query drops its datalog-clause? branch. pre-transform now matches
an empty "" as a string, so a # after one is read as a tag.
…table

query-operators now holds each operator's argument schema, error
message, build fn and whether it queries blocks. build-query dispatches
through it instead of a cond per operator, and query-schema is built
from it, so the operators are listed once. The file-graph operators
that build-query never translated in DB graphs (page-tags, page-ref,
page-property, namespace, sort-by, full-text-search, all-page-tags) are
now rejected instead of silently returning nothing.
@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Combined into #13871 with the other PRs of this theme, to keep the number of open PRs down; the commits are unchanged there.

@yuxi-liu-wired
yuxi-liu-wired deleted the fix/query-tag-title-quote branch November 17, 2026 02:21
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.

Renaming a tag to a title with a double quote breaks every query that filters on it

1 participant