Skip to content

Commit

Permalink
fix(core): expand env variables on load and unload
Browse files Browse the repository at this point in the history
Env variables using other variables were not unloaded from the environment
and further customizations were impossible in more specific env files.
  • Loading branch information
matheo authored and xiongemi committed Jun 25, 2024
1 parent a3322f7 commit 1130c5f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
22 changes: 18 additions & 4 deletions e2e/nx/src/extras.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { parseJson } from '@nx/devkit';
import {
checkFilesExist,
cleanupProject,
getSelectedPackageManager,
expectTestsPass,
isNotWindows,
newProject,
readFile,
readJson,
runCLI,
runCLIAsync,
uniq,
updateFile,
updateJson,
Expand Down Expand Up @@ -293,7 +294,7 @@ describe('Extra Nx Misc Tests', () => {
});

describe('Env File', () => {
it('should have the right env', () => {
it('should have the right env', async () => {
const appName = uniq('app');
runCLI(
`generate @nx/react:app ${appName} --style=css --bundler=webpack --no-interactive`
Expand Down Expand Up @@ -334,8 +335,21 @@ describe('Extra Nx Misc Tests', () => {
});
`
);
const unitTestsOutput = runCLI(`test ${appName}`);
expect(unitTestsOutput).toContain('Successfully ran target test');
expectTestsPass(await runCLIAsync(`test ${appName}`));

updateFile('.env', (content) => {
content = content.replace('firstname', 'firstname2');
content = content.replace('lastname', 'lastname2');
return content;
});
updateFile(`apps/${appName}/src/app/app.spec.tsx`, (content) => {
content = content.replace(
'Welcome firstname lastname',
'Welcome firstname2 lastname2'
);
return content;
});
expectTestsPass(await runCLIAsync(`test ${appName}`));
});
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
"czg": "^1.4.0",
"detect-port": "^1.5.1",
"dotenv": "~16.3.1",
"dotenv-expand": "^10.0.0",
"dotenv-expand": "~11.0.6",
"ejs": "^3.1.7",
"enhanced-resolve": "^5.8.3",
"esbuild": "0.19.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"cli-spinners": "2.6.1",
"cliui": "^8.0.1",
"dotenv": "~16.3.1",
"dotenv-expand": "~10.0.0",
"dotenv-expand": "~11.0.6",
"enquirer": "~2.3.6",
"figures": "3.2.0",
"flat": "^5.0.2",
Expand Down
12 changes: 7 additions & 5 deletions packages/nx/src/tasks-runner/task-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ function loadDotEnvFilesForTask(
override: false,
});
environmentVariables = {
...expand({
...myEnv,
ignoreProcessEnv: true, // Do not override existing env variables as we load
}).parsed,
...expand(myEnv).parsed,
...environmentVariables,
};
}
Expand All @@ -198,7 +195,12 @@ function loadDotEnvFilesForTask(
function unloadDotEnvFiles(environmentVariables: NodeJS.ProcessEnv) {
const unloadDotEnvFile = (filename: string) => {
let parsedDotEnvFile: NodeJS.ProcessEnv = {};
loadDotEnvFile({ path: filename, processEnv: parsedDotEnvFile });
const myEnv = loadDotEnvFile({
path: filename,
processEnv: parsedDotEnvFile,
override: false,
});
parsedDotEnvFile = { ...expand(myEnv).parsed, ...parsedDotEnvFile };
Object.keys(parsedDotEnvFile).forEach((envVarKey) => {
if (environmentVariables[envVarKey] === parsedDotEnvFile[envVarKey]) {
delete environmentVariables[envVarKey];
Expand Down
34 changes: 23 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1130c5f

Please sign in to comment.