Skip to content

Plugin can ONLY find .env file if dotenvOptions.path is set with an absolute path #56

Closed
@PsiRadish

Description

@PsiRadish

Describe the bug
If dotenvOptions.path is not set then dotenv will proceed with its default path of

path.resolve(process.cwd(), '.env')

and since process.cwd() is the VS Code installation directory, it will not find anything. Setting dotenvOptions.path to a relative path also fails, for likely the same reason.

To Reproduce
Steps to reproduce the behavior:

  1. In some React project
    Add a .env file at the project root containing
    HELLO=WORLD
    
  2. In typescript-plugin-css-modules
    Add the following after the dotenv.config call in index.ts and rebuild:
    logger.log(`process.env.HELLO after dotenv.config = ${process.env.HELLO}`);
  3. Put the above modified version of typescript-plugin-css-modules in the React project's node_modules
  4. Add typescript-plugin-css-modules to compilerOptions.plugins in the React project's tsconfig.json with no additional options
  5. Reload Window in VS Code for the React project, then view a ts/tsx file and open the TS Server log
  6. tsserver.log contains:
    [typescript-plugin-css-modules] process.env.HELLO after dotenv.config = undefined
    
    Loading from default location with dotenvOptions.path unset = FAILURE
  7. Add the following options for typescript-plugin-css-modules to tsconfig.json:
    "options": {
      "dotenvOptions": {
        "path": "./.env"
      }
    }
  8. Reload Window in VS Code, view the ts/tsx file again, and open the TS Server log
  9. tsserver.log contains:
    [typescript-plugin-css-modules] process.env.HELLO after dotenv.config = undefined
    
    Loading from relative path in dotenvOptions.path = FAILURE
  10. Set the following options for typescript-plugin-css-modules in tsconfig.json:
    "options": {
      "dotenvOptions": {
        "path": "C:/Full/Absolute/Path/to/Project/.env"
      }
    }
  11. Reload Window in VS Code, view the ts/tsx file again, and open the TS Server log
  12. tsserver.log shows:
    [typescript-plugin-css-modules] process.env.HELLO after dotenv.config = WORLD
    
    Loading from absolute path in dotenvOptions.path = ONLY SUCCESS

Expected behavior

  1. Plugin can direct dotenv to load a .env file located at the root of the project without dotenvOptions.path having to be set
  2. Plugin can direct dotenv to load a file from a relative path in dotenvOptions.path

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions