Skip to content

side effects in API routes breaks Greenwood build #64

@thescientist13

Description

@thescientist13

Related to #59

Currently, if the DB environment variables are not loaded (e.g. node --env-file=.env ) and a Greenwood build is run, the build will break on trying to instantiate the SQL client

➜  api git:(effect) ✗ npm run build

> api.analogstudios.net@0.3.1 build
> greenwood build

-------------------------------------------------------
Welcome to Greenwood (v0.33.0) ♻️
-------------------------------------------------------
Running Greenwood with the build command.
Initializing project config
Initializing project workspace contexts
Generating graph of workspace files...
building from local sources...
LibsqlError: URL_INVALID: The URL '' is not in a valid format
    at parseUri (file:///Users/owenbuckley/Workspace/analogstudiosri/api/node_modules/@libsql/core/lib-esm/uri.js:9:15)
    at expandConfig (file:///Users/owenbuckley/Workspace/analogstudiosri/api/node_modules/@libsql/core/lib-esm/config.js:25:17)
    at createClient (file:///Users/owenbuckley/Workspace/analogstudiosri/api/node_modules/@libsql/client/lib-esm/web.js:8:26)
    at file:///Users/owenbuckley/Workspace/analogstudiosri/api/src/client/db.ts:7:16
    at ModuleJob.run (node:internal/modules/esm/module_job:343:25)
    at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:647:26)
    at async walkDirectoryForPages (file:///Users/owenbuckley/Workspace/analogstudiosri/api/node_modules/@greenwood/cli/src/lifecycles/graph.js:106:33)
    at async walkDirectoryForPages (file:///Users/owenbuckley/Workspace/analogstudiosri/api/node_modules/@greenwood/cli/src/lifecycles/graph.js:76:27)
    at async generateGraph (file:///Users/owenbuckley/Workspace/analogstudiosri/api/node_modules/@greenwood/cli/src/lifecycles/graph.js:302:9)
    at async generateCompilation (file:///Users/owenbuckley/Workspace/analogstudiosri/api/node_modules/@greenwood/cli/src/lifecycles/compile.js:113:19) {
  code: 'URL_INVALID',
  rawCode: undefined,
  [cause]: undefined
}

To avoid this for now (since this shouldn't be an issue just for building), it was required to wrap the instantiation

let client: ReturnType<typeof createClient>;

if(process.env.DATABASE_URL === undefined || process.env.DATABASE_TOKEN === undefined) {
  console.warn('DATABASE_URL and DATABASE_TOKEN must both be defined');
} else {
  client = createClient({
    url: process.env.DATABASE_URL,
    authToken: process.env.DATABASE_TOKEN
  });
}

This is related to a Greenwood issue where API routes are currently imported, to get access to the isolation config value
https://github.com/ProjectEvergreen/greenwood/blob/v0.34.0-alpha.4/packages/cli/src/lifecycles/graph.js#L112

Would be nice if this could "resistant" to side effects, like was done for SSR pages - ProjectEvergreen/greenwood#991

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions