Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util.format list issue #1099

Merged
merged 3 commits into from
Jul 9, 2024
Merged
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
28 changes: 26 additions & 2 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | boolean | number | null | undefined>;
Expand Down Expand Up @@ -588,8 +589,31 @@ export class Messages<T extends string> {
}
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
Copy link
Contributor

@mdonnalley mdonnalley Jul 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a link to the regex is brilliant 🏆

const specifierRegex = new RegExp('%[sdifjoO]{1}', 'gm');

// 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);
});
}
}
Expand Down
Loading