jakeg / heroku-buildpack-bun

Heroku buildpack for Bun.js
MIT License
19 stars 20 forks source link

Env problem during pre-build step #9

Closed ludersGabriel closed 1 week ago

ludersGabriel commented 1 month ago

Hey! Are env variables not available during the heroku-prebuild step?

Even though they are there after the app goes live (i can see tem with printenv), when running the prebuild im getting a validation error cause they arent there.

This is my env configuration file with zod:

import { config } from 'dotenv'
import { expand } from 'dotenv-expand'

import { ZodError, z } from 'zod'

const stringBoolean = z.coerce
  .string()
  .transform((val) => {
    return val === 'true'
  })
  .default('false')

const EnvSchema = z.object({
  NODE_ENV: z.string().default('development'),
  DATABASE_URL: z.string(),
  DB_MIGRATING: stringBoolean,
  DB_SEEDING: stringBoolean,
  APP_SECRET: z.string(),
})

export type EnvSchema = z.infer<typeof EnvSchema>

expand(config())

try {
  EnvSchema.parse(process.env)
} catch (error) {
  if (error instanceof ZodError) {
    let message = 'Missing required values in .env:\n'
    error.issues.forEach((issue) => {
      message += issue.path[0] + '\n'
    })

    const e = new Error(message)
    console.log(process.env)

    e.stack = ''
    throw e
  } else {
    console.log(error)
  }
}

export default EnvSchema.parse(process.env)

Very straight forward and works fine when the start script runs. Just the build that fails, I cant migrate and seed my db because of it. Here is my package.json with the commands:

{
  "name": "hono-backend",
  "scripts": {
    "dev": "bun run --watch src/index.ts",
    "db:generate": "drizzle-kit generate",
    "db:migrate": "cross-env DB_MIGRATING=true bun run src/db/migrate.ts",
    "db:seed": "cross-env DB_SEEDING=true bun run src/db/seed.ts",
    "db:studio": "drizzle-kit studio",
    "route:test": "bun db:seed && bun test",
    "heroku-prebuild": "bun db:migrate && bun db:seed",
    "build": "bun --target bun build ./src/index.ts --outdir ./build",
    "start": "bun build/index.js"
  },
  "dependencies": {
    "@hono/zod-validator": "^0.2.2",
    "dotenv": "^16.4.5",
    "dotenv-expand": "^11.0.6",
    "drizzle-orm": "^0.31.2",
    "drizzle-zod": "^0.5.1",
    "hono": "^4.4.8",
    "postgres": "^3.4.4",
    "reflect-metadata": "^0.2.2",
    "typedi": "^0.10.0",
    "zod": "^3.23.8"
  },
  "devDependencies": {
    "@eslint/js": "^9.5.0",
    "@types/bun": "latest",
    "cross-env": "^7.0.3",
    "drizzle-kit": "^0.22.7",
    "eslint": "9.x",
    "eslint-config-prettier": "^9.1.0",
    "globals": "^15.6.0",
    "prettier": "^3.3.2",
    "typescript-eslint": "^7.14.1"
  }
}

The error I get is that DATABASE_URL and APP_SECRET are missing during the prebuild step. But they are in the console and show up with printenv once the app starts

jakeg commented 1 month ago

No, Heroku don't provide them. Instead, you need to manually read them from disks in files, as per in ./bin/compile

jakeg commented 1 month ago

Ok, so there's actually a way I can export those env vars as per the docs. Try again now... does it work?

ludersGabriel commented 1 month ago

Hey! Thanks for replying. Couldn't try it yesterday, ended up using a procfile with the generate and seed on the release command. When I have the opportunity all try it out without it again!

jakeg commented 1 month ago

Have a chance to try it out yet?

jakeg commented 1 week ago

I'm going to assume this is fixed, but re open if not.