Skip to content

Commit

Permalink
Allow python protobuf codegen export to coexist with namespace/host p…
Browse files Browse the repository at this point in the history
…ackage (#21665)

Before this change, if a codegen export clashes with an existing package
in the venv the export halts completely. The current change skips the
problematic exports. This is sub optimal because not all symbols are
exported but better than halting the export.

See #21659
  • Loading branch information
achrafmam2 authored Nov 20, 2024
1 parent fbec3bb commit ac4851a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/notes/2.25.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ established for this purpose. This non-profit's only source of revenue is

### General

- [Fixed](https://github.com/pantsbuild/pants/pull/21665) bug where `pants --export-resolve=<resolve> --export-py-generated-sources-in-resolve=<resolve>` fails (see [#21659](https://github.com/pantsbuild/pants/issues/21659) for more info).

### New Options System

The "legacy" options system is removed in this release. All options parsing is now handled by the new, native parser.
Expand Down
6 changes: 5 additions & 1 deletion src/python/pants/backend/python/goals/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ async def python_codegen_export_setup() -> _ExportPythonCodegenSetup:
content=textwrap.dedent(
f"""\
import os
import shutil
import site
import sys
Expand All @@ -424,7 +425,10 @@ async def python_codegen_export_setup() -> _ExportPythonCodegenSetup:
for item in os.listdir(codegen_dir):
if item == "{_ExportPythonCodegenSetup.SCRIPT_NAME}":
continue
os.rename(os.path.join(codegen_dir, item), os.path.join(site_packages_dir, item))
src = os.path.join(codegen_dir, item)
dest = os.path.join(site_packages_dir, item)
shutil.copytree(src, dest, dirs_exist_ok=True)
shutil.rmtree(src)
"""
).encode(),
)
Expand Down
30 changes: 27 additions & 3 deletions src/python/pants/backend/python/goals/export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,32 @@ async def do_codegen(request: CodegenGenerateSourcesRequest) -> GeneratedSources
rule_runner.write_files(
{
"test-resolve.lock": "",
# Case #1
# `foo`` package exports `an-input.py` file as a generated source.
"src/python/foo/BUILD": dedent(
"""\
codegen_target(name="codegen", source="an-input", resolve="test-resolve")
"""
"""
codegen_target(
name="codegen",
source="an-input",
resolve="test-resolve"
)
"""
),
"src/python/foo/an-input": "print('Hello World!')\n",
# Case #2
# The local `ansicolors`` package exports `ansicolors-input.py` file as a generated source.
# The local `ansicolors package clashes with the 3rd party dep `ansicolors``.
# This is an edge case.
"src/python/ansicolors/BUILD": dedent(
"""
codegen_target(
name="codegen",
source="ansicolors-input",
resolve="test-resolve"
)
"""
),
"src/python/ansicolors/ansicolors-input": "print('Hello World!')\n",
}
)

Expand All @@ -267,3 +287,7 @@ async def do_codegen(request: CodegenGenerateSourcesRequest) -> GeneratedSources
export_snapshot = rule_runner.request(Snapshot, [export_result.digest])
assert any(p.endswith("__pants_codegen__/codegen_setup.py") for p in export_snapshot.files)
assert any(p.endswith("__pants_codegen__/foo/an-input.py") for p in export_snapshot.files)
assert any(
p.endswith("__pants_codegen__/ansicolors/ansicolors-input.py")
for p in export_snapshot.files
)

0 comments on commit ac4851a

Please sign in to comment.