AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Pg-Meta Pg-Format: ArrayToList UseSpace Flag Is Dead, Causing Stray Leading Space For Nested Arrays

The useSpace flag in arrayToList is ignored because both ternary branches are identical, resulting in an extra leading space when formatting nested arrays that appear as the first element. This diverges from upstream pg-format and can affect exact string comparisons in SQL generation.

mediumConfidence 95%Pg-Meta

Origin Analysis

In arrayToList, the ternary condition `useSpace ? safeSql`${sql} (` : safeSql`${sql} (` has identical branches, so the flag has no effect and a space is always added before the opening parenthesis.
1. Import literal and string from pg-meta pg-format. 2. Call string([[1, 2], [3, 4]]); observe output " (1, 2), (3, 4)" with a leading space. 3. Call literal([['a', 'b']]); observe output " ('a', 'b')" with a leading space. 4. Compare with upstream pg-format which produces no leading space.

Fixing Code Block

function arrayToList(useSpace: boolean, array: any[], formatter: (value: any) => string): string { let sql = ''; array.forEach((element, index) => { sql = useSpace ? safeSql`${sql} (` : safeSql`${sql}(`; sql += formatter(element); sql += ')'; }); return sql; }
The false branch of the ternary is corrected to omit the space before the opening parenthesis, so when useSpace is false, no space is inserted. This makes the flag functional and aligns output with upstream pg-format.

Edge Case Audit

The change is safe for SQL validity (leading whitespace is harmless to Postgres) but may break tests or downstream code that inadvertently rely on the stray space. Rolling back is trivial by reverting the single character change in the false branch. No concurrency or cross-platform issues are anticipated, but ensure all callers are updated to expect the corrected formatting.

Ecosystem Topology