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

Load custom formatter by relative path only if it begins with a dot #1413

Merged
merged 6 commits into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ If anything is missing from the migration guide, please submit an issue.
* Custom formatters will need to migrate
* `json` formatter is deprecated and will be removed in next major release. Custom formatters should migrate to use the `message` formatter, or the [standalone JSON formatter](https://github.com/cucumber/cucumber/tree/master/json-formatter) as a stopgap.
* Remove long-deprecated `typeName` from options object for `defineParameterType` in favour of `name`
* Custom formatters are no longer loaded relative to the current directory unless it begins with a dot (e.g. `--format=./relpath/to/formatter`)
davidjgoss marked this conversation as resolved.
Show resolved Hide resolved

### Bug fixes

Expand Down
19 changes: 6 additions & 13 deletions features/support/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,12 @@ export class World {
envOverride: NodeJS.ProcessEnv = null
): Promise<void> {
const messageFilename = 'message.ndjson'
const args = ['node', executablePath]
.concat(inputArgs, [
'--backtrace',
'--predictable-ids',
'--format',
`message:${messageFilename}`,
])
.map((arg) => {
if (_.includes(arg, '/')) {
return path.normalize(arg)
}
return arg
})
const args = ['node', executablePath].concat(inputArgs, [
'--backtrace',
'--predictable-ids',
'--format',
`message:${messageFilename}`,
])
const env = _.merge({}, process.env, this.sharedEnv, envOverride)
const cwd = this.tmpDir

Expand Down
11 changes: 9 additions & 2 deletions src/formatter/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ const FormatterBuilder = {
},

loadCustomFormatter(customFormatterPath: string, cwd: string) {
const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath)
const CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
let CustomFormatter = null

if (customFormatterPath.startsWith('.')) {
const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath)
CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
} else {
CustomFormatter = require(customFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
Copy link

Choose a reason for hiding this comment

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

This should be using createRequire to run the require from the cwd

Copy link
Contributor

@davidjgoss davidjgoss Nov 24, 2020

Choose a reason for hiding this comment

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

Yeah that seems a fair point, although per the docs:

module.createRequire(filename)#

Added in: v12.2.0

Given we're still supporting Node 10 for the time being (its EOL is April 2021), could use https://github.com/sindresorhus/import-cwd instead.

Copy link

@merceyz merceyz Nov 25, 2020

Choose a reason for hiding this comment

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

I'd suggest using https://yarnpkg.com/package/create-require as it will use the node builtins when it can, so createRequire on node >= 12.2 createRequireFromPath on node >= 10.12, then a polyfil for the rest which is much better

Copy link

Choose a reason for hiding this comment

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

Should actually use it for both branches here, which simplifies the logic and has the same end result

Copy link
Member Author

Choose a reason for hiding this comment

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

Added the above, works great for both cases, thanks for pointing it out

}

Copy link

@merceyz merceyz Nov 25, 2020

Choose a reason for hiding this comment

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

Adding create-require to the dependencies and importing it simplifies this down to

Suggested change
if (customFormatterPath.startsWith('.')) {
const fullCustomFormatterPath = path.resolve(cwd, customFormatterPath)
CustomFormatter = require(fullCustomFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
} else {
CustomFormatter = require(customFormatterPath) // eslint-disable-line @typescript-eslint/no-var-requires
}
CustomFormatter = createRequire(cwd)(customFormatterPath)

Copy link
Member Author

Choose a reason for hiding this comment

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

Switched to this

if (typeof CustomFormatter === 'function') {
return CustomFormatter
} else if (
Expand Down