Skip to content

Commit

Permalink
fix(monorepo): ensure local venv is used for all subproject tasks (#587)
Browse files Browse the repository at this point in the history
The poetry env info command will print the parent environment if the VIRTUAL_ENV environment variale
is already set to the parent environment. Ensure this gets reset for all subproject tasks to address
issues where dependencies would appear to be missing during build.
  • Loading branch information
cogwirrel authored Oct 12, 2023
1 parent 9310211 commit 0eec5ef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 94 deletions.
23 changes: 15 additions & 8 deletions packages/monorepo/src/components/nx-configurator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from "path";
import { Component, JsonFile, Project, Task } from "projen";
import { JavaProject } from "projen/lib/java";
import { NodePackageManager, NodeProject } from "projen/lib/javascript";
import { PythonProject } from "projen/lib/python";
import { Poetry, PythonProject } from "projen/lib/python";
import { NxProject } from "./nx-project";
import { NxWorkspace } from "./nx-workspace";
import { Nx } from "../nx-types";
Expand Down Expand Up @@ -136,19 +136,26 @@ export class NxConfigurator extends Component implements INxProjectCore {
this.nx.affected.defaultBase = options?.defaultReleaseBranch ?? "mainline";
}

public patchPoetryInstall(project: PythonProject): void {
const installTask = project.tasks.tryFind("install");

if (installTask?.steps[0]?.exec !== "unset VIRTUAL_ENV") {
installTask?.env("VIRTUAL_ENV", "");
installTask?.prependExec("unset VIRTUAL_ENV");
public patchPoetryEnv(project: PythonProject): void {
// Since the root monorepo is a poetry project and sets the VIRTUAL_ENV, and poetry env info -p will print
// the virtual env set in the VIRTUAL_ENV variable if set, we need to unset it to ensure the local project's
// env is used.
if (ProjectUtils.isNamedInstanceOf(project.depsManager as any, Poetry)) {
project.tasks.addEnvironment(
"VIRTUAL_ENV",
"$(env -u VIRTUAL_ENV poetry env info -p || echo '')"
);
project.tasks.addEnvironment(
"PATH",
"$(echo $(env -u VIRTUAL_ENV poetry env info -p || echo '')/bin:$PATH)"
);
}
}

public patchPythonProjects(projects: Project[]): void {
projects.forEach((p) => {
if (ProjectUtils.isNamedInstanceOf(p, PythonProject)) {
this.patchPoetryInstall(p);
this.patchPoetryEnv(p);
}
this.patchPythonProjects(p.subprojects);
});
Expand Down
26 changes: 4 additions & 22 deletions packages/monorepo/test/__snapshots__/monorepo.test.ts.snap

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

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

0 comments on commit 0eec5ef

Please sign in to comment.