Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix production build broken by missing env DATABASE_URL at build time #41

Merged
merged 1 commit into from
Jul 22, 2024

Conversation

andresgutgon
Copy link
Contributor

@andresgutgon andresgutgon commented Jul 16, 2024

What?

We moved DB initialization to core: #34 This breaks production build because at build time DATABASE_URL is not and should never be defined

TODO

  • QA the build works

@andresgutgon andresgutgon added the 🚧 wip Work in progress label Jul 16, 2024
node_modules
npm-debug.log
.pnpm-store
out
dist
*.md
.git
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were inserting a lot of shit in the image.

@@ -17,38 +18,37 @@ RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install \
--frozen-lockfile \
--filter "@latitude-data/web..."

# PRODUCTION stage
# ------------------------------------------------------
FROM installer AS builder
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builder and installer can be merged I think.

@andresgutgon andresgutgon force-pushed the fix/security-issue-with-dockerfile-production branch from f0552b4 to ecfce74 Compare July 18, 2024 18:31
dockerfile: apps/web/docker/Dockerfile
# This enables to have this service for testing production build
# It will not run when doing docker compose up
profiles: [building]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker compose allow to have profiles. So no profile runs when docker compose up which is perfect for this case. I only want for the combinience of developing Dockerfile production and run docker compose build web

@@ -24,6 +24,7 @@
},
"dependencies": {
"@latitude-data/env": "workspace:^",
"@t3-oss/env-core": "^0.10.1",
Copy link
Contributor Author

@andresgutgon andresgutgon Jul 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the perfect excuse for this sorry. Explained in the next comment.

Copy link
Collaborator

@geclos geclos Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plz... if (process.env.BUILDING_CONTAINER == 'true') return let's not fight more over this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why you don't want a tool built for this propose

Copy link
Collaborator

@geclos geclos Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like it for NextJS for the client-side safety measures that you mention ☝🏼 . I hate that we use it in api because it brings 0 value there, and adding a dependency is just overhead.

Copy link
Contributor Author

@andresgutgon andresgutgon Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this package is type save. When you do env.SOMETHING you're sure that's a number, string, enum,... Doing the old way you still use untyped process.env


import '@latitude-data/env'

const envvars = z.object({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing this breaks production build because it tries to find DATABASE_URL at build time. With t3-env this is easy fixable by using skipValidation with BUILDING_CONTAINER flag as we did in web.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? I don't get it why is this code evaluated at build time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's imported by nextjs bundler. And because is imported zod parse function is run

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next bundler runs the code of its dependencies at compile time? :drunk:

@andresgutgon andresgutgon force-pushed the fix/security-issue-with-dockerfile-production branch from c8999f0 to d04f5c4 Compare July 19, 2024 08:48
@@ -1,6 +1,7 @@
{
"extends": "@latitude-data/typescript-config/base.json",
"compilerOptions": {
"moduleResolution": "Bundler",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'bundler' for use with bundlers. Like node16 and nodenext, this mode supports package.json "imports" and "exports", but unlike the Node.js resolution modes, bundler never requires file extensions on relative paths in imports.

Reference

I think Bundler is ok

Copy link
Collaborator

@geclos geclos Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did u need this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For t3-env/core. It use a module system that requires the more modern version. But the thing is we're already using Bundler in NextJS app.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An I think bundler makes a lot of sense for us because we're using one only final bundler in nextjs or api and not having by package bundlers

RUN --mount=type=cache,id=pnpm,target=/pnpm/store \
BUILDING_CONTAINER=true \
pnpm turbo build --filter='@latitude-data/web...'

FROM base AS runner
FROM base as prod-deps
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is worth it to have a stage only to install production deps. That then are copied in production stage

@andresgutgon andresgutgon removed the 🚧 wip Work in progress label Jul 19, 2024
@andresgutgon andresgutgon changed the title Fix/security issue with dockerfile production Fix production build broken by missing env DATABASE_URL at build time Jul 19, 2024
@andresgutgon andresgutgon requested a review from geclos July 19, 2024 09:04
We moved DB initialization to core:
#34
This breaks production build because at build time DATABASE_URL is not
and should never be defined
@andresgutgon andresgutgon force-pushed the fix/security-issue-with-dockerfile-production branch from d04f5c4 to 5dce8ba Compare July 22, 2024 07:35
@andresgutgon andresgutgon merged commit 492b8a8 into main Jul 22, 2024
2 checks passed
@andresgutgon andresgutgon deleted the fix/security-issue-with-dockerfile-production branch July 22, 2024 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants