Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/import-escape-terminal-controls.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'nostream': patch
---

fix(cli): show control characters in `nostream import` error messages as escapes

When a line of an import file could not be parsed, `nostream import` printed the parser's message as it is, and that message quotes part of the file. A file with a terminal escape sequence in a malformed record could therefore change the operator's display or, where the terminal allows it, replace the clipboard. The per-line errors, the failure message and an error that quotes the input path (for example a file that does not exist) now show control characters as visible escapes (for example `\x1b`), so the context stays readable and nothing is sent to the terminal.
13 changes: 12 additions & 1 deletion src/cli/commands/import.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { runImportEvents } from '../../import-events'
import { escapeForTerminal } from '../utils/output'

type ImportOptions = {
file?: string
Expand All @@ -16,5 +17,15 @@ export const runImport = async (options: ImportOptions, rawArgs: string[]): Prom
args.push('--batch-size', String(options.batchSize))
}

return runImportEvents([...args, ...rawArgs])
try {
return await runImportEvents([...args, ...rawArgs])
} catch (error) {
// The message can quote the input path or the input file; the CLI error
// boundary prints it as it is.
if (error instanceof Error) {
error.message = escapeForTerminal(error.message)
}

throw error
}
}
5 changes: 3 additions & 2 deletions src/import-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
EventImportStats,
} from './services/event-import-service'
import { createEventRepository } from './factories/event-repository-factory'
import { escapeForTerminal } from './cli/utils/output'
import { getMasterDbClient } from './database/client'

interface CliOptions {
Expand Down Expand Up @@ -226,7 +227,7 @@ export const runImportEvents = async (

const onLineError = ({ lineNumber, reason }: EventImportLineError) => {
if (loggedErrors < MAX_ERROR_LOGS) {
console.warn(`[line ${lineNumber}] ${reason}`)
console.warn(`[line ${lineNumber}] ${escapeForTerminal(reason)}`)
loggedErrors += 1
return
}
Expand Down Expand Up @@ -294,7 +295,7 @@ if (require.main === module) {
})
.catch((error: unknown) => {
if (error instanceof Error) {
console.error(`Import failed: ${error.message}`)
console.error(`Import failed: ${escapeForTerminal(error.message)}`)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
} else {
console.error('Import failed with unknown error')
}
Expand Down
19 changes: 19 additions & 0 deletions test/unit/cli/import-command.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from 'chai'

import { runImport } from '../../../src/cli/commands/import'

describe('runImport', () => {
it('shows control characters of a rejected import as visible escapes', async () => {
const file = 'missing-\u001b]52;c;ZXZpbA\u0007.jsonl'

try {
await runImport({ file }, [])
expect.fail('Expected the import to reject')
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
expect(message).to.include('Input file does not exist')
expect(message).to.include('missing-\\x1b]52;c;ZXZpbA\\x07.jsonl')
expect(message).to.not.match(/[\u0000-\u001f\u007f-\u009f]/)
}
})
})
25 changes: 24 additions & 1 deletion test/unit/cli/import.runtime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,31 @@ describe('import runtime routing', () => {
const code = await runImportEvents(['--help'])

expect(code).to.equal(0)
const output = logStub.getCalls().map((call) => call.args.join(' ')).join('\n')
const output = logStub
.getCalls()
.map((call) => call.args.join(' '))
.join('\n')
expect(output).to.include('file.jsonl|file.json')
expect(output).to.include('nostream import ./events.json --batch-size 1000')
})

it('prints a line error with terminal control characters as visible escapes', async () => {
const filePath = makeTempFile('events.jsonl', '{"x":1}\n')
const warnStub = sinon.stub(console, 'warn')

sinon.stub(dbClient, 'getMasterDbClient').returns(stubDb())
sinon.stub(EventImportService.prototype, 'importFromReadable').callsFake(async (_stream, options) => {
options?.onLineError?.({ lineNumber: 7, reason: 'Unexpected token "\u001b]52;c;ZXZpbA\u0007"' })
return emptyStats()
})

await runImportEvents([filePath])

const output = warnStub
.getCalls()
.map((call) => call.args.join(' '))
.join('\n')
expect(output).to.include('[line 7] Unexpected token "\\x1b]52;c;ZXZpbA\\x07"')
expect(output).to.not.match(/[\u0000-\u001f\u007f-\u009f]/)
})
})
Loading