Skip to content

feat(rules): make body-max-line-length ignore lines with URLs #4486

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

aspiers
Copy link

@aspiers aspiers commented Jun 29, 2025

Description

Whilst the body-max-line-length is almost always desirable, there is one fairly frequent scenario in which it gets in the way: when lines in the body of the commit message contain URLs exceeding the maximum line length.

In this case, the URL cannot be split across lines without breaking it, and minifying it results in obfuscation and a dependency on a third-party service, both of which are undesirable.

So make an exception in this rule for any line which contains a URL. Allow other content on the same line, in order to support use of various rich-text formats such as Markdown, which are often used in order to render links in more elegant ways.

Motivation and Context

After lengthy discussion in #2112 of how to handle long URLs, this PR proposes a simple fix which seems to be closest to achieving broad consensus as a solid first step. More sophisticated refinements could potentially be layered on later, although there is not yet clear consensus what those might be.

Usage examples

// commitlint.config.js
module.exports = {};
cat <<EOF | yarn commitlint # passes
chore: some test commit header

This is the body, and it contains a
https://example.com/URL/with/a/very/very/very/very/very/very/very/very/very/long/path
EOF

How Has This Been Tested?

Relevant unit tests have been added, and it's been tested from the CLI as shown above.

Types of changes

Technically this is a change of existing functionality, although in practice it's so minor that it's more like a new feature.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Previously the multi-line test fixtures in the test suite for
the `body-max-line-length` rule were defined but never used;
instead, the single line fixtures were being used in tests
supposed to test multi-line commit messages.

This was presumably an innocent copy-and-paste error, so fix the
multi-line tests to use the multi-line fixtures.
Copy link

codesandbox-ci bot commented Jun 29, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@denilsonsa
Copy link

Can you also please do it for the footer-max-line-length?

I've noticed that commitlint was complaining about my long URLs because it was thinking those were part of the footer. So in my application I've changed both the body and the footer rules to just warning instead of error.

(I haven't even tried your PR. I'm just talking from my experience as someone working on a project that has commitlint enabled.)

Copy link

@FelixZY FelixZY left a comment

Choose a reason for hiding this comment

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

While I have been a proponent of a more complex solution, I would like to commend you, @aspiers, for digging into this and doing what many (myself included) did not - submitting an actual solution! Thank you for looking into this! I'm hoping it will be merged and am looking forward to make use of the changes.

Note for other reviewers: I've only made a passing read-through on my phone - actual code owners should not rely on my review as complete.

//
// This is overly lenient, in order to avoid costly regexps which
// have to worry about all the many edge cases of valid URLs.
const URL_REGEX = /\bhttps?:\/\/\S+\.\S+/;
Copy link

Choose a reason for hiding this comment

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

I wonder if it would be possible to support generalised URIs (i.e. other URI schemes than http(s) as well, e.g. to allow ftp:// or urn:isbn: style URIs?

I suppose this would require an even more permissive regexp which may not be desired (such as \S+:\S+). An alternative would be to support only the official IANA registered schemes (e.g. (https?|ftp|urn)).

Copy link
Author

Choose a reason for hiding this comment

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

\S+:\S+ would indeed be way too permissive, and result in permitting all kinds of long lines which do not contain genuine URIs.

The list of official IANA registered schemes is extremely long and constantly changing, so I don't see that as a practical way forward either. Plus for many URI schemes there is no good reason for them to be super long, e.g. mailto:, tel:, and presumably urn:isbn: too.

We could use a small hardcoded list of the most popular protocols, e.g. /https?|ftp|file/ but I think anything much more complex than this is allowing the quest for perfection to become the enemy of good enough. I'm very confident that the vast majority of cases where this rule is currently getting in the way are all related to https URLs.

//
// This is overly lenient, in order to avoid costly regexps which
// have to worry about all the many edge cases of valid URLs.
const URL_REGEX = /\bhttps?:\/\/\S+\.\S+/;
Copy link

Choose a reason for hiding this comment

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

I'd suggest dropping the trailing \.\S+ part of the regex. I don't see the relevance and it will exclude URLs that use a single hostname, e.g. http://localhost/hello/world.

Copy link
Author

Choose a reason for hiding this comment

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

I do wonder how often a localhost URL needed in a commit message is likely to be super long, but OK I can imagine this happening as a corner case so I can change that.

Copy link
Author

Choose a reason for hiding this comment

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

Dropped \.\S+ and force-pushed so this is now resolved.

Copy link

@denilsonsa denilsonsa Jul 2, 2025

Choose a reason for hiding this comment

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

I do wonder how often a localhost URL needed in a commit message is likely to be super long

Manufactured example that reflects real-life:

This commits fixes a bug in the page foobar when the parameters are fizzbuzz.
http://localhost/application/management/foo/bar-manager?foo=12345678&bar=12345678&fizz=12345678&buzz=12345678

Adding URLs to the localhost dev server can be a simple way to explain how to reproduce a certain bug or how to access a certain new feature.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that's exactly what I was imagining. Like I said, I suspect it's a rare corner case that the combination of the path and query params are long enough to trigger the rule. But anyway, this case is now covered just in case it happens.

@escapedcat escapedcat requested a review from JounQin June 30, 2025 09:15
Copy link
Collaborator

@JounQin JounQin left a comment

Choose a reason for hiding this comment

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

I agree that we should support other url protocols at the same time.

@aspiers
Copy link
Author

aspiers commented Jul 1, 2025

I agree that we should support other url protocols at the same time.

As per my above comment, please can you specify exactly which ones you think need to be supported?

@aspiers
Copy link
Author

aspiers commented Jul 1, 2025

It just occurred to me that javascript:<some JS code> is a reasonable corner case which might require a long URI, e.g. when documenting use of bookmarklets.

But again, I think that worrying about these corner cases inside this PR is probably a mistake, because they can be dealt with incrementally in later refinements. This is preferable to further delaying a fix to what we already know is by far the most common problem, i.e. commit messages which want to refer to external web pages with long https URLs. Until we've dealt with that most common case, I suspect that we're mostly just speculating on the relevance of the other corner cases, rather than relying on more empirical feedback from users on concrete instances where the rule isn't doing what they want.

Whilst the `body-max-line-length` is almost always desirable, there
is one fairly frequent scenario in which it gets in the way: when
lines contain URLs exceeding the maximum line length.

In this case, the URL cannot be split across lines without breaking
it, and minifying it results in obfuscation and a dependency on a
third-party service, both of which are undesirable.

So make an exception in this rule for any line which contains a URL.
Allow other content on the same line, in order to support use of
various rich-text formats such as Markdown, which are often used
in order to render links in more elegant ways.
@aspiers aspiers force-pushed the body-max-line-length-URLs-exception branch from c7dc1e6 to 13a7b75 Compare July 1, 2025 12:55
@JounQin
Copy link
Collaborator

JounQin commented Jul 1, 2025

I think only including http(s):// is a good start and let's gather feedbacks later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants