Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(run): allow for loading of env files to be skipped (#3375)
  • Loading branch information
JamesHenry committed Oct 14, 2022
1 parent fd0befa commit 5dbd904
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 62 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,12 @@ Simply run `lerna add-caching` and follow the instructions in order to generate

You can learn more about the configuration it generates here: https://lerna.js.org/docs/concepts/task-pipeline-configuration

### Automatic loading of .env files in `lerna run` with the new task-runner implementation

By default the modern task runner powered by Nx will automatically load `.env` files for you. You can set `--load-env-files` to false if you want to disable this behavior for any reason.

For more details about what `.env` files will be loaded by default please see: https://nx.dev/recipes/environment-variables/define-environment-variables

### Obselete options in `lerna run` with the new task-runner implementation

There are certain legacy options for `lerna run` which are no longer applicable to the modern task-runner. Please see full details about those flags, and the reason behind their obselence, here:
Expand Down
6 changes: 6 additions & 0 deletions commands/run/README.md
Expand Up @@ -111,6 +111,12 @@ current working directory.
$ lerna run build --profile --profile-location=logs/profile/
```

### `--load-env-files`

By default the modern task runner powered by Nx will automatically load .env files for you. You can set `--load-env-files` to false if you want to disable this behavior for any reason.

For more details about what `.env` files will be loaded by default please see: https://nx.dev/recipes/environment-variables/define-environment-variables

### `useNx=false`

By setting `useNx` to `false` you can use the legacy task running implementations in `lerna` (`p-map` and `p-queue`) instead of using the default modern task runner implementation powered by [Nx](https://nx.dev).
5 changes: 5 additions & 0 deletions commands/run/command.js
Expand Up @@ -78,6 +78,11 @@ exports.builder = (yargs) => {
describe: "When useNx is not false, show verbose output from dependent tasks.",
type: "boolean",
},
"load-env-files": {
group: "Command Options:",
describe: "When useNx is not false, automatically load .env files",
type: "boolean",
},
});

return filterOptions(yargs);
Expand Down
1 change: 1 addition & 0 deletions commands/run/index.js
Expand Up @@ -311,6 +311,7 @@ class RunCommand extends Command {

const extraOptions = {
excludeTaskDependencies: mimicLernaDefaultBehavior,
loadDotEnvFiles: this.options.loadEnvFiles ?? true,
};

return { targetDependencies, options, extraOptions };
Expand Down
2 changes: 1 addition & 1 deletion commands/version/package.json
Expand Up @@ -48,7 +48,7 @@
"@lerna/run-topologically": "file:../../utils/run-topologically",
"@lerna/temp-write": "file:../../utils/temp-write",
"@lerna/validation-error": "file:../../core/validation-error",
"@nrwl/devkit": ">=14.8.1 < 16",
"@nrwl/devkit": ">=14.8.6 < 16",
"chalk": "^4.1.0",
"dedent": "^0.7.0",
"load-json-file": "^6.2.0",
Expand Down
4 changes: 2 additions & 2 deletions core/lerna/package.json
Expand Up @@ -59,11 +59,11 @@
"@lerna/publish": "file:../../commands/publish",
"@lerna/run": "file:../../commands/run",
"@lerna/version": "file:../../commands/version",
"@nrwl/devkit": ">=14.8.1 < 16",
"@nrwl/devkit": ">=14.8.6 < 16",
"import-local": "^3.0.2",
"inquirer": "^8.2.4",
"npmlog": "^6.0.2",
"nx": ">=14.8.1 < 16",
"nx": ">=14.8.6 < 16",
"typescript": "^3 || ^4"
}
}
7 changes: 7 additions & 0 deletions core/lerna/schemas/lerna-schema.json
Expand Up @@ -911,6 +911,9 @@
"skipNxCache": {
"$ref": "#/$defs/commandOptions/run/skipNxCache"
},
"loadEnvFiles": {
"$ref": "#/$defs/commandOptions/run/loadEnvFiles"
},

"npmClient": {
"$ref": "#/$defs/globals/npmClient"
Expand Down Expand Up @@ -1644,6 +1647,10 @@
"skipNxCache": {
"type": "boolean",
"description": "During `lerna run`, when true, skip the nx cache."
},
"loadEnvFiles": {
"type": "boolean",
"description": "During `lerna run`, when false, skip loading of environment variables from .env files."
}
},
"version": {
Expand Down
122 changes: 122 additions & 0 deletions e2e/tests/lerna-run/lerna-run-nx-env-files.ts
@@ -0,0 +1,122 @@
import { remove, writeFile } from "fs-extra";
import { Fixture } from "../../utils/fixture";
import { normalizeCommandOutput, normalizeEnvironment } from "../../utils/snapshot-serializer-utils";

expect.addSnapshotSerializer({
serialize(str: string) {
return normalizeCommandOutput(normalizeEnvironment(str));
},
test(val: string) {
return val != null && typeof val === "string";
},
});

describe("lerna-run-nx-env-files", () => {
let fixture: Fixture;

beforeEach(async () => {
fixture = await Fixture.create({
name: "lerna-run-nx-env-files",
packageManager: "npm",
initializeGit: true,
runLernaInit: true,
installDependencies: true,
/**
* Because lerna run involves spawning further child processes, the tests would be too flaky
* if we didn't force deterministic terminal output by appending stderr to stdout instead
* of interleaving them.
*/
forceDeterministicTerminalOutput: true,
});

await writeFile(fixture.getWorkspacePath(".env"), `SOMETHING_IN_ENV_FILE=some_value_here`, "utf-8");

await fixture.lerna("create package-1 -y");
await fixture.addScriptsToPackage({
packagePath: "packages/package-1",
scripts: {
"log-env-var": "echo $SOMETHING_IN_ENV_FILE",
},
});
});
afterEach(() => fixture.destroy());

describe("without nx enabled", () => {
it("should log an empty value", async () => {
// Enable legacy task runner
await fixture.overrideLernaConfig({
useNx: false,
});

const output = await fixture.lerna("run log-env-var -- --silent");

expect(output.combinedOutput).toMatchInlineSnapshot(`
lerna notice cli v999.9.9-e2e.0
lerna info Executing command in 1 package: "npm run log-env-var --silent"
lerna info run Ran npm script 'log-env-var' in 'package-X' in X.Xs:
lerna success run Ran npm script 'log-env-var' in 1 package in X.Xs:
lerna success - package-X
`);
});
});

describe("with nx enabled", () => {
it("should log a value by default", async () => {
await fixture.addNxJsonToWorkspace();

const output = await fixture.lerna("run log-env-var -- --silent");

expect(output.combinedOutput).toMatchInlineSnapshot(`
> Lerna (powered by Nx) Nx didn't recognize the following args: loadDotEnvFiles
When using '--' all executor args have to be defined after '--'.
> package-X:log-env-var --silent
> package-X@0.0.0 log-env-var
> echo $SOMETHING_IN_ENV_FILE "--silent"
some_value_here --silent
> Lerna (powered by Nx) Successfully ran target log-env-var for project package-X
lerna notice cli v999.9.9-e2e.0
`);
});

it("should log an empty value when --load-env-files=false", async () => {
await fixture.addNxJsonToWorkspace();

const output = await fixture.lerna("run log-env-var --load-env-files=false -- --silent");

expect(output.combinedOutput).toMatchInlineSnapshot(`
> Lerna (powered by Nx) Nx didn't recognize the following args: loadDotEnvFiles
When using '--' all executor args have to be defined after '--'.
> package-X:log-env-var --silent
> package-X@0.0.0 log-env-var
> echo $SOMETHING_IN_ENV_FILE "--silent"
--silent
> Lerna (powered by Nx) Successfully ran target log-env-var for project package-X
lerna notice cli v999.9.9-e2e.0
`);
});
});
});

0 comments on commit 5dbd904

Please sign in to comment.