ssube / salty-dog

Rule-based JSON/YAML validator using JSON schemas
https://ssube.github.io/salty-dog/
MIT License
12 stars 4 forks source link

v0.9 image fails trying to load schema from current directory #1023

Open ssube opened 2 years ago

ssube commented 2 years ago

Bug

Something is broken!

Summary

Error loading schema to validate config since v0.9 image was published.

Steps to Reproduce

Actual Behavior

uncaught error during main: Error: ENOENT: no such file or directory, open '/build/src/git.company.com/sKSMtss1/1/x/y/z/rules/salty-dog.yml'
    at Object.openSync (node:fs:585:3)
    at readFileSync (node:fs:453:35)
    at loadSchema (/usr/local/share/.config/yarn/global/node_modules/salty-dog/out/bundle/index.cjs:33664:44)
    at validateConfig (/usr/local/share/.config/yarn/global/node_modules/salty-dog/out/bundle/index.cjs:33687:33)
    at main (/usr/local/share/.config/yarn/global/node_modules/salty-dog/out/bundle/index.cjs:33775:8) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/build/src/git.company.com/sKSMtss1/1/x/y/z/rules/salty-dog.yml'
}

Expected Behavior

Should load the config from /salty-dog/rules/salty-dog.yml (or ../../rules, relative to the entrypoint script), whether that is /salty-dog/out/bundle/index.cjs or /salty-dog/out/src/index.js.

ssube commented 2 years ago

This is being caused by esbuild, and there is a warning during bundling:

node config/esbuild.mjs
▲ [WARNING] "import.meta" is not available in the configured target environment ("es2017") and will be empty
    out/src/config/index.js:11:18:
      11 │     if (doesExist(import.meta) && doesExist(import.meta.url)) {
         ╵                   ~~~~~~~~~~~
  The target environment was set to "es2017" here:
    config/tsconfig.json:25:14:
      25 │     "target": "es2017",
         ╵               ~~~~~~~~
▲ [WARNING] "import.meta" is not available in the configured target environment ("es2017") and will be empty
    out/src/config/index.js:11:44:
      11 │     if (doesExist(import.meta) && doesExist(import.meta.url)) {
         ╵                                             ~~~~~~~~~~~
  The target environment was set to "es2017" here:
    config/tsconfig.json:25:14:
      25 │     "target": "es2017",
         ╵               ~~~~~~~~
▲ [WARNING] "import.meta" is not available in the configured target environment ("es2017") and will be empty
    out/src/config/index.js:12:42:
      12 │ ...n join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
         ╵                                 ~~~~~~~~~~~
  The target environment was set to "es2017" here:
    config/tsconfig.json:25:14:
      25 │     "target": "es2017",
         ╵               ~~~~~~~~

(https://git.apextoaster.com/ssube/salty-dog/-/jobs/385796)

The code falls back to process.cwd():

/**
 * Path to project root directory.
 */
export function dirName(): string {
  if (doesExist(import.meta) && doesExist(import.meta.url)) {
    return join(dirname(fileURLToPath(import.meta.url)), '..', '..', '..');
  } else {
    return process.cwd();
  }
}

(https://github.com/ssube/salty-dog/blob/master/src/config/index.ts#L29)

ssube commented 2 years ago

The first case, running the container itself, is fixed. Running the global command from within the container, is not:

dirName called [
  '/usr/local/bin/node',
  '/usr/local/bin/salty-dog',
  'check',
  '--config-path',
  '/salty-dog/docs'
]
uncaught error during main: Error: ENOENT: no such file or directory, open '/usr/rules/salty-dog.yml'
    at Object.openSync (node:fs:585:3)
    at readFileSync (node:fs:453:35)
    at loadSchema (/usr/local/share/.config/yarn/global/node_modules/salty-dog/out/bundle/index.cjs:33805:44)
    at validateConfig (/usr/local/share/.config/yarn/global/node_modules/salty-dog/out/bundle/index.cjs:33828:33)
    at main (/usr/local/share/.config/yarn/global/node_modules/salty-dog/out/bundle/index.cjs:33916:8) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/usr/rules/salty-dog.yml'
}

The usual logic for finding rules/ does not apply to the /usr/local/bin/salty-dog binary/command path.