D1 export: `quote()` in d1.js turns a literal `\n` into a newline when the value also has a real newline

オープン 初心者向け
#8,678 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@Jeremiah-D がすでに取り組んでいます。

2026年11月20日 から。

  • #8699 @Jeremiah-D による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
85/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript
領域
database, testing

調査の方向性

src/workerd/server/d1/d1.js を読み、quote() をたどって、改行と復帰のエスケープ方法がどのように選ばれるかを確認してください。shell.c.in と Miniflare の dumpSql.ts について説明されている衝突処理を比較し、その後 d1-export-test.js を調べてください。実際の改行とリテラルの \n の両方を含むエクスポートケース(および対応する復帰のケース)を追加し、インポート時にエクスポートが両方の値を保持することを確認してください。

索引モデルが issue の本文から書いたものです。

説明

The new built-in D1 (src/workerd/server/d1/d1.js, from #8486) has the same export escaping bug that was reported against D1 in cloudflare/workers-sdk#9042 (internal ticket CFSQL-1294). Remote D1 has since fixed it, and cloudflare/workers-sdk#15334 fixes it in Miniflare's dumpSql.ts. But cloudflare/workers-sdk#16955 replaces dumpSql.ts with this code, so once that lands wrangler d1 export --local will have the bug again.

The problem is in quote(), which uses the same logic as the old dumpSql.ts (where this reproduces on wrangler 4.154.0):

let out = `'${text.replace(/'|(\n)|(\r)/g, (_, n, r) => {
  if (n) return ((lf = true), '\\n');
  if (r) return ((cr = true), '\\r');
  return "''";
})}'`;
if (cr) out = `replace(${out},'\\r',char(13))`;
if (lf) out = `replace(${out},'\\n',char(10))`;

Each real newline becomes the two characters \n, but a \n already in the value is left as-is. The replace() wrapper can't tell them apart, so on import both turn into newlines.

Example: the value 'a' || char(10) || 'b\nc' (hex 610A625C6E63) exports as

replace('a\nb\nc','\n',char(10))

which reads back as 610A620A63. The backslash-n is gone. \r has the same problem.

The comment says this follows shell.c.in's output_quoted_escaped_string, but the shell version first checks whether the value already contains the escape and picks one it doesn't (\n, then \012, then (\n0), (\n1), ...). Remote D1 export does the same thing now, e.g. the value above exports from remote as:

replace('a\012b\nc','\012',char(10))

So the fix is probably to port that collision check, like cloudflare/workers-sdk#15334 does for Miniflare. The export test in d1-export-test.js only has newlines with no literal backslash-n, which is why it doesn't catch this. A row with both would.

主要言語
C++
スター
9.3k
フォーク
845
平均マージ
1日 23時間
マージ済み PR(30日)
558

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudflare/workerd のほかの issue

cloudflare/workerd の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。