Skip to content

Yargs v17 calls coerce with undefined when option is not passed #2130

@dsamerotte

Description

@dsamerotte

Per #1219, yargs has not historically called coerce when the associated option is not passed. However, when porting my code to v17, I see that this is no longer the case, and I'm wondering whether this is an intentional breaking change or a regression. The only potentially relevant breaking change that I see in the release notes is that "coerce is now applied before validation," but I wouldn't expect this change in behavior from that description alone.

I understand that yarg's middleware underwent substantial changes in v17. Based on my two-second investigation, it looks like v16 maps over the supplied options and then calls coerce as needed, such that coerce can never receive undefined. However, v17 instead appears to reduce all middleware without checking the existence of the related option.

The obvious result is that code like .coerce('dir', path.resolve), which still appears in the docs, no longer works, and client code now needs to explicitly check for undefined.

If this is an intentional change, should this also affect subcommand processing? I'm even seeing coerce called like this in parent commands when calling only a subcommand and when the parent option is not passed and is marked as global: false. If I understand the global option correctly, this seems especially strange.

I thought I'd keep this short and start with the high-level question first. Based on the answer, I can, of course, provide additional thoughts and information. Thanks!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions