Can I load a .env file for a module instead of the whole project?

There are some utils for loading .env files, can this support the above? Or do I need to use my own solution here.
Solution:
I see what you mean, I'll change the working directory in the IDEA run configuration
Jump to solution
46 Replies
gdude
gdude•4mo ago
What's your use-case specifically? .env is a standard and is parsed at runtime, so I'm curious what you're trying to do here
Shaun
ShaunOP•4mo ago
My bot is in a module, not the root project, the .env in my module isn't being loaded
gdude
gdude•4mo ago
the .env must be located in your current working directory otherwise, yeah, it won't find it dotenv is a sort of informal standard - it's a key-value file, it supports line comments, and it must be placed in the current working directory (it's not a kordex thing, we just provide a util for it)
Shaun
ShaunOP•4mo ago
I have seen implementations in the past where multiple .env files are supported for sub projects, with descending priority etc No worries - I'll have to implement something else then
gdude
gdude•4mo ago
I'm not really sure of the context you're working with it doesn't make sense to me to ask for a .env for a subproject because that implies you're resolving it at compile-time whereas .env is intended for users to fill out for run-time configuration
Shaun
ShaunOP•4mo ago
It's being used as a dev runtime configuration
gdude
gdude•4mo ago
is there a reason you can't put it in your cwd? I guess I could make the util check the classpath that doesn't seem very useful, though
Solution
Shaun
Shaun•4mo ago
I see what you mean, I'll change the working directory in the IDEA run configuration
gdude
gdude•4mo ago
yeah I mean for dev you'd probably just put it in your project root and gitignore it
Shaun
ShaunOP•4mo ago
There are multiple modules which have separate .env files
gdude
gdude•4mo ago
I see
Shaun
ShaunOP•4mo ago
This works now,thanks
gdude
gdude•4mo ago
no worries, but if you can think of an approach that might help out better, I'm happy to look into it
Shaun
ShaunOP•4mo ago
This is an implementation I saw on some typescript framework a while back, it seemed nice and intuitive for me.
root
.env
|----moduleA
.env
|----moduleB
.env
root
.env
|----moduleA
.env
|----moduleB
.env
if I run moduleA, .env from root and .env from moduleA are loaded, overwriting the root environment.
gdude
gdude•4mo ago
so moduleA is executable as well?
Shaun
ShaunOP•4mo ago
It can be sure, depends on your project
gdude
gdude•4mo ago
the question then is how KordEx would figure out how to traverse the tree I guess the gradle plugin would make it possible since it could supply a system property or.. something
Shaun
ShaunOP•4mo ago
I'm overlooking some details here Related to running code in a jar, vs from the IDE I don't think it needs a plugin, can just look at the directory structure when running in IDE
gdude
gdude•4mo ago
well, I don't think this approach makes sense for production
Shaun
ShaunOP•4mo ago
No but is .env meant for production?
gdude
gdude•4mo ago
yes
Shaun
ShaunOP•4mo ago
Okay fair enough
gdude
gdude•4mo ago
I wonder if I could just make the kordex gradle plugin configure the application plugin then it'd parse the .envs there and insert them into the runtime env vars that way are you using the kordex gradle plugin?
Shaun
ShaunOP•4mo ago
I'd personally have curent module > working directory > project root, the former and latter are optional I don't think so
gdude
gdude•4mo ago
mkay I don't think this is something kordex could realistically do on its own because it has to figure out how far up to look for .env files but with gradle's help it should work
Shaun
ShaunOP•4mo ago
Yeah well if I get some spare time maybe I'll put a pull request in, I'm better at explaining things using code lol
gdude
gdude•4mo ago
it's okay, I get what you're saying haha I'll open an issue on the gradle plugins repo so I don't forget
Shaun
ShaunOP•4mo ago
Okay sounds good thank you
gdude
gdude•4mo ago
will link in a few
Shaun
ShaunOP•4mo ago
Just an aside, my kordex bot is in 133 servers now 😮
No description
gdude
gdude•4mo ago
Nice, good work \o/ are you using kordex v2? just out of curiosity
Shaun
ShaunOP•4mo ago
val kordexVersion = "1.9.0-SNAPSHOT"
gdude
gdude•4mo ago
ah okay, you're on v1 still that's all good, v2 is like, a couple days old lmao
Shaun
ShaunOP•4mo ago
stable?
gdude
gdude•4mo ago
as stable as v1 I haven't had any reports yet anyway let me get this issue typed up https://github.com/Kord-Extensions/gradle-plugins/issues/5 I think that's correct, right?
Shaun
ShaunOP•4mo ago
Is this suggesting to bundle the .env files with the app?
gdude
gdude•4mo ago
No the application plugin provides a run task you'd use that to run your development bot the kordex plugin already configures it, but only with the main class
Shaun
ShaunOP•4mo ago
Okay, if its part of the run task then yes that sounds good to me
gdude
gdude•4mo ago
okay, cool alright then, I'll work on that when I get the chance probably this weekend you can watch the issue, I'll update it when it happens
Shaun
ShaunOP•4mo ago
Okay great thanks 🙂
gdude
gdude•4mo ago
o7 you will need to move to the gradle plugin for it though
gdude
gdude•4mo ago
but that's all documented if you need it https://docs.kordex.dev/kordex-plugin.html
gdude
gdude•4mo ago
you can do that now, but bear in mind that it will default to kordex v2 if you don't specify a version, and v2 has a different package layout (among other changes) I detailed the changes here if needed https://docs.kordex.dev/about.html#version-2
gdude
gdude•4mo ago
No description
gdude
gdude•4mo ago
No description
gdude
gdude•4mo ago
overriding works too okay cool this works I'm not ready to ship yet but I figured I'd post an update OK, shipping See https://discord.com/channels/1121419906995458098/1121524367818498128/1273968152945889343 :>
Want results from more Discord servers?
Add your server