Skip to content

Passing JSON in env variables is broken #2846

@infomiho

Description

@infomiho

The way we parse and output the values in .env.server is broken. It prevents users from customizing the PgBoss options via the PG_BOSS_NEW_OPTIONS env variables. Based on this user report.

Reproduction

  1. You'll need an app that has jobs defined.
  2. Add this to .env.server:
    PG_BOSS_NEW_OPTIONS='{"teamConcurrency":3}'
    
  3. You'll get this error:

    [ Server!] Environment variable PG_BOSS_NEW_OPTIONS was not parsable by JSON.parse()!

Investigation

Looking into the .wasp/out/server/.env file, we see this:

PG_BOSS_NEW_OPTIONS="{\"teamConcurrency\":3}"

This looks fine, it's an string that's escaped.

Adding extra logging to the .wasp/out/sdk/wasp/dist/server/jobs/core/pgBoss/pgBoss.js file, gets us extra info:

[ Server!] SyntaxError: Expected property name or '}' in JSON at position 1 (line 1 column 2)
[ Server!]     at JSON.parse (<anonymous>)
[ Server!]     at createPgBoss (/Users/ilakovac/dev/wasp/waspc/examples/todoApp/.wasp/out/sdk/wasp/dist/server/jobs/core/pgBoss/pgBoss.js:11:37)
[ Server!]     at <anonymous> (/Users/ilakovac/dev/wasp/waspc/examples/todoApp/.wasp/out/sdk/wasp/dist/server/jobs/core/pgBoss/pgBoss.js:3:14)
[ Server!]     at ModuleJob.run (node:internal/modules/esm/module_job:271:25)
[ Server!]     at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:578:26)
[ Server!]     at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:116:5)

Logging the value of the env variable, it looks like env variables are not unescaped before we try to use them in JSON.parse():

[ Server!] {\"teamConcurrency\":3}

Potential fix

We should probably look into how to unescape the values that we load from process.env or we should consider using single quotes for strings that might need to be escaped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions