From 1ba8def34e5d78d86583c3231d30859a2d6743fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=9A=80=20Jack?= Date: Fri, 29 Sep 2023 16:06:18 +1000 Subject: [PATCH] fix(monorepo): ensure local venv is used for all subproject tasks 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. --- .../src/components/nx-configurator.ts | 23 ++++-- .../test/__snapshots__/monorepo.test.ts.snap | 26 +----- .../type-safe-api-project.test.ts.snap | 80 ++++--------------- 3 files changed, 35 insertions(+), 94 deletions(-) diff --git a/packages/monorepo/src/components/nx-configurator.ts b/packages/monorepo/src/components/nx-configurator.ts index 6c24c8ccf..3ebbe35d9 100644 --- a/packages/monorepo/src/components/nx-configurator.ts +++ b/packages/monorepo/src/components/nx-configurator.ts @@ -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"; @@ -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", + "$(unset VIRTUAL_ENV; poetry env info -p)" + ); + project.tasks.addEnvironment( + "PATH", + "$(echo $(VIRTUAL_ENV='' poetry env info -p)/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); }); diff --git a/packages/monorepo/test/__snapshots__/monorepo.test.ts.snap b/packages/monorepo/test/__snapshots__/monorepo.test.ts.snap index bd2fea703..14e1e97bb 100644 --- a/packages/monorepo/test/__snapshots__/monorepo.test.ts.snap +++ b/packages/monorepo/test/__snapshots__/monorepo.test.ts.snap @@ -9006,8 +9006,8 @@ cython_debug/ "packages/consumer/.projen/tasks.json": { "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -9041,14 +9041,8 @@ cython_debug/ }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "poetry update", }, @@ -9365,8 +9359,8 @@ cython_debug/ "packages/library/.projen/tasks.json": { "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -9400,14 +9394,8 @@ cython_debug/ }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "poetry update", }, @@ -11818,14 +11806,8 @@ cython_debug/ }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "pip install --upgrade pip", }, diff --git a/packages/type-safe-api/test/project/__snapshots__/type-safe-api-project.test.ts.snap b/packages/type-safe-api/test/project/__snapshots__/type-safe-api-project.test.ts.snap index f0a4b6955..4c4b1032b 100644 --- a/packages/type-safe-api/test/project/__snapshots__/type-safe-api-project.test.ts.snap +++ b/packages/type-safe-api/test/project/__snapshots__/type-safe-api-project.test.ts.snap @@ -5956,8 +5956,8 @@ pyproject.toml "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { "AWS_PDK_VERSION": "0.0.0", - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -6002,14 +6002,8 @@ pyproject.toml }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "mkdir -p openapi_java_python_runtime && touch openapi_java_python_runtime/__init__.py", }, @@ -12033,8 +12027,8 @@ mocks "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { "AWS_PDK_VERSION": "0.0.0", - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -12082,14 +12076,8 @@ mocks }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "mkdir -p openapi_python_python_infra && touch openapi_python_python_infra/__init__.py", }, @@ -13075,8 +13063,8 @@ pyproject.toml "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { "AWS_PDK_VERSION": "0.0.0", - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -13121,14 +13109,8 @@ pyproject.toml }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "mkdir -p openapi_python_python_runtime && touch openapi_python_python_runtime/__init__.py", }, @@ -20783,8 +20765,8 @@ pyproject.toml "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { "AWS_PDK_VERSION": "0.0.0", - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -20829,14 +20811,8 @@ pyproject.toml }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "mkdir -p openapi_typescript_python_runtime && touch openapi_typescript_python_runtime/__init__.py", }, @@ -35370,8 +35346,8 @@ pyproject.toml "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { "AWS_PDK_VERSION": "0.0.0", - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -35416,14 +35392,8 @@ pyproject.toml }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "mkdir -p smithy_java_python_runtime && touch smithy_java_python_runtime/__init__.py", }, @@ -53590,8 +53560,8 @@ mocks "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { "AWS_PDK_VERSION": "0.0.0", - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -53639,14 +53609,8 @@ mocks }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "mkdir -p smithy_python_python_infra && touch smithy_python_python_infra/__init__.py", }, @@ -54632,8 +54596,8 @@ pyproject.toml "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { "AWS_PDK_VERSION": "0.0.0", - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -54678,14 +54642,8 @@ pyproject.toml }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "mkdir -p smithy_python_python_runtime && touch smithy_python_python_runtime/__init__.py", }, @@ -62526,8 +62484,8 @@ pyproject.toml "//": "~~ Generated by projen. To modify, edit .projenrc.js and run "npx projen".", "env": { "AWS_PDK_VERSION": "0.0.0", - "PATH": "$(echo $(poetry env info -p)/bin:$PATH)", - "VIRTUAL_ENV": "$(poetry env info -p)", + "PATH": "$(echo $(VIRTUAL_ENV='' poetry env info -p)/bin:$PATH)", + "VIRTUAL_ENV": "$(unset VIRTUAL_ENV; poetry env info -p)", }, "tasks": { "build": { @@ -62572,14 +62530,8 @@ pyproject.toml }, "install": { "description": "Install and upgrade dependencies", - "env": { - "VIRTUAL_ENV": "", - }, "name": "install", "steps": [ - { - "exec": "unset VIRTUAL_ENV", - }, { "exec": "mkdir -p smithy_typescript_python_runtime && touch smithy_typescript_python_runtime/__init__.py", },