-
Notifications
You must be signed in to change notification settings - Fork 472
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
build(lint): throw error when number of extra args doesn't match the number of format specifiers in logger. #5895
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like theres a couple more linting that fail at the bottom of this PR as well
@@ -78,7 +78,7 @@ describe('samProject', () => { | |||
// simulate error when no samconfig.toml file in directory | |||
const result = await getStackName(projectRoot) | |||
assert.deepStrictEqual(result, {}) | |||
assertLogsContain('No stack name or region information available in samconfig.toml', true, 'info') | |||
assertLogsContain('No stack name or region information available in samconfig.toml: %O', true, 'info') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the log literally contain a "%O"
string? If so, that seems like a bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but believe it is only because we are in the TestLogger
, which adds the raw strings to an array. (https://github.com/aws/aws-toolkit-vscode/blob/b2ebc10342f63ac95cd3055393e715cb369ec450/packages/core/src/test/testLogger.ts)
plugins/eslint-plugin-aws-toolkits/lib/rules/no-string-sub-mismatch.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Justin M. Keyes <[email protected]>
…kit-vscode into lint/noStringSubDrop
## Problem The test logger does not format additional arguements into the logs in the same way as ToolkitLogger. Ex. `getLogger().debug('here is the obj: %O', obj)` will produce two `LogEntries` one with `"here is the obj: %O"` and one with the object itself. This is problematic because it causes confusion (see: #5895 (comment)) and it also can cause `assertLogsContain` to throw an error since there is now a non-string/error entry in the log entry list (see: https://github.com/aws/aws-toolkit-vscode/blob/338ea67f2ba0294fc535a9a949fd1cdaeaa96d98/packages/core/src/test/globalSetup.test.ts#L171-L185). ## Solution - Have test logger inherit from `BaseLogger` to minimize implementation dupe. - If we see a string, format it with extra inputs before pushing it into entries. - Add tests for the `testLogger`. - Adjust existing tests that relied on this behavior. - If on web, we simply concat the strings to avoid reimplementing `util.format`: https://github.com/nodejs/node/blob/3178a762d6a2b1a37b74f02266eea0f3d86603be/lib/internal/util/inspect.js#L2191-L2315 --- <!--- REMINDER: Ensure that your PR meets the guidelines in CONTRIBUTING.md --> License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
Builds off of:
#5878
Problem
currently
logger.error(msg, err)
silently dropserr
if it doesn't map to a%x
format specifier in msg.we want to either prevent that mistake, or enhance our logger so that it prints useful parts of err instead of silently dropping it.
Also, there are cases like:
getLogger().error('Certificate path {0} already exists', certPath)
, but this silently drops the argcertPath
. Pretty sure this syntax is from Java IIRC and not supported by Node.Solution
Provide a custom lint rule to ensure the number of extra args matches the number of format specifiers.
Note About Alternatives
meta
items to strings and appended them to the log message or add them as separate log messages. This would allow usage of the formgetLogger().error('hit error', e)
.logger.error
statement corresponds to one message in the log.Notes
Relevant: code used to format errors:
aws-toolkit-vscode/packages/core/src/shared/errors.ts
Lines 329 to 334 in f806ae0
which should now actually be used since errors won't be dropped.
License: I confirm that my contribution is made under the terms of the Apache 2.0 license.