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

Add support for .env file #4718

Closed
jmariller opened this issue Aug 8, 2022 · 10 comments · Fixed by #5531
Closed

Add support for .env file #4718

jmariller opened this issue Aug 8, 2022 · 10 comments · Fixed by #5531
Labels
enhancement New feature or request

Comments

@jmariller
Copy link

Hi,

I am referring to an already existing issue which was closed, imho, without a proper resolution:
#2724

There was a pull request which was eventually rejected, with a proposed alternative solution (see one of the first messages from @paul-soporan):
#3938

Now I would really love to try and implement the proposal myself, unfortunately I do not have enough time and knowledge on my end. However with some hints and support I would be more than happy to help: is anybody willing to support this?

Many thanks!

@RDIL RDIL added the enhancement New feature or request label Sep 9, 2022
@pi0neerpat
Copy link

pi0neerpat commented Jan 24, 2023

Looking forward to that PR getting merged. Until then, the only solution is to inject the variable manually every time you run a command, which is not ideal.

GITHUB_PAT_PACKAGES=ghp_**** yarn install
npmScopes:
  my-organization:
    npmRegistryServer: https://npm.pkg.github.com
    npmAlwaysAuth: true
    npmAuthToken: ${GITHUB_PAT_PACKAGES}

We lost the ability to use a global .npmrc, and never got a replacement. I would prefer not needing to check in my PAT to git.

arcanis added a commit that referenced this issue Jun 26, 2023
**What's the problem this PR addresses?**

A common need is to provide environment values into the environment via
`.env` files. There's been a couple of issues and attempts at
implementation already, which all were decently upvoted. I myself could
have used it once or twice 😄

Props to @jj811208 for his implementation in #4835 - I wanted to make
the configuration a little more generic (allowing to have multiple
environment files, and to possibly disable it altogether), but it was a
appreciated start.

Fixes #4718 
Closes #4835 (Supercedes it)

**How did you fix it?**

A new setting, `injectEnvironmentFiles`, lets you define files that Yarn
will load and inject into all scripts. It only affects subprocesses -
Yarn itself still uses `process.env` for its checks, so you can't for
example set `YARN_*` values and expect them to be applied to the current
process (use the yarnrc file for that instead).

The `injectEnvironmentFiles` setting has a few properties:

- It defaults to `.env`
- Nothing will be injected if it's set to an empty array or null
- The paths inside may be suffixed by `?` - in that case, Yarn won't
throw if the file doesn't exist

The idea with this last property is to allow for simple user
configuration (imagine, with the example below, that the project also
has a gitignore with `.env.*`):

```
injectEnvironmentFiles:
  - .env
  - .env.${USER}?
```

**Checklist**
<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [x] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
@jmariller
Copy link
Author

Thank you so much @arcanis for implementing this feature, very much appreciated 🙏 Do you have any idea when the new version will be released with this change?

@merceyz
Copy link
Member

merceyz commented Jun 29, 2023

@jmariller I've created a new release candidate so you can get it now by running

yarn set version canary

@jmariller
Copy link
Author

awesome, thanks a lot @merceyz 👍

@co-sic
Copy link

co-sic commented Jun 29, 2023

@jmariller is it working for you?
I get the error Usage Error: Environment variable not found (NODE_AUTH_TOKEN) in /**/my-service/.yarnrc.yml (in /**/my-service/.yarnrc.yml)

with my .yarnrc.yml file

nodeLinker: node-modules

injectEnvironmentFiles:
  - .env

npmScopes:
  my-organization:
    npmRegistryServer: https://npm.pkg.github.com/
    npmAuthToken: ${NODE_AUTH_TOKEN}

yarnPath: .yarn/releases/yarn-4.0.0-rc.47.cjs

@jmariller
Copy link
Author

@co-sic no it does not work for me either, I am getting precisely the same error with a very similar setup - I am using pnp instead of node-modules for nodeLinker, aside from that all the same.

@arcanis
Copy link
Member

arcanis commented Jun 29, 2023

Turns out I forgot to make a change, and since the test was missing I didn't see it; I fixed it and added a test in #5544

arcanis added a commit that referenced this issue Jun 29, 2023
**What's the problem this PR addresses?**

I forgot in #4718 to add a test making sure that the yarnrc settings
could use the variable set in the dotenv files (the existing tests were
mainly around the values set during script execution), and of course I
forgot a change 🥲

**How did you fix it?**

We now pass the updated environment when interpolating values.

**Checklist**

<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [x] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
@jmariller
Copy link
Author

thank you @arcanis is there already a new release candidate so we can also try it?

@arcanis
Copy link
Member

arcanis commented Jun 30, 2023

Not yet, I prefer to wait until releasing a new one until #5542 is fixed by the Nx folks. In the meantime you can try it by running yarn set version from sources.

Note that I'm considering changing the default value of injectEnvironmentFiles from .env to .env.yarn due to the risks described in #5544 (comment)

@jmariller
Copy link
Author

fair enough - and by the way, it works perfectly now 🙂 looking forward to getting the new release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
6 participants