AminFazlMondo/Lerna-Projen

Feature request: dependencies between sub projects

jeromevdl opened this issue · 14 comments

At the moment, when defining a LernaProject with sub projects (defining the same parent) and building or compiling, ... all projects are compiled or built in parallel (eg. lerna run default --stream).

Feature: provide the ability to have dependencies between sub projects (let's say A & B) so that a project (B) cannot be built/compiled/... before another one (A) if fully built. For example, to have a sample project (B) that use the output of A.

Thanks @jeromevdl for logging this issue.
There are couple of utility functions that might be helpful to chievev this in https://github.com/AminFazlMondo/Lerna-Projen/blob/main/src/utils.ts.
Please let me know if I have not fully captured your requirement.

Thank you @AminFazlMondo. Almost good, I still have a problem tho:

Using useNx: true in my parent project and specifying addNxTaskDependency(projectB, "compile", "build", projectA); I can now see that projectA is built before projectB is compiled which is great.
But for some reason, it does not take into account the command I specified for my tests projectA.setScript('test', 'npx projen test --group=-e2e'); and thus run the e2e tests, which I don't want.
If I remove the addNxTaskDependency and only keep only useNx: true, it works properly, a bit weird. Any idea?

I have not experienced this before, Let me set up couple of projects and test that.
Meanwhile, could you please confirm that your build task in projectA does not have any reference to test task?

build task is npx projen build which obviously run tests but this is the internal projen stuff...

If I understand correctly, you are running the compile task, and in the scenario that there is no task dependency between two projects, it will only run compile task on both projects in parallel. but with the dependency that you added it will make sure to run the build task on the projectA before compile of projectB.
As you mentioned, the build task includes test task. In past, I have worked around this by adding my e2e tests as a new task that is not part of the build, and it is only part of release task.
Hope this clarifies more

Actually the e2e tests should not be executed as part of the tests (note the dash before e2e), it means it runs all the tests except the e2e (using jest groups). This is the problem, with this dependency, it looks like my command is ignored and thus e2e tests are executed.

And I have another task only for e2e tests.

Could you please try projectA.setScript('test', 'jest --group=-e2e')

not better, it is ignored. The command executed is : build » test | jest --passWithNoTests --updateSnapshot

The reason that it runs that command is that with the setScript function, you are only modifying the script i the package.json file not the actual task, so it means when it gets to run build it runs the original command.
Here is one of the workarounds

projectA.testTask.steps.at(0).exec = 'jest --group=-e2e'

or maybe

projectA.testTask.steps.at(0).args = ['--group=-e2e']

@jeromevdl , did the solution given above helped you?

didn't have the time to try again, will tell you. thanks for hint

exec and args are readonly variables, cannot change them :(

@jeromevdl ,
This is another way I got that to add the option for me locally, please let me know if that works for you.

import { javascript, typescript } from "projen";
import { LernaProject, utils } from "lerna-projen";
const project = new LernaProject({
  defaultReleaseBranch: "main",
  devDeps: ["lerna-projen"],
  name: "lerna",
  packageManager: javascript.NodePackageManager.PNPM,
  useNx: true,
  projenrcTs: true,
});
const projectA = new typescript.TypeScriptProject({
  defaultReleaseBranch: 'main',
  name: 'projectA',
  parent: project,
  packageManager: javascript.NodePackageManager.PNPM,
  outdir: 'packages/projectA',
  jestOptions: {
    extraCliOptions: ['--group=-e2e']
  }
})
const projectB = new typescript.TypeScriptProject({
  defaultReleaseBranch: 'main',
  name: 'projectB',
  parent: project,
  packageManager: javascript.NodePackageManager.PNPM,
  outdir: 'packages/projectB'
})
utils.addNxTaskDependency(projectB, "compile", "build", projectA)
project.synth();

Closing the issue due to inactivity, please re-open if the issue still exists