Skip to content

Fix usethis tool failing in directories with leading dots#1903

Merged
nathanjmcdougall merged 4 commits into
mainfrom
copilot/fix-usethis-tool-bug
Apr 9, 2026
Merged

Fix usethis tool failing in directories with leading dots#1903
nathanjmcdougall merged 4 commits into
mainfrom
copilot/fix-usethis-tool-bug

Conversation

Copilot AI commented Apr 9, 2026

Copy link
Copy Markdown
Contributor
  • Fix get_project_name_from_dir() in src/usethis/_file/dir.py to strip leading/trailing non-alphanumeric characters (., -, _) since package names must start and end with a letter or digit per PEP spec
  • Fix ensure_pyproject_toml_via_uv() and opinionated_uv_init() in src/usethis/_backend/uv/init.py to pass --name with the sanitized project name from get_project_name_from_dir()
  • Fix ensure_pyproject_toml_via_poetry() and opinionated_poetry_init() in src/usethis/_backend/poetry/init.py to use get_project_name_from_dir() instead of raw usethis_config.cpd().name
  • Add tests for the leading-dot directory name case
  • Fix Windows test failure: use synthetic paths instead of creating real directories (lesson filed as agent: avoid creating filesystem directories with OS-special names in tests #1906)
  • Fix basedpyright: replace PurePosixPath with Path/str (basedpyright rejects PurePosixPath for Path | str | None)
  • Run full coverage and static checks (prek, basedpyright, CodeQL all pass)

Skills used: usethis-file-create, usethis-qa-static-checks, usethis-python-test-full-coverage, usethis-lesson-create, usethis-github-issue-create

Resolves #1925

…name

Pass --name with a sanitized project name to uv init, and fix
get_project_name_from_dir() to strip leading/trailing non-alphanumeric
characters (dots, dashes, underscores) per PEP packaging spec.

Also update poetry init to use get_project_name_from_dir() instead of
the raw directory name for consistency.

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/e415c235-da21-4847-b92c-704513c59e6e

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix bug when running usethis tool in directory with leading dot Fix usethis tool failing in directories with leading dots Apr 9, 2026
Copilot AI requested a review from nathanjmcdougall April 9, 2026 06:47
@nathanjmcdougall nathanjmcdougall marked this pull request as ready for review April 9, 2026 07:12
@codecov

codecov Bot commented Apr 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@codspeed-hq

codspeed-hq Bot commented Apr 9, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 3 untouched benchmarks


Comparing copilot/fix-usethis-tool-bug (42f76be) with main (a0eb8ab)

Open in CodSpeed

@nathanjmcdougall

Copy link
Copy Markdown
Collaborator

@copilot there's a test falling in the GitHub logs. Looks flaky. Please fix and make a lesson for how to avoid this flakiness

The test_only_dots test created a directory named "..." which on Windows
is interpreted as parent directory traversal (../..), causing
FileExistsError. Since get_project_name_from_dir() only reads Path.name
and never accesses the filesystem, all tests now use PurePosixPath
synthetic paths instead of creating real directories.

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/b47fb40c-7e7c-44f9-89e3-258b10a5fe34

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>

Copilot AI commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

@copilot there's a test falling in the GitHub logs. Looks flaky. Please fix and make a lesson for how to avoid this flakiness

Fixed in 220589e. The test_only_dots test created a directory named "..." which on Windows is interpreted as parent directory traversal (../..), causing FileExistsError. Since get_project_name_from_dir() only reads Path.name and never touches the filesystem, all tests now use PurePosixPath synthetic paths instead of creating real directories. Lesson filed as #1906.

@nathanjmcdougall

Copy link
Copy Markdown
Collaborator

@copilot Basedpyright is failing, please fix

PurePosixPath is not assignable to Path | str | None. Use tmp_path /
"name" (Path without mkdir) for most tests, and string paths for
Windows-problematic names ("..." and "project.") since usethis_config
.set() accepts str and converts to Path internally.

Agent-Logs-Url: https://github.com/usethis-python/usethis-python/sessions/06111449-4dd0-43b5-87cf-f13293af9a57

Co-authored-by: nathanjmcdougall <18602289+nathanjmcdougall@users.noreply.github.com>

Copilot AI commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Basedpyright is failing, please fix

Fixed in 42f76be. PurePosixPath is not assignable to Path | str | None per basedpyright. Tests now use tmp_path / "name" (creates a Path without mkdir()) for most cases, and str paths for Windows-problematic names ("...", "project.") since usethis_config.set() accepts str and converts to Path internally.

@nathanjmcdougall nathanjmcdougall merged commit fe3b8a6 into main Apr 9, 2026
20 checks passed
@nathanjmcdougall nathanjmcdougall deleted the copilot/fix-usethis-tool-bug branch April 9, 2026 20:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug when running usethis tool in a dir with a leading dot in its name

2 participants