fix(ruleset-migrator): url loaded rulesets fail to resolve npm installed rulesets #2557
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Checklist
Does this PR introduce a breaking change?
Summary:
A remotely loaded ruleset will fail to load if it
extends
an npm/yarn installed ruleset such as:"@stoplight/spectral-owasp-ruleset"
"spectral-aws-apigateway-ruleset"
This is NOT an issue if the remote ruleset extends core rulesets "spectral:oas" and "spectral:asyncapi" because of the check here.
Cause:
In the
resolveModule()
method, thepath.isURL(ctx.filepath)
check will return true for a remote ruleset and not bother to check if the module/identifier is an npm installed module via therequireResolve()
method. This causes the npm installed module to wrongly resolve to a relative URL path and 404.Notes:
The
isPackageImport()
method will wrongly return true if my remote ruleset extends another ruleset in the same remote path. So, I'm testing the identifier if it is in fact installed viarequireResolve()
.We also can't assume all module names will be prefixed
@
, because it would fail to findspectral-aws-apigateway-ruleset
.Steps to reproduce:
npm install --save -D @stoplight/spectral-owasp-ruleset
spectral lint -r https://raw.githubusercontent.com/jquick-axway/spectral-test-rulesets/main/api-linting/owasp.yaml https://raw.githubusercontent.com/jquick-axway/spectral-test-rulesets/main/api-linting/tests/openapi-v3.json
@stoplight/spectral-owasp-ruleset
is resolving to a URL instead of the npm locally installed module.Could not load https://raw.githubusercontent.com/jquick-axway/spectral-test-rulesets/main/api-linting/@stoplight/spectral-owasp-ruleset (imported by https://raw.githubusercontent.com/jquick-axway/spectral-test-rulesets/main/api-linting/.spectral.js): Error fetching https://raw.githubusercontent.com/jquick-axway/spectral-test-rulesets/main/api-linting/@stoplight/spectral-owasp-ruleset: Not Found