dotenv-gradle icon indicating copy to clipboard operation
dotenv-gradle copied to clipboard

The path that load the specified env file is incorrect

Open ttbadr opened this issue 2 years ago • 9 comments

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

ttbadr avatar Oct 12 '22 02:10 ttbadr

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. 👍

uzzu avatar Oct 19 '22 09:10 uzzu

@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.

uzzu avatar Oct 29 '22 14:10 uzzu

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 avatar Nov 26 '22 18:11 ikstewa

@ikstewa Thanks for help this. I got it. It would be better to not needed. I'll try to fix this issue.

uzzu avatar Feb 15 '23 13:02 uzzu

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

latch-lio avatar Mar 13 '23 18:03 latch-lio

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.

uzzu avatar Nov 26 '23 18:11 uzzu

The use cases of using CLI arguments that apply only to subprojects may not be considered. I'll try to add test cases later.

uzzu avatar Nov 26 '23 18:11 uzzu

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

uzzu avatar Nov 27 '23 02:11 uzzu

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

uzzu avatar Nov 27 '23 02:11 uzzu