dotenv-gradle
dotenv-gradle copied to clipboard
The path that load the specified env file is incorrect
I got an odd issue, I specified the env file in the gradle.properties
with a relative path. I think the path is relative to the root project path, but the log tells me the plugin lookup the env file under the first subproject path. I have no idea
I apply the plugin in the root project test
if I set the dotenv.filename=./env/.env.aks_fresh
, the plugin will load the file from project/test/commons/env/.env.aks_fresh
if I set the dotenv.filename=../env/.env.aks_fresh
, the plugin will load the file from project/env/.env.aks_fresh
the respect result is loading the file from project/test/env/.env.aks_fresh
if I set the dotenv.filename=.env.aks_fresh
, the plugin will load the file from project/test/.env.aks_fresh
, so seem the dotenv.filename
did not support the file path, but only the file name, right?
I hope the dotenv.filename
support the file path, I have lots of env file, and I want to put them under a folder
Thanks for reporting!
As you said, only the file name is supported, current behavior when specified relative path is not expected, though. I’ll try to add a feature for supporting relative path from subproject path. 👍
@ttbadr
I’ve add a test by using relative path, but it works. https://github.com/uzzu/dotenv-gradle/commit/92b6640205ee0c1cd37eadb72775b98e0bb86efc 🤔 Your use case seems to not a use case by using relative path simply. I’d like to know your usecase more clearly.
- Gradle version
- dotenv-gradle plugin version
- Could you explain your Gradle Project structure?
- Directory structure
- Gradle Project structure
- The settings.gradle If you modified Project#projectDir.
- A directory structure which you want to in your project.
The issue is that you have to specify the env file in all subprojects. So if you want to use say ./env/.env.staging
from a sub-project named sub
, the build will fail unless there are two files ./env/.env.staging
and ./sub/env/.env.staging
@ikstewa Thanks for help this. I got it. It would be better to not needed. I'll try to fix this issue.
I got the same. Changing the .env filename through -Pdotenv.filename at command line get me the root project folder instead of the subproject path:
root-project/
├─ build.gradle
├─ api/
│ ├─ gemini/
│ │ ├─ build.gradle
> gradlew" :api:gemini:bootRun --args='--spring.profiles.active=local' -Pdotenv.filename=.env.local
Could not read the dotenv file specified in the gradle.properties. dotenv.filename: .env.local, path: /home/xxxx/workspace/root-project/.env.local
Trying to update to relative path:
> gradlew" :api:gemini:bootRun --args='--spring.profiles.active=local' -Pdotenv.filename=api/gemini/.env.local
Could not read the dotenv file specified in the gradle.properties. dotenv.filename: .env.local, path: /home/xxxx/workspace/root-project/api/api/gemini/.env.local
it put 2x api, not sure why? I'm not sure why should I put a relative path based on the rootDir
Sorry for keeping you waiting for a long time.
This issue should have been fixed in 3.0.0. https://github.com/uzzu/dotenv-gradle/blob/main/CHANGELOG.md#300---2023-11-27
Please try to use it, and give the feedback.
The use cases of using CLI arguments that apply only to subprojects may not be considered. I'll try to add test cases later.
I'm still working on how to make the use cases of executing subproject's task with dotenv option by using CLI arguments, but I think the following tests would be good to pass.
Now failing test case
@Test
fun changeFileWithIgnoringParentByUsingCliOptionWithCallSubProject() {
RootProject(projectDir) {
settingsGradle(
"""
include("sub")
""".trimIndent()
)
buildGradle(
"""
plugins {
id("base")
id("co.uzzu.dotenv.gradle")
}
println("[root] FOO: ${'$'}{env.FOO.value}")
""".trimIndent()
)
file(
".env.template",
"""
FOO=
""".trimIndent()
)
file(
".env",
"""
FOO=100
""".trimIndent()
)
directory("sub")
file(
"sub/build.gradle",
"""
plugins {
id("base")
}
println("[sub] BAR: ${'$'}{env.BAR.value}")
""".trimIndent()
)
file(
"sub/.env.template",
"""
BAR=
""".trimIndent()
)
file(
"sub/.env",
"""
BAR=200
""".trimIndent()
)
file(
"sub/.env.local",
"""
BAR=2000
""".trimIndent()
)
}
val result = GradleRunner.create()
.withPluginClasspath()
.withProjectDir(projectDir)
.withArguments(":sub:clean", "-Pdotenv.filename=.env.local")
.build()
assertAll {
assertThat(result.output).contains("[root] FOO: 100")
assertThat(result.output).contains("[sub] BAR: 2000")
}
}
execution log
FAILURE: Build failed with an exception.
* Where:
Build file '/private/var/folders/xxx/build.gradle.kts' line: 1
* What went wrong:
An exception occurred applying plugin request [id: 'co.uzzu.dotenv.gradle']
> Failed to apply plugin 'co.uzzu.dotenv.gradle'.
> Could not read the dotenv file specified in the gradle.properties. dotenv.filename: .env.local, path: /private/var/folders/xxx/.env.local
https://github.com/uzzu/dotenv-gradle/issues/67 created a issue of problem https://github.com/uzzu/dotenv-gradle/issues/39#issuecomment-1466671706 https://github.com/uzzu/dotenv-gradle/issues/39#issuecomment-1827020991 Please watch issue if you got the same.
This issue only deals of feedback to https://github.com/uzzu/dotenv-gradle/blob/main/CHANGELOG.md#changed-1