question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cli format option doesn't load modules

See original GitHub issue

I’ve been busy with some formatters for Commitlint, so far I’ve created two; json and junit formats.

During initial testing, I encountered some unexpected behavior related to importing these formatters in the CLI package. On line 268, in cli.js, we use require.resolve. I mistakenly assumed it would handle global/local modules and absolute/relative file paths. Unfortunately, this isn’t the case. I’ve tried to use flags.cwd in the resolve method, but this only seems to fix locally installed modules and relative file paths.

TL;DR; Format module resolves must be improved, I suggest resolve-from and resolve-global. What do you think? I will create a PR with the solution listed below!

Expected Behavior

$ echo 'foo: bar' | npx commitlint -o commitlint-format-junit
<?xml version="1.0" encoding="utf-8"?>
<testsuites>
  ...
</testsuites>

Current Behavior

$ echo 'foo: bar' | npx commitlint -o commitlint-format-junit
/usr/local/lib/node_modules/commitlint/node_modules/@commitlint/cli/lib/cli.js:272
	throw err;

Affected packages

  • cli
  • core
  • prompt
  • config-angular

Possible Solution

So, I took a look at @commitlint/load how it’s loading the configuration. I noticed that cosmiconfig is used, so I started digging there. It turns out; it’s pretty hard to resolve things like this. I would recommend using an external library to fix this. So far I found two libraries, both from Sindre Sorhus which should cover all of our needs; resolve-from and resolve-global (both MIT licensed).

With this we can do this:

function loadFormatter(config, flags) {
	const moduleName = flags.format || config.formatter;
	const moduleResolvers = [
		name => resolveFrom.silent(flags.cwd, name),
		name => resolveGlobal.silent(name),
	];

	const modulePath = moduleResolvers.find(resolve => resolve(moduleName));

	if (modulePath) {
		return require(modulePath);
	}

	throw new Error(`Cannot find format module ${moduleName}`);
}

I’ve briefly talked with demurgos, from node-tooling slack, about this. And I agree with him that local should be resolved before global, so the order is intentional.

Steps to Reproduce (for bugs)

  1. npm install -g commitlint commitlint-format-junit
  2. echo 'foo: bar' | npx commitlint -o commitlint-format-junit

Your Environment

I’m running this from a docker container, so I wouldn’t pollute my own global installations 😄If you want to use the same environment, spin it up with:

$ docker run -ti --rm -v $PWD:/code -w /code bycedric/ci-node sh
# apk add --no-cache git
Executable Version
commitlint --version 7.2.0
git --version 2.18.0
node --version v10.11.0

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
marioneblcommented, Oct 9, 2018

@byCedric I added a review to your PR. Apart from a minor thing I am fine with all your points - great work! 👍

Are you be interested in maintaining commitlint? I’ll be on vacation for two weeks starting on Friday 12th, so I’d introduce you to the project when I am back. What do you think?

1reaction
marioneblcommented, Oct 7, 2018

Good catch! Going with resolve-from for starters should be enough. I think there is some complexity in this when thinking it through

  • As a user i’d expect formatters configured via cli flags to resolve from process.cwd()
  • formatters configured on commitlint.config.js should be resolved from the dirname of the config file they were specified in
Read more comments on GitHub >

github_iconTop Results From Across the Web

latest - Environment Modules
The MODULES_AUTO_HANDLING environment variable is defined by config sub-command when changing this configuration option from its default value. The --auto and ...
Read more >
In VS Code, I'm getting this error, 'Failed to load module ...
Running the script from the package.json file doesn't apply prettier but if I format it myself (option+shift+f on a mac) it works. So...
Read more >
Command-line parameter to force "module" instead ... - GitHub
I'm aware that I can name a file with .mjs or set "type": "module" in package.json. I'm also aware of the --input-type=module CLI...
Read more >
Setting the AWS CLI output format - AWS Documentation
This topic describes the different output formats for the AWS Command Line Interface (AWS CLI). The AWS CLI supports the following output formats:....
Read more >
CLI options - Terragrunt
Runs the provided terraform command against a stack , where a stack is a tree of terragrunt modules. The command will recursively find...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found