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

Implements .env file support #5531

Merged
merged 1 commit into from
Jun 26, 2023
Merged

Implements .env file support #5531

merged 1 commit into from
Jun 26, 2023

Conversation

arcanis
Copy link
Member

@arcanis arcanis commented Jun 25, 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

  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

@arcanis arcanis merged commit c0cb6a5 into master Jun 26, 2023
15 checks passed
@arcanis arcanis deleted the mael/dotenv branch June 26, 2023 12:37
@daniluk4000
Copy link

Will this allow to use any variables from .env in .yarnrc.yml?

For example:

injectEnvironmentFiles:
  - .env

npmScopes:
  myScope:
    npmAlwaysAuth: true
    npmAuthToken: "${NPM_TOKEN:-}" #From .env
    npmRegistryServer: "https://customgitlab/api/v4/packages/npm"

@arcanis
Copy link
Member Author

arcanis commented Jun 26, 2023

I forgot to add a test case covering it but I think it should.

@belgattitude
Copy link

Nothing will be injected if it's set to an empty array or null

Worth to mention that

injectEnvironmentFiles: []  # <-- works
injectEnvironmentFiles: null # <-- Internal Error: ENOENT: no such file or directory, open '/home/sebastien/github/nextjs-monorepo-example/null'

@belgattitude
Copy link

Added some concerns here: #5544 (comment)

Myself I'd recommend to make it 'opt-in'. It will create issues with few projects (ie nextjs...)

@hanzelkatomas
Copy link

May I ask which version will be this released?

@merceyz
Copy link
Member

merceyz commented Jul 26, 2023

It will be in v4, you can use one of the release candidates if you want.

yarn set version canary

@lukeapage
Copy link

I was trying it out, thanks. Its worth noting that if you specify a env file that does not exist, you get an error, which means the use-case of .env.user that is git ignored is bad dx (the user needs to create an empty env file in order to run yarn install)

@arcanis
Copy link
Member Author

arcanis commented Aug 1, 2023

I was trying it out, thanks. Its worth noting that if you specify a env file that does not exist, you get an error, which means the use-case of .env.user that is git ignored is bad dx (the user needs to create an empty env file in order to run yarn install)

It doesn't error if you suffix the path with a question mark (ie .env.user? won't throw if .env.user is missing).

@lukeapage
Copy link

lukeapage commented Aug 4, 2023

Perfect, thanks - that works.

I just discovered it also errors when I run the command to setup my editor

yarn dlx @yarnpkg/sdks vscode

Internal Error: ENOENT: no such file or directory, open 'C:\Users\xxxx\AppData\Local\Temp\xfs-e4f23b26\dlx-12060\.env'
Error: ENOENT: no such file or directory, open 'C:\Users\xxxx\AppData\Local\Temp\xfs-e4f23b26\dlx-12060\.env'

which must be a bug?

(edit - easy to work around by adding ?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for .env file
6 participants