From ceb7cbdbcf1a48ea19b3ab3c368766cf8d04e643 Mon Sep 17 00:00:00 2001 From: Eric Willhoit Date: Tue, 9 Jul 2024 11:02:15 -0500 Subject: [PATCH 1/3] fix: util.format list issue --- src/messages.ts | 12 ++++++++++-- test/unit/messagesTest.ts | 6 +++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/messages.ts b/src/messages.ts index cf18a666e..60c17e686 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -588,8 +588,16 @@ export class Messages { } const messages = ensureArray(msg); return messages.map((message) => { - ensureString(message); - return util.format(message, ...tokens); + const msgStr = ensureString(message); + // If the message does not contain a specifier, util.format still appends the token to the end. + // The 'markdownLoader' automatically splits bulleted lists into arrays. + // This causes the token to be appended to each line regardless of the presence of a specifier. + // Here we check for the presence of a specifier and only format the message if one is present. + // https://nodejs.org/api/util.html#utilformatformat-args + // https://regex101.com/r/8Hf8Z6/1 + const specifierRegex = new RegExp('%[sdifjoO]{1}', 'gm'); + + return specifierRegex.test(msgStr) ? util.format(msgStr, ...tokens) : msgStr; }); } } diff --git a/test/unit/messagesTest.ts b/test/unit/messagesTest.ts index a72e1a10b..63ab1f11b 100644 --- a/test/unit/messagesTest.ts +++ b/test/unit/messagesTest.ts @@ -67,14 +67,14 @@ describe('Messages', () => { it('should return single string from array of messages', () => { expect(messages.getMessage('manyMsgs', ['blah', 864])).to.equal( - `hello blah 864${EOL}world blah 864${EOL}test message 2 blah and 864` + `hello${EOL}world${EOL}test message 2 blah and 864` ); }); it('should return multiple string from array of messages', () => { expect(messages.getMessages('manyMsgs', ['blah', 864])).to.deep.equal([ - 'hello blah 864', - 'world blah 864', + 'hello', + 'world', 'test message 2 blah and 864', ]); }); From 4989731327123c856f2f2ef6ceadb3df95d40a72 Mon Sep 17 00:00:00 2001 From: Eric Willhoit Date: Tue, 9 Jul 2024 13:46:20 -0500 Subject: [PATCH 2/3] fix: swap for temp telemetry event --- src/messages.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/messages.ts b/src/messages.ts index 60c17e686..dea911c98 100644 --- a/src/messages.ts +++ b/src/messages.ts @@ -12,6 +12,7 @@ import * as util from 'node:util'; import { fileURLToPath } from 'node:url'; import { AnyJson, asString, ensureJsonMap, ensureString, isJsonMap, isObject } from '@salesforce/ts-types'; import { ensureArray, upperFirst } from '@salesforce/kit'; +import { Lifecycle } from './lifecycleEvents'; import { SfError } from './sfError'; export type Tokens = Array; @@ -597,7 +598,22 @@ export class Messages { // https://regex101.com/r/8Hf8Z6/1 const specifierRegex = new RegExp('%[sdifjoO]{1}', 'gm'); - return specifierRegex.test(msgStr) ? util.format(msgStr, ...tokens) : msgStr; + // NOTE: This is a temporary telemetry event to track down missing specifiers in messages. + // Once we have enough data and correct missing specifiers, we can remove this. + // The followup work is outlined in: W-16197665 + if (!specifierRegex.test(msgStr) && tokens.length > 0) { + void Lifecycle.getInstance().emitTelemetry({ + eventName: 'missing_message_specifier', + library: 'sfdx-core', + function: 'getMessageWithMap', + messagesLength: messages.length, + message: msgStr, + tokensLength: tokens.length, + }); + } + + // return specifierRegex.test(msgStr) ? util.format(msgStr, ...tokens) : msgStr; + return util.format(msgStr, ...tokens); }); } } From 4c520101a0405e9b17f6c6bb89095437f69f4f0d Mon Sep 17 00:00:00 2001 From: Eric Willhoit Date: Tue, 9 Jul 2024 13:51:22 -0500 Subject: [PATCH 3/3] chore: revert test --- test/unit/messagesTest.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/unit/messagesTest.ts b/test/unit/messagesTest.ts index 63ab1f11b..a72e1a10b 100644 --- a/test/unit/messagesTest.ts +++ b/test/unit/messagesTest.ts @@ -67,14 +67,14 @@ describe('Messages', () => { it('should return single string from array of messages', () => { expect(messages.getMessage('manyMsgs', ['blah', 864])).to.equal( - `hello${EOL}world${EOL}test message 2 blah and 864` + `hello blah 864${EOL}world blah 864${EOL}test message 2 blah and 864` ); }); it('should return multiple string from array of messages', () => { expect(messages.getMessages('manyMsgs', ['blah', 864])).to.deep.equal([ - 'hello', - 'world', + 'hello blah 864', + 'world blah 864', 'test message 2 blah and 864', ]); });