static-web-apps-cli icon indicating copy to clipboard operation
static-web-apps-cli copied to clipboard

Failed to load staticwebapp.config.json schema on start

Open StanSymphonia opened this issue 3 years ago • 71 comments

I'm using the latest version 1.1.4

Are you accessing the CLI from the default port :4280 ?

  • [ ] No, I am using a different port number (--port) and accessing the CLI from that port
  • [X] Yes, I am accessing the CLI from port :4280

Describe the bug When starting an app with swa start I get WARNING: Failed to load staticwebapp.config.json schema. Continuing without validation!. Even if it's just a warning and is referring to the schema file (not the config itself) staticwebapp.config.json doesn't work as expected. e.g. none of the routes declared is working.

To Reproduce Steps to reproduce the behavior:

  1. Go to any swa app
  2. Type in command 'swa start'
  3. See warning in console
  4. Check in browser any routes declared -> won't work.

Expected behavior staticwebapp.config.json to work. Routes and any other settings declared to work.

Desktop (please complete the following information):

  • OS: Windows
  • Version: 11

Additional context After investigating this issue i have discovered that the URL used to load the config schema is hardcoded: https://github.com/Azure/static-web-apps-cli/blob/affefb24872286a542fe7bbd54340648ec3294d0/src/core/utils/user-config.ts#L168C13-L168C13 . At this moment, the website https://www.schemastore.org/ which is hosting the schema file, is down (error 503). Allowing loading the schema file only from a single website has created a single point of failure which we are experiencing right now.

staticwebapp.config.json has a property $schema, but as you can see the value is not used for actually loading the schema. My suggestion is to allow the user to provide a value for $schema (in order to no longer depend on https://www.schemastore.org/ ).

Thank you.

Edit: there's currently an issue on Schemastore repository: https://github.com/SchemaStore/schemastore/issues/3303

StanSymphonia avatar Oct 11 '23 10:10 StanSymphonia

I followed @LiamOSullivan steps above but rather than putting in a new blob address I increased the time out

  • Go to %userprofile%\AppData\Roaming\npm\node_modules@azure\static-web-apps-cli\dist\core\utils (for Windows).
  • Open user-config.js.
  • Search for https://json.schemastore.org/staticwebapp.config.json.
  • Two line below increase the time out to 10000 milliseconds
  • Save and retry

ewardirl avatar Mar 01 '24 16:03 ewardirl

It's happening, right now.... AGAIN!!!!!

Please, reopen and FIX this.

LuKePicci avatar Mar 01 '24 17:03 LuKePicci

Temporary solution (what I did):

1. Go to `%userprofile%\AppData\Roaming\npm\node_modules\@azure\static-web-apps-cli\dist\core\utils` (for Windows).

2. Open `user-config.js`.

3. Search for `https://json.schemastore.org/staticwebapp.config.json`.

4. Upload `staticwebapp.config.json` to blob storage (for example) or use my file: `https://stcuberdonintegration.blob.core.windows.net/assets/staticwebapp.config.json` **(Will be removed in 24H)**

5. Replace `https://json.schemastore.org/staticwebapp.config.json` with your remote path.

Thank you for solution!

I was able to patch @azure/static-web-apps-cli for my deployment pipelines to use custom url instead of https://json.schemastore.org/staticwebapp.config.json.

patches/@azure+static-web-apps-cli+1.1.6.patch

diff --git a/node_modules/@azure/static-web-apps-cli/dist/core/utils/user-config.js b/node_modules/@azure/static-web-apps-cli/dist/core/utils/user-config.js
index 00861dd..0f69e81 100644
--- a/node_modules/@azure/static-web-apps-cli/dist/core/utils/user-config.js
+++ b/node_modules/@azure/static-web-apps-cli/dist/core/utils/user-config.js
@@ -149,7 +149,7 @@ function findLineAndColumnByPosition(content, position) {
     return notFound;
 }
 async function loadSWAConfigSchema() {
-    const schemaUrl = "https://json.schemastore.org/staticwebapp.config.json";
+    const schemaUrl = process.env.STATICWEBAPP_CONFIG_SCHEMA_JSON_URL || "https://json.schemastore.org/staticwebapp.config.json";
     try {
         const res = await (0, node_fetch_1.default)(schemaUrl, { timeout: 10 * 1000 });
         if (res.status === 200) {

package.json

{
  "scripts": {
    "postinstall": "patch-package"
  },
  "dependencies": {
    "@azure/static-web-apps-cli": "1.1.6",
    "patch-package": "^8.0.0",
    "postinstall-postinstall": "^2.1.0"
  }
}

.env

STATICWEBAPP_CONFIG_SCHEMA_JSON_URL=https://example.com/staticwebapp.config.schema.json

simkuns avatar Mar 01 '24 18:03 simkuns

@cjk7989 this should really be reopened, https://json.schemastore.org being down caused a monorepo with hundreds of devs to block all pull requests. We should be able to provide a local fallback.

jrestall avatar Mar 02 '24 08:03 jrestall

@cjk7989 Friday, I was able to bring my SWA up just fine locally for testing during development. Tonight, I ran everything with the exact same configuration and I get this error. Are there really no parameters I could supply to have it not reach out to a spotty third party?

emscook avatar Mar 04 '24 08:03 emscook

Currently swa fails to start locally because of this error:

 network timeout at: https://json.schemastore.org/staticwebapp.config.json
[swa] ✖ Failed to load staticwebapp.config.json schema. Continuing without validation!
...
✖ SWA emulator stopped because API server exited with code 1.

nerblock avatar Mar 04 '24 08:03 nerblock

Having a way of doing local validation is not the solution. We would still see default pipelines to fail each time someone decides to attack schemas tore.org

I think validation shloud be non-fatal at build time, it will be redone by swa api during publish. An optional parameter to force fatal errors on validation would be nice to have for some scenarios.

LuKePicci avatar Mar 04 '24 08:03 LuKePicci

Here's a simple fix that skips the validation (as the message says) instead of killing process. Some reordering of rows and missing schema returns config instead of null. File: node_modules/@azure/static-web-apps-cli/dist/core/utils/user-config.js

async function validateRuntimeConfigAndGetData(filepath) {
    const ajv4 = new ajv_draft_04_1.default({
        strict: false,
        allErrors: true,
    });    
    logger_1.logger.silly(`Reading content from staticwebapp.config.json...`);
    const content = (await readFile(filepath)).toString("utf-8");
    let config;
    try {
        logger_1.logger.silly(`Parsing staticwebapp.config.json...`);
        config = JSON.parse(content);
    }
    catch (err) {
        printJSONValidationWarnings(filepath, content, err.message);
        return null;
    }
    logger_1.logger.silly(`Loading staticwebapp.config.json schema...`);
    const schema = await loadSWAConfigSchema();
    if (!schema) {
        logger_1.logger.error(`Failed to load staticwebapp.config.json schema. Continuing without validation!`);
        return config;
    }
    logger_1.logger.silly(`Compiling schema...`);
    const validate = ajv4.compile(schema);
    logger_1.logger.silly(`Validating staticwebapp.config.json...`);
    const isValidSWAConfigFile = validate(config);
    if (!isValidSWAConfigFile) {
        printSchemaValidationWarnings(filepath, config, validate);
        return null;
    }
    logger_1.logger.silly(`File validated successfully. Continuing with configuration!`);
    return config;
}

TuukkaLaakso avatar Mar 04 '24 09:03 TuukkaLaakso

Thanks for the feedback. It’s an intermittent bug but occurs more common recently. We are going to have a local backup schema as a fallback option if the download fails. The fix will come soon.

cjk7989 avatar Mar 04 '24 09:03 cjk7989

Here's a simple fix that skips the validation (as the message says) instead of killing process. Some reordering of rows and missing schema returns config instead of null. File: node_modules/@azure/static-web-apps-cli/dist/core/utils/user-config.js

async function validateRuntimeConfigAndGetData(filepath) {
    const ajv4 = new ajv_draft_04_1.default({
        strict: false,
        allErrors: true,
    });    
    logger_1.logger.silly(`Reading content from staticwebapp.config.json...`);
    const content = (await readFile(filepath)).toString("utf-8");
    let config;
    try {
        logger_1.logger.silly(`Parsing staticwebapp.config.json...`);
        config = JSON.parse(content);
    }
    catch (err) {
        printJSONValidationWarnings(filepath, content, err.message);
        return null;
    }
    logger_1.logger.silly(`Loading staticwebapp.config.json schema...`);
    const schema = await loadSWAConfigSchema();
    if (!schema) {
        logger_1.logger.error(`Failed to load staticwebapp.config.json schema. Continuing without validation!`);
        return config;
    }
    logger_1.logger.silly(`Compiling schema...`);
    const validate = ajv4.compile(schema);
    logger_1.logger.silly(`Validating staticwebapp.config.json...`);
    const isValidSWAConfigFile = validate(config);
    if (!isValidSWAConfigFile) {
        printSchemaValidationWarnings(filepath, config, validate);
        return null;
    }
    logger_1.logger.silly(`File validated successfully. Continuing with configuration!`);
    return config;
}

Saved my teams morning productivity, thank you!

danbasszeti avatar Mar 04 '24 09:03 danbasszeti

Temporary solution (what I did):

  1. Go to %userprofile%\AppData\Roaming\npm\node_modules\@azure\static-web-apps-cli\dist\core\utils (for Windows).
  2. Open user-config.js.
  3. Search for https://json.schemastore.org/staticwebapp.config.json.
  4. Upload staticwebapp.config.json to blob storage (for example) or use my file: https://stcuberdonintegration.blob.core.windows.net/assets/staticwebapp.config.json (Will be removed in 24H)
  5. Replace https://json.schemastore.org/staticwebapp.config.json with your remote path.

Thank you. I have found another way to solve it. And it works without hosting the staticwebapp.config.json file So instead of simply replacing the URL, I increased the timeout to 5 minutes instead of just 10 seconds, as the only issue was I was able to load the file in browser but it would take more than 10 seconds.

Here's how you can do it:

  1. Go to user-config.js
  2. Find const schemaUrl = "https://json.schemastore.org/staticwebapp.config.json"; and below there's a line with timeout set to 10 * 1000
  3. Change the 10 to whatever number you think works
  4. Reload your terminal (it wasn't working without it)

PS. Finding user-config was tricky because it was installed globally and since I am using nvm mine was located at ~/.nvm/versions/node/v18.18.0/lib/node_modules/@azure/static-web-apps-cli/dist/core/utils/user-config.js

zainzafar90 avatar Mar 04 '24 10:03 zainzafar90

I disabled validation until this gets resolved:

    /*
    const schema = await loadSWAConfigSchema();
    if (!schema) {
        logger_1.logger.error(`Failed to load staticwebapp.config.json schema. Continuing without validation!`, true);
        return null;
    }
    logger_1.logger.silly(`Compiling schema...`);
    const validate = ajv4.compile(schema);
    */
    const validate = () => true;

nerblock avatar Mar 04 '24 10:03 nerblock

A quick way to use the CLI until this is resolved is to remove the "true" parameter to the logger.error method, as this causes the script to exit:

static-web-apps-cli/src/core/utils/user-config.ts

This: logger_1.logger.error(`Failed to load staticwebapp.config.json schema. Continuing without validation!`); instead of this: logger_1.logger.error(`Failed to load staticwebapp.config.json schema. Continuing without validation!`, true);

Credit to @cristianbretti

OlliejED avatar Mar 04 '24 10:03 OlliejED

I probably understood this problem. In loadSWAConfigSchema(), there is a process to load data from the following SchemaURL, right?

const schemaUrl = "https://json.schemastore.org/staticwebapp.config.json";

Currently, it seems that the data delivery for this URL is very slow. This is causing a timeout.

0xfab0131 avatar Mar 04 '24 10:03 0xfab0131

Additional information: This URL cannot be accessed.

image

0xfab0131 avatar Mar 04 '24 10:03 0xfab0131

In conclusion, it seems that this issue occurs at least during a period when the distribution of the above SchemaURL is unstable.

0xfab0131 avatar Mar 04 '24 10:03 0xfab0131

A quick way to use the CLI until this is resolved is to remove the "true" parameter to the logger.error method, as this causes the script to exit:

static-web-apps-cli/src/core/utils/user-config.ts

This: logger_1.logger.error(`Failed to load staticwebapp.config.json schema. Continuing without validation!`); instead of this: logger_1.logger.error(`Failed to load staticwebapp.config.json schema. Continuing without validation!`, true);

Credit to @cristianbretti

Removing the "true" from logger_1.logger.error to skip validation will cause the routing to not work. I recommend the two temporary solutions mentioned by @zainzafar90, to save the schema into Azure blob storage, or set the timeout to a larger value.

cjk7989 avatar Mar 04 '24 10:03 cjk7989

I think the following measures are necessary. Add processing that allows individuals to specify Schema for development by dividing the URL that distributes Schema into multiple parts or as an option.

Please have an image like the following.

swa start --schema "https://[alterbaseurl]/staticwebapp.config.json"

0xfab0131 avatar Mar 04 '24 10:03 0xfab0131

Can we skip accessing the network for the commands that does not require it? I am often find myself working on the plane with unstable internet connection. I don’t understand why do I need to download anything from the Internet if I just want to test the app. For deployment we may have additional arguments.

offbeatful avatar Mar 04 '24 10:03 offbeatful

Solved with workaround!!

After few hrs of struggle and understand I finally got it work by storing schema to different host and pointing updating the same in user-config.js. Thanks @cjk7989

Note: For mac os, path for file is: /usr/local/lib/node_modules/@azure/static-web-apps-cli/dist/core/utils/user-config.js. depends on node installation directory. change the schemaUrl in the file to your hosted schema path.

Those who need the content of the schema to host, I have a copy of it at: https://anuraggandhi.com/schema-swa.config.json. (please don't use it directly from this host. It will be removed shortly.)

softgandhi avatar Mar 04 '24 13:03 softgandhi