public patchPoetryEnv()

in packages/monorepo/src/components/nx-configurator.ts [192:232]


  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)) {
      ["install", "install:ci"].forEach((t) => {
        const task = project.tasks.tryFind(t);

        // Setup env
        const createVenvCmd = "poetry env use python$PYTHON_VERSION";
        !task?.steps.find((s) => s.exec === createVenvCmd) &&
          task?.prependExec(createVenvCmd);

        // Ensure the projen & pdk bins are removed from the venv as we always want to use the npx variant
        const removeBinsCmd =
          "rm -f `poetry env info -p`/bin/projen `poetry env info -p`/bin/pdk";
        !task?.steps.find((s) => s.exec === removeBinsCmd) &&
          task?.exec(removeBinsCmd);

        const pythonVersion = project.deps.tryGetDependency("python")?.version;
        task!.env(
          "PYTHON_VERSION",
          pythonVersion && !pythonVersion?.startsWith("^")
            ? pythonVersion
            : `$(pyenv latest ${
                pythonVersion?.substring(1).split(".")[0] ||
                DEFAULT_PYTHON_VERSION
              } | cut -d '.' -f 1,2 || echo '')`
        );
      });

      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)"
      );
    }
  }