Escape control characters in the error message (#879)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 54b5ea7 · Browse files · 3 files·+14 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+14 added−7 removed
- tests:+1 added−1 removed
- other:+13 added−6 removed
Provenance
- Commit
de32969- Tree
7ea30ec- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
54b5ea7
3 files changed+14 added−7 removed
modified:lib/arguments/escape.js+7 added-1 removed
1234567891010111213141516171819202147 unmodified linesimport {platform} from 'node:process';import {stripVTControlCharacters} from 'node:util';
export const joinCommand = (filePath, rawArgs) => { const fileAndArgs = [filePath, ...rawArgs]; const command = fileAndArgs.join(' '); const escapedCommand = fileAndArgs.map(arg => quoteString(escapeControlCharacters(arg))).join(' '); return {command, escapedCommand};};
const escapeControlCharacters = arg => arg.replaceAll(SPECIAL_CHAR_REGEXP, character => escapeControlCharacter(character));export const escapeLines = lines => stripVTControlCharacters(lines) .split('\n') .map(line => escapeControlCharacters(line)) .join('\n');
const escapeControlCharacters = line => line.replaceAll(SPECIAL_CHAR_REGEXP, character => escapeControlCharacter(character));
const escapeControlCharacter = character => { const commonEscape = COMMON_ESCAPES[character]; if (commonEscape !== undefined) { return commonEscape;47 unmodified linesmodified:lib/return/error.js+6 added-5 removed
1 import {signalsByName} from 'human-signals';2 import stripFinalNewline from 'strip-final-newline';3 import {isBinary, binaryToString} from '../utils.js';4 import {fixCwdError} from '../arguments/cwd.js';5+import {escapeLines} from '../arguments/escape.js';6 import {getFinalError, isPreviousError} from './clone.js';7 8 export const makeSuccessResult = ({9 command,53 unmodified lines63 error: initialError,64 signal,65 signalDescription,66 exitCode,66- command,67+ escapedCommand,68 timedOut,69 isCanceled,70 timeout,71 cwd,50 unmodified lines122 error,123 signal,124 signalDescription,125 exitCode,125- command,126+ escapedCommand,127 timedOut,128 isCanceled,129 timeout,130 cwd,1 unmodified line132 const errorCode = error?.code;133 const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled});134 const originalMessage = getOriginalMessage(error, cwd);135 const newline = originalMessage === '' ? '' : '\n';135- const shortMessage = `${prefix}: ${command}${newline}${originalMessage}`;136+ const shortMessage = `${prefix}: ${escapedCommand}${newline}${originalMessage}`;137 const messageStdio = all === undefined ? [stdio[2], stdio[1]] : [all];138 const message = [shortMessage, ...messageStdio, ...stdio.slice(3)]138- .map(messagePart => stripFinalNewline(serializeMessagePart(messagePart)))139+ .map(messagePart => escapeLines(stripFinalNewline(serializeMessagePart(messagePart))))140 .filter(Boolean)141 .join('\n\n');142 return {originalMessage, shortMessage, message};143 };27 unmodified lines171 return '';172 }173 174 const originalMessage = isPreviousError(error) ? error.originalMessage : String(error?.message ?? error);174- return fixCwdError(originalMessage, cwd);175+ return escapeLines(fixCwdError(originalMessage, cwd));176 };177 178 const serializeMessagePart = messagePart => Array.isArray(messagePart)179 ? messagePart.map(messageItem => serializeMessageItem(messageItem)).join('')13 unmodified linesmodified:test/return/error.js+1 added-1 removed
92 unmodified lines93 test('error.shortMessage does not contain stdout/stderr/stdio', testFullIgnoreMessage, fullStdio, 'shortMessage');94 95 const testErrorMessageConsistent = async (t, stdout) => {96 const {message} = await t.throwsAsync(execa('noop-both-fail.js', [stdout, 'stderr']));97- t.true(message.endsWith(`noop-both-fail.js ${stdout} stderr\n\nstderr\n\nstdout`));97+ t.true(message.endsWith(' stderr\n\nstderr\n\nstdout'));98 };99 100 test('error.message newlines are consistent - no newline', testErrorMessageConsistent, 'stdout');101 test('error.message newlines are consistent - newline', testErrorMessageConsistent, 'stdout\n');85 unmodified lines