You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
D1 export: quote() in d1.js turns a literal \n into a newline when the value also has a real newline #8678
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):
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.
Activity