vscode-yaml icon indicating copy to clipboard operation
vscode-yaml copied to clipboard

Unable to parse YAML schema

Open reececomo opened this issue 2 years ago • 8 comments

Describe the bug

A YAML schema fails to parse.

Unable to parse content from '[...]/_schema/configSchema.yml': Parse error at offset 0.YAML(768)

Works correctly in other IDEs, but not in VSCode/with this extension.

We would like to support our developers who use VSCode. We use extensive comments/notes in our YAML, so we prefer YAML over JSON.

Expected Behavior

YAML-defined schemas (i.e. my-schema.yaml) should work correctly.

Current Behavior

Unable to parse content from '[...]/_schema/configSchema.yml': Parse error at offset 0.YAML(768)

Steps to Reproduce

  1. Define config.yml and _schema/configSchema.yml
  2. Add # yaml-language-server: $schema=_schema/configSchema.yml to config.yml
  3. 💥

Environment

  • [x] Windows
  • [x] Mac
  • [x] Linux

reececomo avatar Dec 20 '22 23:12 reececomo

It is impossible to determine why it fails to parse without looking at the congifSchema.yml. If you can provide the schema we can check.

gorkem avatar Dec 21 '22 21:12 gorkem

Fair point @gorkem - for context:

  • We've tried using a handful of YAML files — with varying levels of detail, variations of headers, etc. — none appear to parse.
  • They work fine in other IDEs
  • They validate with YAML validators

Example schema

Here's an example configSchema.yml:

---
"$schema": http://json-schema.org/draft-06/schema#
"$ref": "#/definitions/I18nConfig"

definitions:
  I18nConfig:
    type: object
    additionalProperties: false
    properties:
      defaultLanguage:
        "$ref": "#/definitions/LanguageTag"
      languages:
        type: object
        additionalProperties: false
        patternProperties:
          "^[a-z]{2,3}(?:-[a-zA-Z0-9]+)?$":
            "$ref": "#/definitions/LanguageConfig"
    required:
      - defaultLanguage
      - languages

  LanguageConfig:
    type: object
    additionalProperties: false
    properties:
      debugName:
        type: string
        minLength: 1
      localizedName:
        type: string
        minLength: 1
      script:
        "$ref": "#/definitions/LanguageScript"
      fallback:
        type: array
        items:
          "$ref": "#/definitions/LanguageTag"
      javaConstant:
        type: string
        pattern: ^[A-Z][A-Z_]+[A-Z]$
      direction:
        type: string
        enum: ['ltr', 'rtl']
    required:
      - debugName
      - localizedName
      - script

  LanguageTag:
    type: string
    minLength: 2
    pattern: ^[a-z]{2,3}(?:-[A-Z0-9]+)?$

  LanguageScript:
    type: string
    enum: [ 'Latn', 'Cyrl', 'Arab', 'Deva' ]

reececomo avatar Dec 22 '22 00:12 reececomo

Also:

Example yaml file

Here is an example config file for reference:

# yaml-language-server: $schema=_schema/configSchema.yml

---

defaultLanguage: en

languages:
  ar:
    debugName: Arabic
    localizedName: 'عربي'
    script: Arab
    direction: rtl

  da-DK:
    debugName: Danish
    localizedName: 'Dansk'
    script: Latn

  en:
    debugName: English
    localizedName: 'English'
    script: Latn

  en-GB:
    debugName: English (United Kingdom)
    localizedName: 'English (United Kingdom)'
    script: Latn
    javaConstant: ENGLISH_UK
    
  es-419:
    debugName: Spanish (Latin America)
    localizedName: 'Español (Latinoamerica)'
    script: Latn
    fallback: ['es-ES']
    javaConstant: SPANISH_419

  es-ES:
    debugName: Spanish (Spain)
    localizedName: 'Español (España)'
    script: Latn

reececomo avatar Dec 22 '22 00:12 reececomo

I have the same problem. Do we need more info to resolve this?

Here is what I have tested:

  • Create an empty VSCode project.
  • (My env: MacOS Big Sur 11.7.2, VS Code Version: 1.75.1, YAML extension v1.11.0)
  • Create test.yaml with one line content:
    test: My test
    
  • Create test.schema.yaml with content:
    %YAML 1.1
    ---
    id: "my-test-schema-id"
    $schema: "http://json-schema.org/draft-04/schema"
    title:
      A Test YAML Schema
    description: |
      Test schema.
    
    properties:
      test:
        type: string
        description: A test property
    
  • Add workspace settings at .vscode/settings.json,
    {
        "yaml.schemas": {
            "./test.schema.yaml": ["/test.yaml"]
        }
    }
    
  • Observe the error
    • Unable to load schema from 'test-yaml-schema/test.schema.yaml': No content.YAML(768)
    • The error appears at the first character below --- in test.schema.yaml, shown as a red "~" underline
    • The error also appears at the first charater in test.yaml, shown as a red "~" underline
  • Variations
    • If I move the $schema: "http://json-schema.org/draft-04/schema" line above to the first line below --- in test.schema.yaml, the red "~" will appear at "http://json-schema.org/draft-04/schema". Network is fine and I can access the url.

bradleyzhou avatar Feb 13 '23 03:02 bradleyzhou

Hi,

I am having the same problem. I tested my schema file with eemeli/yaml and the YAML parses just fine. Converting the schema from YAML to JSON and then using this works. It would be good to have some error message that is more meaningful than "Parse error at offset 0.YAML(768)". What is 768?

I am using a Mac running Ventura 13.5.2 (22G91), VS code 1.82.0 and the YAML plugin v1.14.0.

Document to validate:

%YAML 1.1
---
id: test_rights
description: >
  A test file with some random rights used to evaluate the validation of YAML
  files against the JSON schema.

rights:

  - privilege:
    id: to-use-colons
    description: >
      A generic privilege that the right holder is permitted to do something.
      In this case: to use a colon in a multi-line string. Seems to be working
      just fine?

Schema:

%YAML 1.1
---
$schema: http://json-schema.org/draft-07/schema#
$id: http://rightsdriven.org/schemas/torshi/rightsdriven.json
$comment: >
  Torshi schema for specifications of rights.
  Copyright (c) 2023 Alexander Voss

title: RightsDriven
type: object
required: [id, description, rights]
additionalProperties: false

properties:

  id:
    type: string
    title: identifier
    description: An identifier for the system of rights.

  description:
    type: string

  name:
    type: string
    title: description
    description: A human-readable description of the system of rights.

  rights:
    type: array
    items:
      anyOf:
        - rights-privilege:
            $ref: "#/$defs/right-privilege"
        - right-claim-right:
            $ref: "#/$defs/right-claim-right"
        - right-power:
            $ref: "#/$defs/right-power"
        - right-immunity:
            $ref: "#/$defs/right-immunity"
        - right-liberty:
            $ref: "#/$defs/right-liberty"
    additionalItems: false

# ============================================================================
# Definitions
# ============================================================================
$defs:

  # Top-level right definition for a privilege.
  right-privilege:
    type: object
    description: >
      A privilege is something that someone is entitled to do.
    properties:
      privilege:
        type: string
      description:
        type: string
    additionalProperties: false
    required: [privilege, description]

  # Top-level right definition for a claim right.
  right-claim-right:
    type: object
    description: >
      A claim-right is a right the right holder has to a claim that another
      person do something or that they refrain from doing something.
    properties:
      id:
        type: string
      claim:
        $ref: "#/$defs/claim"
    required: [id, claim]

  # A claim is...
  claim:
    type: string
    description: >
      A claim specifies what a duty bearer must do or must refrain from doing.

  # Top-level right definition for a power.
  right-power:
    type: object
    description:
      A power allows the right holder to modify the rights of others.

  # Top-level right definition for an immunity.
  right-immunity:
    type: object
    description: >
      An immunity...

  # Top-level right definition for a liberty.
  right-liberty:
    type: object
    description: >
      A liberty is a combination of a privilege and a claim-right.
...

settings.json:

{
    "yaml.format.proseWrap": "always",
    "yaml.schemas": {
        "file:///Users/avoss/src/torshi-model/src/schemas/rightsdriven.yaml": "file:///Users/avoss/src/torshi-model/src/schemas/testrights.yaml"
    }
}

alexvoss avatar Sep 09 '23 22:09 alexvoss

I tried to narrow it down and it seems that the %YAML directive is the problem in my case. Removing it and reloading the window (Cmd-Shift-P on a Mac, then typing "reload" and selecting "Developer:Reload Window") solves the problem. Putting it back and reloading causes the problem to re-occur. This works with this minimal schema:

%YAML 1.1
---
title: Learning JSON/YAML schema 
type: object
properties:
  id: 
    type: string

The schema posted by the OP does not have a directive, so I do wonder what is going on there.

alexvoss avatar Sep 10 '23 10:09 alexvoss

Update: adding a comment before the document start also causes the error - but only if the comment contains a number? Add # Copyright (c) and it works, add # Copyright (c) 2003 and it does not. Funny thing, though, even moving the comment to the place after the document separator (which is now on line 1) makes it fail?! Witness this:

---
# a comment - that seems to work unless we add a number 123
title: Learning JSON/YAML schema 
type: object
properties:
  id: 
    type: string

Am I just demonstrating how little I know about YAML or is this broken - at the fundamental level of parsing the schema's YAML representation?

Update: it does not matter where in the schema code a comment contains a number, it makes the schema parsing fail. I guess this is an issue with the underlying yaml-language-server, so will try to replicate this using a different front-end and then post an issue there (after checking existing ones, which I have only screened a bit so far and I think I have not gone through closed ones, in case they need to be re-opened).

alexvoss avatar Sep 10 '23 10:09 alexvoss