stackblitz / webcontainer-core

Dev environments. In your web app.
https://webcontainers.io
MIT License
3.9k stars 165 forks source link

Error trying to define YAML Schema: Unable to load schema from 'http://json-schema.org/draft-06/schema': socket hang up #1309

Open marcelopbarros opened 9 months ago

marcelopbarros commented 9 months ago

Describe the bug

When creating a YAML Schema usually we start with something like that in a _schema.json file:

{
    "$schema": "http://json-schema.org/draft-06/schema#"
}

But when writing on StackBlitz I got that error:

Unable to load schema from 'http://json-schema.org/draft-06/schema': socket hang up.

Link to the blitz that caused the error

https://stackblitz.com/~/github.com/marcelopbarros/mgmgoiania

Steps to reproduce

Open the file on src/content/pages/_schema.json that starts with "$schema": "http://json-schema.org/draft-06/schema#".

Expected behavior

No errors or warnings

Parity with Local

Screenshots

image

Platform

Version = 1.84.0
Hash = 5b71eec7e7e29934191def5462461888ab204c32
WebContainer = 6854296d

Browser name  = Chrome
Full version  = 120.0.0.0
Major version = 120
navigator.appName = Netscape
navigator.userAgent = Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
performance.memory = {
  "totalJSHeapSize": 85881438,
  "usedJSHeapSize": 79821738,
  "jsHeapSizeLimit": 4294705152
}

Additional context

When using local the error doesn't appear. I believe the URL is somewhat blocked when running on StackBlitz.

jrvidal commented 9 months ago

@marcelopbarros :wave: Thanks for the report. I can't access the repo you've linked.

In any case, the most likely case for the error is a CORS error when trying to fetch that schema URL.

marcelopbarros commented 9 months ago

@marcelopbarros 👋 Thanks for the report. I can't access the repo you've linked.

To make things a little easier I created a new repo with a minimal viable example. This one is public:

https://github.com/marcelopbarros/schema-yaml-test

The warning still persists in StackBlitz (and it works locally).

image

But using this new repo I realized the schema still works, even with the warning. (In my personal project I had to disable and enable the YAML extension to make everything working again).

image

At the beginning I was assuming the reason the schema wasn't working it was because of the warning. But now, that I see things works even with the warning, I think you are free to judge if it still worth to look at the root of the socket hang up problem (if it's really a CORS error). Feel free to close the issue if you think the warning is ok.

Thank you for your time (and apologies for any inconvenience 🙃)

jrvidal commented 9 months ago

It is indeed a network related problem. From your second screenshot, I don't think it is actually working. It should give you errors if, for instance, you don't have a label, or you define it to be a numeric value.

If you change the schema URL to be https, everything should work. This is a hard limitation of WebContainer: we can't magically bypass network restrictions from the browser i.e. mixing http and https content in certain contexts.

Can you confirm changing the URL works for you?

marcelopbarros commented 9 months ago

First, I tried testing without any changes, to confirm that the schema is being loaded even with the socket hang up warning. As you commented, changing label to a numeric value generates an error. So, I believe the schema is being loaded. The definition also appears at the bottom of the screen, next to the keyboard layout: pages.

image

Also, as you suggested, changing the URL to https gets rid of the warning:

image

Finally, it's worth mention that the YAML Extension has a "yaml.schemaStore.url" config, which by default (according to this) points to "https://www.schemastore.org/api/json/catalog.json". This catalog seems to have some JSON Schema (all using https). I believe that's the reason the validation still works even with the warning.

image