stoplightio / spectral

A flexible JSON/YAML linter for creating automated style guides, with baked in support for OpenAPI (v3.1, v3.0, and v2.0), Arazzo v1.0, as well as AsyncAPI v2.x.
https://stoplight.io/spectral
Apache License 2.0
2.47k stars 233 forks source link

Spectral errors when using the UTF16-LE encoded .spectral.yaml file generated by following Readme instructions on Windows #2380

Open DawMatt opened 1 year ago

DawMatt commented 1 year ago

Describe the bug If you follow the readme instruction 1 (create a local ruleset) using Powershell on Windows, it generates a .spectral.yaml file using UTF16-LE encoding. Spectral reports Error running Spectral! when linting and using this file.

To Reproduce

  1. Open a Powershell terminal session on your Windows system
  2. Follow the usage instruction 1 (create a local ruleset) from the repository home page: echo 'extends: ["spectral:oas", "spectral:asyncapi"]' > .spectral.yaml
  3. Place an OpenAPI document in the same folder. It doesn't matter what document it is.
  4. Run this CLI command spectral lint someopenapifile.yaml --verbose, replacing someopenapifile.yaml with the name of the file selected in the previous step
  5. See error
spectral lint .\Clean-S146-043.yaml --verbose
Error running Spectral!
Error #1: Invalid ruleset provided
          at assertRuleset         …idation/index.js:14  throw new Error('In…
          at read                  …or/dist/index.js:22  (0, validation_1.as…
          at async migrateRuleset  …or/dist/index.js:33  const ruleset = awa…
          at async getRuleset      …ls/getRuleset.js:44  const migratedRules…
          at async lint            …linter/linter.js:16  const ruleset = awa…

Expected behavior Spectral would lint the OpenAPI using the ruleset created according to the usage instructions.

Screenshots Please refer to the error text above.

Environment (remove any that are not applicable):

Additional context This was discovered due to the same issue being present in the Visual Studio Code extension and the Spectral Documentation Ruleset.

There appears to be a superficial fix or a deeper fix available for this issue.

Superficial fix would be to update all of the readmes (including the additional repositories linked above) to make it clear a different approach is required for creating a local ruleset using Powershell. A command like this:

echo 'extends: ["https://unpkg.com/@stoplight/spectral-documentation@1.0.0/dist/ruleset.js"]' | Out-File .spectral.yaml -Encoding ascii

creates a configuration file using UTF8 and Spectral will lint using this.

A deeper fix would be to support additional file encodings, even if only by forcing them to UTF8 before processing.

@philsturgeon , I've create this new bug in the main spectral repository instead of reopening https://github.com/stoplightio/spectral-documentation/issues/3 as issue impacts multiple spectral repositories.

ravage84 commented 1 year ago

Described behavior & workaround confirmed with Spectral CLI version 6.6.0, installed via NPM 9.5.1 on Windows 10, Node.js v18.16.0

LasneF commented 1 year ago

i spend bunch of time to get the reason definitly your comment helps me a lot @ravage84 , So powerShell user : echo 'extends: ["spectral:oas"]' | Out-File .spectral.yaml -Encoding ascii