Skip to content

Commit 2c91b57

Browse files
rmorsheaofek
andauthored
Name .pth file so it loads first (#828)
* Name .pth file so it loads first Users adding their own .pth files likely need to be loaded after the one inserted by hatchling. The only way to do this is by naming it so the hatchling .pth file comes first alphabetically. * update * add history entry --------- Co-authored-by: Ofek Lev <[email protected]>
1 parent 3ea6fad commit 2c91b57

File tree

8 files changed

+11
-7
lines changed

8 files changed

+11
-7
lines changed

backend/src/hatchling/builders/wheel.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,9 @@ def build_editable_detection(self, directory: str, **build_data: Any) -> str:
438438
editable_project.add_to_path(os.path.dirname(relative_path))
439439

440440
for filename, content in sorted(editable_project.files()):
441+
if filename.endswith('.pth') and not filename.startswith('_'):
442+
filename = f'_{filename}'
443+
441444
record = archive.write_file(filename, content)
442445
records.write(record)
443446

@@ -475,7 +478,7 @@ def build_editable_explicit(self, directory: str, **build_data: Any) -> str:
475478
for relative_directory in self.config.dev_mode_dirs
476479
)
477480

478-
record = archive.write_file(f"{self.metadata.core.name.replace('-', '_')}.pth", '\n'.join(directories))
481+
record = archive.write_file(f"_{self.metadata.core.name.replace('-', '_')}.pth", '\n'.join(directories))
479482
records.write(record)
480483

481484
for included_file in self.recurse_forced_files(self.get_forced_inclusion_map(build_data)):

docs/history/hatchling.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1111
***Fixed:***
1212

1313
- Fix dev mode when the project has symlinks and file inclusion is defined with the `packages` or `only-include` options
14+
- Change the name of generated PTH files for dev mode so they come first lexicographically and therefore load first
1415

1516
## [1.17.0](https://github.com/pypa/hatch/releases/tag/hatchling-v1.17.0) - 2023-05-12 ## {: #hatchling-v1.17.0 }
1617

tests/helpers/templates/wheel/standard_editable_exact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_files(**kwargs):
1616
if str(f.path) == 'LICENSE.txt':
1717
files.append(File(Path(metadata_directory, 'licenses', f.path), f.contents))
1818

19-
pth_file_name = f"{kwargs['package_name']}.pth"
19+
pth_file_name = f"_{kwargs['package_name']}.pth"
2020
loader_file_name = f"_editable_impl_{kwargs['package_name']}.py"
2121
files.append(File(Path(pth_file_name), f"import _editable_impl_{kwargs['package_name']}"))
2222
files.append(

tests/helpers/templates/wheel/standard_editable_exact_extra_dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_files(**kwargs):
1616
if str(f.path) == 'LICENSE.txt':
1717
files.append(File(Path(metadata_directory, 'licenses', f.path), f.contents))
1818

19-
pth_file_name = f"{kwargs['package_name']}.pth"
19+
pth_file_name = f"_{kwargs['package_name']}.pth"
2020
loader_file_name = f"_editable_impl_{kwargs['package_name']}.py"
2121
files.append(File(Path(pth_file_name), f"import _editable_impl_{kwargs['package_name']}"))
2222
files.append(

tests/helpers/templates/wheel/standard_editable_exact_force_include.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_files(**kwargs):
1818
elif f.path.parts[-1] == '__about__.py':
1919
files.append(File(Path('zfoo.py'), f.contents))
2020

21-
pth_file_name = f"{kwargs['package_name']}.pth"
21+
pth_file_name = f"_{kwargs['package_name']}.pth"
2222
loader_file_name = f"_editable_impl_{kwargs['package_name']}.py"
2323
files.append(File(Path(pth_file_name), f"import _editable_impl_{kwargs['package_name']}"))
2424
files.append(

tests/helpers/templates/wheel/standard_editable_pth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_files(**kwargs):
1616
if str(f.path) == 'LICENSE.txt':
1717
files.append(File(Path(metadata_directory, 'licenses', f.path), f.contents))
1818

19-
pth_file_name = f"{kwargs['package_name']}.pth"
19+
pth_file_name = f"_{kwargs['package_name']}.pth"
2020
files.append(File(Path(pth_file_name), '\n'.join(package_paths)))
2121
files.append(
2222
File(

tests/helpers/templates/wheel/standard_editable_pth_extra_dependencies.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def get_files(**kwargs):
1616
if str(f.path) == 'LICENSE.txt':
1717
files.append(File(Path(metadata_directory, 'licenses', f.path), f.contents))
1818

19-
pth_file_name = f"{kwargs['package_name']}.pth"
19+
pth_file_name = f"_{kwargs['package_name']}.pth"
2020
files.append(File(Path(pth_file_name), '\n'.join(package_paths)))
2121
files.append(
2222
File(

tests/helpers/templates/wheel/standard_editable_pth_force_include.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def get_files(**kwargs):
1818
elif f.path.parts[-1] == '__about__.py':
1919
files.append(File(Path('zfoo.py'), f.contents))
2020

21-
pth_file_name = f"{kwargs['package_name']}.pth"
21+
pth_file_name = f"_{kwargs['package_name']}.pth"
2222
files.append(File(Path(pth_file_name), '\n'.join(package_paths)))
2323
files.append(
2424
File(

0 commit comments

Comments
 (0)