R
Railway17mo ago
OneEye

Error loading .env file on golang project, when using turborepo

so I have a Turbo repo project and in the app directory I have a golang project I've changed the build and run script to go build main.go and ./bin/main everything work including the building and starting the server, the probelem as you can see is the absence of .env of course .env is mentioned in the .gitignored file so it's not uploaded but I've added it in the railway UI
Solution:
okay so in summerty, the problem was 1. only load the env files when you're locally developing or just use railway shell to have railway env inside your local 2. don't add extra build and start scripts, railway will take care of it JUST specify the root path where your service exists PS: use railway to manage your local enviorment variables...
Jump to solution
152 Replies
Percy
Percy17mo ago
Project ID: d1e05c2d-ef92-4ac2-8a0a-8505878d0103
OneEye
OneEyeOP17mo ago
d1e05c2d-ef92-4ac2-8a0a-8505878d0103
OneEye
OneEyeOP17mo ago
maybe because the monorepo contains the front-end as well but I've changed the root directory as well but stillno dice
Brody
Brody17mo ago
adding services variables does not create a .env file in your app, it injects environment variables directly into the environment, on railway you do not need to load from a .env file because they are already in the environment, all you need to do is os.Getenv("DB_HOST")
OneEye
OneEyeOP17mo ago
ou so, I don't need to have an env.load function? okay okay I got it do you have any idea on how can I check if I'm in the deployment?
Brody
Brody17mo ago
you can have it in your code for local use, just don't log.fatal on the error value returned by the env.load func
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
^
OneEye
OneEyeOP17mo ago
ooooo okay yeye so the problem is the log.Fetal okay understood thank you very much @Brody let me still check and I'll report back
Brody
Brody17mo ago
in fact you could remove your local .env file, and all the .env load code, and just keep your variables in the railway service, then use the railway cli with railway shell that command pulls the service variables from railway and injects them into a temporary local shell for your local app development
OneEye
OneEyeOP17mo ago
yeye, I'm using envvalut to manage .env files https://vault.dotenv.org/
Dotenv
Log In
Simplify your secrets
Brody
Brody17mo ago
well with railways service variables and the cli there's no need for that either!
OneEye
OneEyeOP17mo ago
hmmmm, I'll look in to it, because I really love this railways approach of "every service being in one place"
Brody
Brody17mo ago
definitely do, it's such a nice dev experience with service variables and the railway shell
OneEye
OneEyeOP17mo ago
okay despite the fact that I changed "log.fatal" to just "fmt.Println" it still has this error
OneEye
OneEyeOP17mo ago
OneEye
OneEyeOP17mo ago
and I've changed that error text as well but still no dice
OneEye
OneEyeOP17mo ago
OneEye
OneEyeOP17mo ago
this should just print it our right? no reason for crashing the app
Brody
Brody17mo ago
and railway did do a new build?
OneEye
OneEyeOP17mo ago
ye
OneEye
OneEyeOP17mo ago
and I even did this
OneEye
OneEyeOP17mo ago
and added this to railway
OneEye
OneEyeOP17mo ago
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
then you are telling your code to load a .env file when on railway? that's specifically what I said you didn't need to do lol
OneEye
OneEyeOP17mo ago
okay..... I'm giga dumb let me fix that :DDD sorry for the false positive
Brody
Brody17mo ago
I think it's time to completely move away from .env files
OneEye
OneEyeOP17mo ago
based
OneEye
OneEyeOP17mo ago
I'm going to burn my pants
Brody
Brody17mo ago
well get going on this
OneEye
OneEyeOP17mo ago
I changed the code to this
OneEye
OneEyeOP17mo ago
ye but I still need it in the local development
Brody
Brody17mo ago
no you dont
OneEye
OneEyeOP17mo ago
really?
Brody
Brody17mo ago
read message again
OneEye
OneEyeOP17mo ago
oooo okay can you link me the docs for this ? if it's not a hastle of course I don't want to bother any more then I have :DD
Brody
Brody17mo ago
OneEye
OneEyeOP17mo ago
I'm still baffled why this doesn't work
OneEye
OneEyeOP17mo ago
but I digress, I'll continue tinkering around thank you for guiding me @Brody really appreciate it
Brody
Brody17mo ago
okay well now or just seems like railway is running old revisions of your code are you deploying from the correct repo and branch? try setting NIXPACKS_CONFIG_FILE = 1 in the service variables
OneEye
OneEyeOP17mo ago
okay let me do that and also yes, it just attached to github project but I'll also try creating a new project as well
Brody
Brody17mo ago
try the variable before the new project
OneEye
OneEyeOP17mo ago
yeye
Brody
Brody17mo ago
and you'd only need a new service, cache is done per service id and no two services will never have the same id
OneEye
OneEyeOP17mo ago
* sad trumpet sounds *
Brody
Brody17mo ago
wat
OneEye
OneEyeOP17mo ago
I'm going to move this from the mono repo to a seperate repo
Brody
Brody17mo ago
no no monorepo is fine, slow your horses
OneEye
OneEyeOP17mo ago
:DDDD sure sure let me create a new project then also do you reckon this error is from railway it self? and not the log.Fatal
Brody
Brody17mo ago
it's from railway but it's caused by something you did show me a screenshot of the service variables please
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
do you have a nixpacks.toml file in your project
OneEye
OneEyeOP17mo ago
nono I'm using turborepo but it's detecting Nix for some reason :DDD
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
nixpacks is the builder, that's normal delete the nixpacks config file service variable
OneEye
OneEyeOP17mo ago
kk or should I give it a 0 value?
Brody
Brody17mo ago
delete it
OneEye
OneEyeOP17mo ago
obliterated it
Brody
Brody17mo ago
have you set this
OneEye
OneEyeOP17mo ago
yeye, and it did this crash
Brody
Brody17mo ago
screenshot of service variables again please
OneEye
OneEyeOP17mo ago
sure
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
omfg I'm sorry
OneEye
OneEyeOP17mo ago
nono don't apologize Brody :DDD you're the one taking your time and helping me
Brody
Brody17mo ago
the variable you want to set to 1 is NIXPACKS_NO_CACHE I copied wrong the first time and didn't even release it till just now
OneEye
OneEyeOP17mo ago
ou okay let me try that also sorry for taking several minutes before replying
Brody
Brody17mo ago
no worries
OneEye
OneEyeOP17mo ago
OneEye
OneEyeOP17mo ago
let me create another service
OneEye
OneEyeOP17mo ago
also on the deployment I completely commented this out
Brody
Brody17mo ago
odd
OneEye
OneEyeOP17mo ago
very can this be the problem btw?
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
no that looks good to me, thats the folder with your go code
OneEye
OneEyeOP17mo ago
ye ye
OneEye
OneEyeOP17mo ago
creted a clean service but still this
Brody
Brody17mo ago
then its not cache
OneEye
OneEyeOP17mo ago
ye
Brody
Brody17mo ago
can you share your repo
OneEye
OneEyeOP17mo ago
okay give me a sec let mech check if there's nothing sencitive there :DDD
Brody
Brody17mo ago
also where is your database hosted
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
the same project?
OneEye
OneEyeOP17mo ago
ye
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
please delete all your database variables and replace them with variable references https://docs.railway.app/develop/variables#reference-variables
OneEye
OneEyeOP17mo ago
ooo okay give me asec
Brody
Brody17mo ago
screenshot of service variables after thats done please
OneEye
OneEyeOP17mo ago
also if you wat we can cal and I can share my screen eve of course if it's not a problem
Brody
Brody17mo ago
cal?
OneEye
OneEyeOP17mo ago
call in like going to a VC
Brody
Brody17mo ago
oh, i dont even have a mic
OneEye
OneEyeOP17mo ago
I just kinda don't want to be bother Brody so if you're not okay with that I completely understand also it's okay if you don't have a mic, at least you'll see everything live so you can asses the situation also did the variable replacement with
DATABASE_URL=${{Postgres.DATABASE_URL}}
DATABASE_URL=${{Postgres.DATABASE_URL}}
Brody
Brody17mo ago
easiest if i can see the repo
OneEye
OneEyeOP17mo ago
sorry Brody I can't do that at this moment
Brody
Brody17mo ago
no worries sus repo, i understand
OneEye
OneEyeOP17mo ago
ye :DDD
Brody
Brody17mo ago
youtube downloader 😉
OneEye
OneEyeOP17mo ago
no it's not sus per say I'm just anxios, you know :DD
OneEye
OneEyeOP17mo ago
but for the death of me I can't understand the only god damn place this env could be loaded is here and it's commented out
Brody
Brody17mo ago
are you loading a .env file somwhere else in your code?
OneEye
OneEyeOP17mo ago
and this is where I connect to the db
OneEye
OneEyeOP17mo ago
nop nop that's the only place I load the file it self other then that I just use it in different places
Brody
Brody17mo ago
chuck a func init() { fmt.Println("test") } in your main.go file to see if it prints on railway
OneEye
OneEyeOP17mo ago
sure I'll strain up delete everything from main and just have single print out there
Brody
Brody17mo ago
slow your horses
OneEye
OneEyeOP17mo ago
🏇 🏇 🏇 🏇 🏇 🏇 🏇 🏇
OneEye
OneEyeOP17mo ago
AAAAAAAAAAAAAAAAA
OneEye
OneEyeOP17mo ago
:DDD why are the programming gods laughing at me :DDD
Brody
Brody17mo ago
and this is a new service?
OneEye
OneEyeOP17mo ago
yes, which has the exact same stuff build command, start command, root path and watch path
Brody
Brody17mo ago
show me a screenshot of your github repo open to this go project
OneEye
OneEyeOP17mo ago
sure thing
OneEye
OneEyeOP17mo ago
OneEye
OneEyeOP17mo ago
here'a closer look
Brody
Brody17mo ago
whats in the bin folder
OneEye
OneEyeOP17mo ago
the built main.go
OneEye
OneEyeOP17mo ago
Brody
Brody17mo ago
you should not be commiting that to your repo add bin/ to the .gitignore
OneEye
OneEyeOP17mo ago
ou okay
OneEye
OneEyeOP17mo ago
finito
OneEye
OneEyeOP17mo ago
let me see if the deployment changed
Brody
Brody17mo ago
the bin folder is in the .gitignore now?
OneEye
OneEyeOP17mo ago
yes and now this error happens
OneEye
OneEyeOP17mo ago
OneEye
OneEyeOP17mo ago
mind you I have the start command as ./bin/main should I remove it?
Brody
Brody17mo ago
you shoud not need any build or start command, remove both also, remove NIXPACKS_NO_CACHE now
OneEye
OneEyeOP17mo ago
okay waiting for the redeploy yeye that's removed
OneEye
OneEyeOP17mo ago
OneEye
OneEyeOP17mo ago
should I remove the build command as well?
OneEye
OneEyeOP17mo ago
OneEye
OneEyeOP17mo ago
I love how for us developers just having different errors is progress :DDD
OneEye
OneEyeOP17mo ago
but then it'll start searching for package.json file in which I still have to write
"build": "go build main.go"
"build": "go build main.go"
Brody
Brody17mo ago
^
OneEye
OneEyeOP17mo ago
oki doki
Brody
Brody17mo ago
the package.json has nothing to do with your go app
OneEye
OneEyeOP17mo ago
I just assumed that pecause the placeholder said yarn run build, that was the default command
Brody
Brody17mo ago
you are the second person to think that
OneEye
OneEyeOP17mo ago
:DDDD
Solution
OneEye
OneEye17mo ago
okay so in summerty, the problem was 1. only load the env files when you're locally developing or just use railway shell to have railway env inside your local 2. don't add extra build and start scripts, railway will take care of it JUST specify the root path where your service exists PS: use railway to manage your local enviorment variables
OneEye
OneEyeOP17mo ago
that'll be it right?
Brody
Brody17mo ago
yes okay so now i wanna see you remove yourself of using .env files locally too
OneEye
OneEyeOP17mo ago
I will Brody I will :DDD I've already linked the project on the previus service and installed railway globaly you've helped me enouht and I really don't want to bother you you've been an emence help and I could have done it without ya
Brody
Brody17mo ago
ah thanks
OneEye
OneEyeOP17mo ago
take care Brody and have a wondeful day/night 🌅/🌃 also let me mark what I said as a solution
Brody
Brody17mo ago
you too!
Want results from more Discord servers?
Add your server