question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Default Resolvers Not Resolving Relative to Given Document

See original GitHub issue

Describe the bug The default resolvers don’t function as documented.

By default, http(s) and file protocols are resolved, relatively to the document Spectral lints against. [Emphasis mine]

However, when linting with a new Spectral() object, the file $refs are resolved relative to where the script runs, not the file being linted.

To Reproduce

C:/api/openapi.yaml:

openapi: 3.0.3
info:
  title: Test API
  version: 1.0.0
paths:
  /test:
    get:
      responses:
        '200':
          $ref: components.yaml#/Ok

C:/api/components.yaml:

Ok:
  description: Success

C:/dev/script.ts:

import fs from 'fs';
import YAML from 'js-yaml';
import { isOpenApiv3, Spectral } from '@stoplight/spectral';
import { DiagnosticSeverity } from '@stoplight/types';

const openApi = fs.readFileSync('C:/api/openapi.yaml', 'utf-8');

const spectral = new Spectral();
spectral.registerFormat('oas3', isOpenApiv3);
spectral.run(YAML.safeLoad(openApi) as string)
    .then(results => {
        results.filter(result => result.severity === DiagnosticSeverity.Error)
            .forEach(result => console.log(result.message))
    });
$ ts-node C:\dev\script.ts
ENOENT: no such file or directory, open 'C:\dev\components.yaml'

Expected behavior Given the OpenAPI document and script above, there should be no errors and therefore no output. Spectral’s default resolvers should be looking for C:/api/components.yaml, not C:/dev/components.yaml.

Environment:

  • TypeScript: v3.9.6
  • Spectral: v5.5.0
  • Node: v12.15.0
  • Windows: 10 Enterprise

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
nulltokencommented, Sep 10, 2020

That’s not a bad idea! Maybe between the “Linting an Object” and “Registering Formats” sections?

@dillonredding That would be great location.

Just want to clarify, by “warning”, you mean like the blockquote here (with the theme: warning comment)?

Indeed. This would bring some attention from the reader 😉

0reactions
dillonreddingcommented, Sep 10, 2020

Additionally, maybe could you add a new section dedicated to linting files where those absolute/relative path concerns would absolutely shine!

That’s not a bad idea! Maybe between the “Linting an Object” and “Registering Formats” sections?

Or maybe just warnings regarding that the provided documents should be complete standalone ones (or that the resolution for relative paths would occur according to the current working directory (which location is quite unpredictable)).

Just want to clarify, by “warning”, you mean like the blockquote here (with the theme: warning comment)? I could definitely see that being helpful in “Linting a YAML String” and “Linting an Object” sections and just point to the new section.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Resolvers - Apollo GraphQL Docs
This object is called the resolver map. The resolver map has top-level fields that correspond to your schema's types (such as Query above)....
Read more >
Parcel 2: Specific resolvers per file type · Issue #3492 - GitHub
The problem is script and link elements in the browser resolve bare specifiers (without './ or / prefixes) as relative paths, not as...
Read more >
Resolver - Parcel
Resolvers run in a pipeline until one of them returns a result. See Dependency resolution for details on how the default resolver works....
Read more >
Using eslint with typescript - Unable to resolve path to module
In my eslintrc.js config file, the "import/resolver" object needed to sit within the "rules" node, not the " ...
Read more >
The dns.resolver.Resolver and dns.resolver.Answer Classes
If the query name is a relative name, the resolver will construct ... An int , the default DNS port to send to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found