-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Load custom formatter by relative path only if it begins with a dot #1413
Conversation
7347adc
to
6d01071
Compare
Would we consider making this more explicit, maybe borrowing webpack's tilda syntax:
$ cucumber-js --format "~@my-scope/my-formatter-package" Normal (current) behaviour: $ cucumber-js --format "lib/formatters/local-custom-formatter.js" (Let me know if I've missed why this wouldn't work!) |
@davidjgoss I don't think this is about a node module vs not. Global node modules cannot be required regularly and local node modules should be fine with being referenced relative to the current path. @dawn-minion I don't understand why this is needed. The current implementation should work fine with absolute paths - which is what I assume you are using for globally installed modules. I'll comment on the issue as I want more specifics about the current problem you're experiencing |
Okay plug in play is very interesting. Let's discuss this more in the issue. |
6d01071
to
ac93d12
Compare
@charlierudolph Sorry for the delay, been busy with other things. I believe that should be it here. |
Code changes look good. Can you please add a note to the changelog under breaking changes? |
This commit makes it possible to load a custom formatter that is not relative to the current working directory, such as one installed globally, or via Yarn PNP. If the path begins with a . it will be loaded relative to the current directory.
bfcac12
to
4bb5a34
Compare
@charlierudolph Should be ready now - had to remove the relative path parser from world.ts as well, as it would strip the leading ./ from the tests. All tests should be passing now |
src/formatter/builder.ts
Outdated
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 |
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.
This should be using createRequire
to run the require from the cwd
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.
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.
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.
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
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.
Should actually use it for both branches here, which simplifies the logic and has the same end result
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.
Added the above, works great for both cases, thanks for pointing it out
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.
LGTM, a couple of suggestions.
src/formatter/builder.ts
Outdated
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 |
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.
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.
src/formatter/builder.ts
Outdated
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 | ||
} | ||
|
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.
Adding create-require
to the dependencies and importing it simplifies this down to
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) |
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.
Switched to this
Sorry for the delay here, been a bit busy with a move. I've added a note about Yarn PnP support to the changelog, added create-require here as @merceyz suggested which simplifies the code. Ready for review again. |
src/formatter/builder.ts
Outdated
let CustomFormatter = null | ||
|
||
CustomFormatter = createRequire(cwd)(customFormatterPath) |
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.
let CustomFormatter = null | |
CustomFormatter = createRequire(cwd)(customFormatterPath) | |
const CustomFormatter = createRequire(cwd)(customFormatterPath) |
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.
Changed
CHANGELOG.md
Outdated
@@ -56,6 +57,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`). |
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.
NIT: They are always loaded relative to the cwd now
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.
Right, the note being that it uses the regular require search path relative to cwd now, instead of always trying to load a file by path from the cwd
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.
Hopefully that's a bit clearer now.
Can't seem to reproduce the test failure on appveyor, it's passing locally here with node v10.23.0 |
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.
@dawn-minion I think the CI failure is an (unrelated) unstable test per #1457.
This commit makes it possible to load a custom formatter that is not relative to the current working directory, such as one installed
globally, or via Yarn PNP. This change makes it so we only load by a relative path if it begins with a dot, otherwise it will load
it as a regular non-relative require.
Fixes #1412