aws / aws-pdk

The AWS PDK provides building blocks for common patterns together with development tools to manage and build your projects.
https://aws.github.io/aws-pdk/
Apache License 2.0
362 stars 72 forks source link

[BUG] (type-safe-api) install fails on python runtime package with missing README #813

Closed jstrunk closed 1 month ago

jstrunk commented 1 month ago

Describe the bug

After cloning a project repo and installing the monorepo's dependencies with pnpm install or similar, packages that depend on an API's python runtime package fail because the generated README.md is missing and poetry install requires it. Using commitGenerated in the type safe api package or the python runtime options still includes the README.md and the module files in the runtime's .gitignore.

Expected Behavior

After cloning a project repo, I should be able to install dependencies and build all packages.

Current Behavior

After cloning the repo and running pnpm i:

│ > nx run api-detective-api-python-handlers:install │ api-detective-api-python-handlers: Progress: resolved 1, reused 0, downloaded 0, added 0 │ api-detective-api-python-handlers: Packages: +2 │ api-detective-api-python-handlers: ++ │ api-detective-api-python-handlers: Progress: resolved 2, reused 1, downloaded 0, added 1 │ api-detective-api-python-handlers: Progress: resolved 2, reused 2, downloaded 0, added 1 │ api-detective-api-python-handlers: Progress: resolved 2, reused 2, downloaded 0, added 2 │ api-detective-api-python-handlers: Progress: resolved 2, reused 2, downloaded 0, added 2, done │ api-detective-api-python-handlers: 👾 install | poetry env use python$PYTHON_VERSION │ api-detective-api-python-handlers: Creating virtualenv api-detective-api-python-handlers-CBvV-DEk-py3.12 in /Users/strunkjd/Library/Caches/pypoetry/virtualenvs │ api-detective-api-python-handlers: Using virtualenv: /Users/strunkjd/Library/Caches/pypoetry/virtualenvs/api-detective-api-python-handlers-CBvV-DEk-py3.12 │ api-detective-api-python-handlers: 👾 install | poetry update │ api-detective-api-python-handlers: Updating dependencies │ api-detective-api-python-handlers: Resolving dependencies... │ api-detective-api-python-handlers: Package operations: 79 installs, 0 updates, 0 removals │ api-detective-api-python-handlers: - Installing ... ... │ api-detective-api-python-handlers: - Installing api-detective-api-python-runtime (0.0.0 /private/tmp/api-detective/packages/api/generated/runtime/python) │ api-detective-api-python-handlers: ChefBuildError │ api-detective-api-python-handlers: Backend subprocess exited when trying to invoke build_editable │ api-detective-api-python-handlers:
│ api-detective-api-python-handlers: Traceback (most recent call last): │ api-detective-api-python-handlers: File "/Users/strunkjd/.pyenv/versions/3.12.2/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 373, in │ api-detective-api-python-handlers: main() │ api-detective-api-python-handlers: File "/Users/strunkjd/.pyenv/versions/3.12.2/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 357, in main │ api-detective-api-python-handlers: json_out["return_val"] = hook(**hook_input["kwargs"]) │ api-detective-api-python-handlers: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ api-detective-api-python-handlers: File "/Users/strunkjd/.pyenv/versions/3.12.2/lib/python3.12/site-packages/pyproject_hooks/_in_process/_in_process.py", line 294, in build_editable │ api-detective-api-python-handlers: return hook(wheel_directory, config_settings, metadatadirectory) │ api-detective-api-python-handlers: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ api-detective-api-python-handlers: File "/var/folders/4/kvx9bwf15_j628lytjzz0mwc0000gq/T/tmp5cjmi4ri/.venv/lib/python3.12/site-packages/poetry/core/masonry/api.py", line 82, in build_e… │ api-detective-api-python-handlers: return WheelBuilder.makein( │ api-detective-api-python-handlers: ^^^^^^^^^^^^^^^^^^^^^ │ api-detective-api-python-handlers: File "/var/folders/4/kvx9bwf15j628lytjzz0mwc0000gq/T/tmp5cjmi4ri/.venv/lib/python3.12/site-packages/poetry/core/masonry/builders/wheel.py", line 81,… │ api-detective-api-python-handlers: wb = WheelBuilder( │ api-detective-api-python-handlers: ^^^^^^^^^^^^^ │ api-detective-api-python-handlers: File "/var/folders/4/kvx9bwf15j628lytjzz0mwc0000gq/T/tmp5cjmi4ri/.venv/lib/python3.12/site-packages/poetry/core/masonry/builders/wheel.py", line 62,… │ api-detective-api-python-handlers: super().init(poetry, executable=executable) │ api-detective-api-python-handlers: File "/var/folders/4/kvx9bwf15_j628lytjzz0mwc0000gq/T/tmp5cjmi4ri/.venv/lib/python3.12/site-packages/poetry/core/masonry/builders/builder.py", line 5… │ api-detective-api-python-handlers: self._meta = Metadata.from_package(self.package) │ api-detective-api-python-handlers: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ api-detective-api-python-handlers: File "/var/folders/4/kvx9bwf15_j628lytjzz0mwc0000gq/T/tmp5cjmi4ri/.venv/lib/python3.12/site-packages/poetry/core/masonry/metadata.py", line 58, in fr… │ api-detective-api-python-handlers: with readme.open(encoding="utf-8") as f: │ api-detective-api-python-handlers: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ api-detective-api-python-handlers: File "/Users/strunkjd/.pyenv/versions/3.12.2/lib/python3.12/pathlib.py", line 1013, in open │ api-detective-api-python-handlers: return io.open(self, mode, buffering, encoding, errors, newline) │ api-detective-api-python-handlers: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ │ api-detective-api-python-handlers: FileNotFoundError: [Errno 2] No such file or directory: '/private/tmp/api-detective/packages/api/generated/runtime/python/README.md' │ api-detective-api-python-handlers:
│ api-detective-api-python-handlers: at ~/.pyenv/versions/3.12.2/lib/python3.12/site-packages/poetry/installation/chef.py:164 in _prepare │ api-detective-api-python-handlers: 160│ │ api-detective-api-python-handlers: 161│ error = ChefBuildError("\n\n".join(message_parts)) │ api-detective-api-python-handlers: 162│ │ api-detective-api-python-handlers: 163│ if error is not None: │ api-detective-api-python-handlers: → 164│ raise error from None │ api-detective-api-python-handlers: 165│ │ api-detective-api-python-handlers: 166│ return path │ api-detective-api-python-handlers: 167│ │ api-detective-api-python-handlers: 168│ def _prepare_sdist(self, archive: Path, destination: Path | None = None) -> Path: │ api-detective-api-python-handlers: Note: This error originates from the build backend, and is likely not a problem with poetry but with api-detective-api-python-runtime (0.0.0 /private/tmp/… │ api-detective-api-python-handlers: 👾 Task "install" failed when executing "poetry update" (cwd: /private/tmp/api-detective/packages/api/handlers/python) │ api-detective-api-python-handlers: Warning: run-commands command "pnpm dlx projen install" exited with non-zero status code

Reproduction Steps

.projenrc.ts

const pdkVersion = "0.23.40";
const projectName = "sample";

const project = new monorepo.MonorepoTsProject({
  devDeps: [`@aws/pdk@${pdkVersion}`],
  name: projectName,
  packageManager: javascript.NodePackageManager.PNPM,
  projenrcTs: true,
  gitignore: [".bashrc", ".profile", ".idea"],
  prettier: true,
});

const api = new TypeSafeApiProject({
  parent: project,
  name: `${projectName}-api`,
  outdir: "packages/api",
  model: {
    language: ModelLanguage.OPENAPI,
    options: {
      openapi: {
        title: `${projectName}-api`,
      },
    },
  },
  infrastructure: {
    language: Language.TYPESCRIPT,
  },
  handlers: {
    languages: [Language.PYTHON],
  },
  runtime: {
    languages: [Language.PYTHON],
    options: {
      python: {
          commitGenerated: true,
      },
    },
  },
  commitGenerated: true,
});

Possible Solution

First, commitGenerated: true in a Python runtime package doesn't work. .gitignore still includes the README.md and module directory:

packages/api/generated/runtime/python/.gitignore

sample_api_python_runtime
README.md

Second, commitGenerated on the type safe api project should be recursively applied to child projects.

Additional Information/Context

Mitigation:

After defining the api project in .projenrc.ts, add the following:

api.runtime.python?.gitignore.removePatterns(
  "README.md",
  api.runtime.python?.moduleName,
);

PDK version used

0.23.40

What languages are you seeing this issue on?

Typescript, Python

Environment details (OS name and version, etc.)

MacOS 14.5 intel

jstrunk commented 1 month ago

While implementing this possible solution, I realized that commitGenerated is for Projen project files and defaults to true. I'm implementing a new option commitGeneratedCode with the desired behavior. It will default to false except for python projects that use poetry.