Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env files in services/* directory breaks runtime start #2132

Open
leorossi opened this issue Feb 20, 2024 · 26 comments
Open

.env files in services/* directory breaks runtime start #2132

leorossi opened this issue Feb 20, 2024 · 26 comments
Labels
bug Something isn't working

Comments

@leorossi
Copy link
Contributor

In a Platformatic Runtime, if thre is a .env file inside a service directory, seems like it is loaded in place of the root .env one.
This happens only if the service's platformatic.json file contains a reference to an env variable like

{
  "plugins": {
    "typescript": "{PLT_MY_SERVICE_TYPESCRIPT}"
  }
}

Repro:

$ node packages/create-platformatic/create-platformatic.mjs

Select Application and create the first (and only service)

Once finished ensure that the runtime starts correctly and stop it. Then

$ cd $RUNTIME_ROOT_DIR
$ touch services/YOUR_SERVICE_NAME/.env

Start again and you'll see that an error occurs, complaining that an env variable is missing.

The .env file inside the service should be either ignored or loaded together with the root one

@leorossi leorossi added the bug Something isn't working label Feb 20, 2024
@bobss24
Copy link
Contributor

bobss24 commented Feb 27, 2024

I can take a look at this, @leorossi

@leorossi
Copy link
Contributor Author

Great, thanks!

@bobss24
Copy link
Contributor

bobss24 commented Feb 29, 2024

This is the screenshot: screenshot

@bobss24
Copy link
Contributor

bobss24 commented Feb 29, 2024

and this:
screnshot

@bobss24
Copy link
Contributor

bobss24 commented Feb 29, 2024

@leorossi, what's the best way to debug this?

I am using VSCode. Are there platformatic-specific instructions somewhere? :)

@bobss24
Copy link
Contributor

bobss24 commented Mar 7, 2024

@leorossi, I am going to attempt attaching a proper debugger but I don't have the cycles, at the moment.

If you have any tips for me for setting up a Node.js debugging environment for plt - or if there are instructions elsewhere, pls let me know!

cc @mcollina!

@mcollina
Copy link
Member

mcollina commented Mar 7, 2024

You can run https://github.com/platformatic/platformatic/blob/main/packages/cli/cli.js with --inspect --inspect-brk and then open chrome dev tools

@bobss24
Copy link
Contributor

bobss24 commented Mar 14, 2024

I believe the error comes from the ConfigManager.. here:

packages/config/lib/manager.js

I'll look deeper tomorrow! :)

@bobss24
Copy link
Contributor

bobss24 commented Mar 14, 2024

The problem is a worker thread starts looking for a .env file up the tree from the starting directory of the service (services/name-of-service) and when it finds the "empty" .env in the service's dir, it gives up looking elsewhere, thus causing the env vars in the project root directory's .env to not be resolved, causing the error!

One solution could be to read all the .envs and then combine them.?

@leorossi
Copy link
Contributor Author

Or, since the worker thread runs only in runtimes, the .env file search should ignore the service's directory, so only the runtime root's one will be loaded.
I like also the idea of combining the envs, but it might be confusing for the user? WDYT @mcollina ?

@bobss24
Copy link
Contributor

bobss24 commented Mar 15, 2024

Or, since the worker thread runs only in runtimes, the .env file search should ignore the service's directory, so only the runtime root's one will be loaded. I like also the idea of combining the envs, but it might be confusing for the user? WDYT @mcollina ?

I like the idea here @leorossi .. however, if you see, the .env in the service's folder should get preference and the other .envs loaded later. But if we can do it, the root dir's .env can be the only one to be supported and we can safely ignore the service dir's .env What do you think?

It would be simpler, I think, for the user to know that the only .env to be read is the root dir's.. but it is an open question at this point what our strategy of parsing .envs should be.. 🤔🙂

I am in favor of supporting just 1 .env, that in the project's root dir...❤️ wdyt @mcollina ??

@mcollina
Copy link
Member

Or, since the worker thread runs only in runtimes, the .env file search should ignore the service's directory, so only the runtime root's one will be loaded.
I like also the idea of combining the envs, but it might be confusing for the user? WDYT @mcollina ?

I agree with not loading those.

@bobss24
Copy link
Contributor

bobss24 commented Mar 15, 2024

I'll try to check in a fix tomorrow..🙂

@bobss24
Copy link
Contributor

bobss24 commented Mar 16, 2024

I think I am too new to tackle this.. 🙁!

@bobss24
Copy link
Contributor

bobss24 commented Mar 17, 2024

Hi @leorossi , I have tried and there's common logic for reading config -- that will need to be refactored, somewhat!

@bobss24
Copy link
Contributor

bobss24 commented Mar 17, 2024

Please guide Me..❤️ 🙏 @mcollina

@mcollina
Copy link
Member

mcollina commented Mar 18, 2024

Unfortunately we have no time to give you step-by-step instructions.

@bobss24
Copy link
Contributor

bobss24 commented Mar 18, 2024

Unfortunately we have no time to give you step-by-step instructions.

Not asking for step-by-step instructions, @mcollina :)

Just looking for advice but never-mind then 🙂!

@bobss24
Copy link
Contributor

bobss24 commented Mar 18, 2024

This env-related code is hard-to-parse @mcollina :) . I'll refactor what I can 🙂!

@bobss24
Copy link
Contributor

bobss24 commented Mar 19, 2024

@leorossi, it can also be that we always default to the current working dir for the .env reading.

We can also make it an option in the configuration file for the service(for the user to specify), or have the runtime pass it to the services as they start up..this will likely not be trivial. Nor are the other options. We can::

  1. Have the user specify the path to the .env file
  2. Have the runtime make this decision and pass it through to the services as they start up
  3. Always parse the CWD's .env and not search anywhere!

Let me know what we prefer!

@mcollina 👍

@mcollina
Copy link
Member

  1. no
  2. yes
  3. no

@bobss24
Copy link
Contributor

bobss24 commented Mar 20, 2024

1. no

2. yes

3. no

Thanks, @mcollina. I'll begin work on this tomorrow. Maybe I should pick up to 3 tasks & work on them in parallel!
🙂

@leorossi
Copy link
Contributor Author

@bobss24 take a look at https://github.com/leorossi/dotenv-tool it's already imported in runtime package

@bobss24
Copy link
Contributor

bobss24 commented Mar 23, 2024

thx @leorossi! ❤️

I'll take a look when I am back from vacation! 🙂 ❤️

@bobss24
Copy link
Contributor

bobss24 commented Apr 9, 2024

I'm back.❤️ will take a look tomorrow, if all goes to plan.. :)

@bobss24
Copy link
Contributor

bobss24 commented Apr 11, 2024

@bobss24 take a look at https://github.com/leorossi/dotenv-tool it's already imported in runtime package

@leorossi thx for the suggestion here. I am not understanding how to use this. If we were collating the two .env's, I would have found your tool quite helpful for it. Here, the problem seems to be different wherein, instead of combining the two environment var config files, it stops looking for others. We have already decided to use the top-level .env in this case, ignoring what the service has in its directory!🍀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants