taplo icon indicating copy to clipboard operation
taplo copied to clipboard

Version Key Is Required in `pyproject.toml`

Open BrutalSimplicity opened this issue 2 years ago • 1 comments

It seems that the taplo cli will fail to validate schemas for pyproject.toml when the version is not specified despite the version not being required by the jsonschema specification.

You can reproduce with a pyproject.toml file like below:

[project]
name = "deployment_handler"
requires-python = ">=3.9"

This works.

[project]
name = "deployment_handler"
version = "1.0.0"
requires-python = ">=3.9"

It seems that taplo may be associating the pyproject file to another schema when version is not specified.

BrutalSimplicity avatar Sep 04 '22 22:09 BrutalSimplicity

Running

RUST_LOG=taplo=debug taplo check pyproject.toml --default-schema-catalog

confirmed it uses the correct schema (https://json.schemastore.org/pyproject.json).

The upstream validator also succeeds, it's probably a TOML->JSON conversion issue somewhere.

tamasfe avatar Sep 04 '22 22:09 tamasfe

@tamasfe this seems to be caused by let msg = err.error.to_string(); at taplo/crates/taplo-cli/src/printing.rs:125. Since err.error is a jsonschema::ValidationError, it doesnt seem reasonable to do anything but patch with a more friendly explanation. That error instance is a

ValidationError {
        instance: Object {
            "name": String("hello"),
        },
        kind: OneOfNotValid,
        instance_path: JSONPointer(
            [
                Property(
                    "project",
                ),
            ],
        ),
        schema_path: JSONPointer(
            [
                Keyword(
                    "properties",
                ),
                Property(
                    "project",
                ),
                Keyword(
                    "oneOf",
                ),
            ],
        ),
    },

but a string representation is unhelpful. Edit for clarity: the pyprojects schema requires one of ["dynamic", "version"] keys to be defined along with the "name" key. However, the error message doesnt provide that information (and the error struct too!)

iliya-malecki avatar Jun 05 '23 16:06 iliya-malecki

I'll try to reproduce and investigate this when I'll get computer access in a few weeks. Thanks for the debugging!

ia0 avatar Jun 05 '23 21:06 ia0

I was able to reproduce the original error, which for the record is:

{"name":"deployment_handler","requires-python":">=3.9"} is not valid under any of the given schemas

I'm not sure what alternative behavior from taplo you would expect. I would argue that this is the job of jsonschema to produce a readable error message (ValidationError implements Display which is what taplo uses). It looks like you already fixed the message for OneOfNotValid errors in https://github.com/Stranger6667/jsonschema-rs/pull/430 a few days after your message, so I guess you reached a similar conclusion as I do?

So I would suggest bumping jsonschema version from 0.16.0 to the next release (the one after 0.17.0) to benefit from your fix. Does this sound good to you?

ia0 avatar Jun 18 '23 19:06 ia0

Yup, the conclusion was exactly that, so i was just waiting for the 0.18 to suggest bumping :)

iliya-malecki avatar Jun 18 '23 21:06 iliya-malecki

please bump, 0.17.1 incorporates the needed changes

iliya-malecki avatar Jul 07 '23 15:07 iliya-malecki

Thanks, I've created #428.

ia0 avatar Jul 07 '23 22:07 ia0

Version 0.8.1 has been released.

ia0 avatar Aug 08 '23 14:08 ia0