digitalbazaar / jsonld-cli

JSON-LD command line interface tool
Other
48 stars 7 forks source link

relative path to context does not work, only absolute paths work #15

Open aucampia opened 4 years ago

aucampia commented 4 years ago

If I specify a relative path to a context file I get a jsonld.InvalidUrl error

Steps to reproduce

cd /var/tmp/
curl https://json-ld.org/contexts/person.jsonld -o ./person.jsonld
echo '{ "http://schema.org/telephone": "1323" }' | npx jsonld compact -c ./person.jsonld -

Expected output

{
  "@context": "/var/tmp/person.jsonld",
  "telephone": "1323"
}

Actual output

jsonld.InvalidUrl: Dereferencing a URL did not result in a valid JSON-LD object. Possible causes are an inaccessible URL perhaps due to a same-origin policy (ensure the server uses CORS if you are using client-side JavaScript), too many redirects, a non-JSON response, or more than one HTTP Link Header was provided for a remote context.
    at /home/iwana/.npm-packages/lib/node_modules/jsonld-cli/node_modules/jsonld/lib/context.js:1188:15
Error: {
  "name": "jsonld.InvalidUrl",
  "details": {
    "code": "loading remote context failed",
    "url": "/person.jsonld",
    "cause": {
      "errno": -2,
      "code": "ENOENT",
      "syscall": "open",
      "path": "/person.jsonld"
    }
  }
}

Notes:

Following works fine:

url to context

echo '{ "http://schema.org/telephone": "1323" }' | npx jsonld compact -c https://json-ld.org/contexts/person.jsonld -

full path to context

curl https://json-ld.org/contexts/person.jsonld -o /var/tmp/person.jsonld
echo '{ "http://schema.org/telephone": "1323" }' | npx jsonld compact -c /var/tmp/person.jsonld -
vorburger commented 3 months ago

It seems to me that the problem isn't that "relative path to context does not work", but that its resolving relative paths based on the current working directory, instead of the basedir of the resource being processed. I was able to work around this (here) by using a cd, but it seems wrong. Alternatively, I was able to work around it (here) by using a --base= - but requiring that seems a bit redundant, to me. Perhaps this is related to #24.

@davidlehn @msporny FYI

davidlehn commented 3 months ago

Is this still an issue? I tried it and it worked but with the relative context in the output.

{
  "@context": "./person.jsonld",
  "telephone": "1323"
}