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 UISolution:Jump to 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...152 Replies
Project ID:
d1e05c2d-ef92-4ac2-8a0a-8505878d0103
d1e05c2d-ef92-4ac2-8a0a-8505878d0103
maybe because the monorepo contains the front-end as well
but I've changed the root directory as well but stillno dice
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")
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?
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
^
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
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 developmentyeye, I'm using envvalut to manage .env files https://vault.dotenv.org/
well with railways service variables and the cli there's no need for that either!
hmmmm, I'll look in to it, because I really love this railways approach of "every service being in one place"
definitely do, it's such a nice dev experience with service variables and the railway shell
okay
despite the fact that I changed "log.fatal" to just "fmt.Println" it still has this error
and I've changed that error text as well but still no dice
this should just print it our right? no reason for crashing the app
and railway did do a new build?
ye
and I even did this
and added this to railway
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
okay.....
I'm giga dumb
let me fix that :DDD
sorry for the false positive
I think it's time to completely move away from .env files
based
I'm going to burn my pants
well get going on this
I changed the code to this
ye but I still need it in the local development
no you dont
really?
read message again
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
oh yeah sorry my bad
https://docs.railway.app/develop/cli
I'm still baffled why this doesn't work
but I digress, I'll continue tinkering around
thank you for guiding me @Brody really appreciate it
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 variablesokay let me do that
and also yes, it just attached to github project
but I'll also try creating a new project as well
try the variable before the new project
yeye
and you'd only need a new service, cache is done per service id and no two services will never have the same id
* sad trumpet sounds *
wat
I'm going to move this from the mono repo to a seperate repo
no no
monorepo is fine, slow your horses
: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
it's from railway but it's caused by something you did
show me a screenshot of the service variables please
do you have a nixpacks.toml file in your project
nono
I'm using turborepo
but it's detecting Nix for some reason :DDD
nixpacks is the builder, that's normal
delete the nixpacks config file service variable
kk
or should I give it a 0 value?
delete it
obliterated it
have you set this
yeye, and it did this crash
screenshot of service variables again please
sure
omfg
I'm sorry
nono don't apologize Brody :DDD
you're the one taking your time and helping me
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
ou okay
let me try that
also sorry for taking several minutes before replying
no worries
let me create another service
also on the deployment I completely commented this out
odd
very
can this be the problem btw?
no that looks good to me, thats the folder with your go code
ye ye
creted a clean service but still this
then its not cache
ye
can you share your repo
okay give me a sec let mech check if there's nothing sencitive there :DDD
also where is your database hosted
the same project?
ye
please delete all your database variables and replace them with variable references
https://docs.railway.app/develop/variables#reference-variables
ooo okay
give me asec
screenshot of service variables after thats done please
also if you wat we can cal and I can share my screen eve
of course if it's not a problem
cal?
call
in like going to a VC
oh, i dont even have a mic
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
easiest if i can see the repo
sorry Brody I can't do that at this moment
no worries
sus repo, i understand
ye :DDD
youtube downloader 😉
no it's not sus per say I'm just anxios, you know :DD
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
are you loading a .env file somwhere else in your code?
and this is where I connect to the db
nop nop
that's the only place I load the file it self
other then that I just use it in different places
chuck a
func init() { fmt.Println("test") }
in your main.go file to see if it prints on railwaysure
I'll strain up delete everything from main and just have single print out there
slow your horses
🏇 🏇 🏇 🏇 🏇 🏇 🏇 🏇
AAAAAAAAAAAAAAAAA
:DDD
why are the programming gods laughing at me :DDD
and this is a new service?
yes, which has the exact same stuff
build command, start command, root path and watch path
show me a screenshot of your github repo open to this go project
sure thing
here'a closer look
whats in the bin folder
the built main.go
you should not be commiting that to your repo
add
bin/
to the .gitignoreou okay
finito
let me see if the deployment changed
the bin folder is in the .gitignore now?
yes and now this error happens
mind you I have the start command as
./bin/main
should I remove it?you shoud not need any build or start command, remove both
also, remove NIXPACKS_NO_CACHE now
okay waiting for the redeploy
yeye that's removed
should I remove the build command as well?
I love how for us developers just having different errors is progress :DDD
but then it'll start searching for package.json file
in which I still have to write
^
oki doki
the package.json has nothing to do with your go app
I just assumed that pecause the placeholder said yarn run build, that was the default command
you are the second person to think that
:DDDD
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 variablesthat'll be it right?
yes
okay so now i wanna see you remove yourself of using .env files locally too
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
ah thanks
take care Brody and have a wondeful day/night 🌅/🌃
also let me mark what I said as a solution
you too!