Skip to content

require: invalid examples from doc comments as functions don't return bool #1776

Open

Description

@fragglet

Description

The doc comments for multiple functions in the require package have code examples that are invalid. Example:

// if require.NoError(t, err) {

The example reads:

	  if require.NoError(t, err) {
		   require.Equal(t, expectedObj, actualObj)
	  }

but the NoError() function does not return any value, so this is invalid.

There are similar invalid examples in the doc comments for NoErrorf, NotEmpty and NotEmptyf.

Activity

  1. brackendawson commented on Aug 6, 2025

    @brackendawson
    Collaborator

    Same as #1609 for require.Error but we missed some. The require documentation is generated from the assert documentation. We either need to make the assert documentation work for both packages or stop generating the one from the other.

  2. PCloud63514 commented on Aug 8, 2025

    @PCloud63514

    Hello! I was wondering if this issue has already been resolved. Could you please confirm? Thank you!

    #1675

  3. fragglet commented on Aug 8, 2025

    @fragglet
    Author

    Hello! I was wondering if this issue has already been resolved. Could you please confirm? Thank you!

    No, #1675 fixed require.Error but not the other functions I've listed above.

  4. PCloud63514 commented on Aug 8, 2025

    @PCloud63514

    I'd like to contribute to resolving this issue!

    In my opinion, even though assert and require are similar, they have different responsibilities.
    Wouldn't it be better to maintain separate documentation for each?

  5. brackendawson commented on Aug 17, 2025

    @brackendawson
    Collaborator

    I've assigned you, @PCloud63514.

    As with all things; maintaining separate docs is a trade-off. Most functions in require actually call their counterpart in assert, this ensures that they are identical. Most of the assert functions' docs convert cleanly to require. The error functions can have a doc example that converts cleanly, #1675 shows this. It's possible to move these functions to files outside of the generated files to have separate documentation, but we'd rather not maintain two identical sets of functions for these error assertions just to allow their docs to be different. It's also possible to do something else to use the same implementation but different docs, but in that case any update to the documentation might not notice that the require docs also need to be updated by hand.

    I'll leave the implementation up to you. Propose a good trade-off and we'll consider it. Otherwise if you just update the docs to a version which converts cleanly I'd be happy merging that PR.

  6. PCloud63514 commented on Aug 20, 2025

    @PCloud63514

    @brackendawson Sorry for the late reply. 😅
    I’m not sure if I expressed my point clearly before.

    My view is that the responsibility of 'require' is to fail immediately, not to return a value.
    Therefore, I believe updating the doc comments is the correct fix.
    For this purpose, I have added a doc generation method for the require package.

    I’ve opened a PR with the revised form!

  7. ccoVeille commented on Aug 21, 2025

    @ccoVeille
    Collaborator

    The more I think about it, the more I feel we should remove all code from comment and use Go Example.

    They can also be generated, but they can be formatted

  8. changed the title [-]Examples from doc comments are invalid[/-] [+]require: invalid examples from doc comments as functions don't return boo[/+] on Aug 25, 2025
  9. changed the title [-]require: invalid examples from doc comments as functions don't return boo[/-] [+]require: invalid examples from doc comments as functions don't return bool[/+] on Aug 25, 2025
  10. ccarcaci commented on May 23, 2026

    @ccarcaci

    @dolmen

    I was looking for a "good first issue" where to start contributing from. This one has been solved by the PR #1850

    I guess this issue should be closed.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions