Skip to content

Commit 28b6258

Browse files
Fix: 修复检测现有项目管理器时可能返回空列表的问题 (#189)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e6a5c1e commit 28b6258

4 files changed

Lines changed: 73 additions & 73 deletions

File tree

.basedpyright/baseline.json

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nb_cli/cli/commands/project.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -342,15 +342,22 @@ async def create(
342342
)
343343

344344
if install_dependencies:
345-
try:
346-
manager = await ListPrompt(
347-
_("Which project manager would you like to use?"),
348-
choices=[Choice(mgr, mgr) for mgr in await all_environment_managers()],
349-
).prompt_async(style=CLI_DEFAULT_STYLE)
350-
except CancelledError:
351-
return
345+
managers = await all_environment_managers()
346+
if len(managers) > 1:
347+
try:
348+
manager = (
349+
await ListPrompt(
350+
_("Which project manager would you like to use?"),
351+
choices=[Choice(mgr, mgr) for mgr in managers],
352+
).prompt_async(style=CLI_DEFAULT_STYLE)
353+
).data
354+
except CancelledError:
355+
return
356+
else:
357+
assert len(managers) == 1
358+
manager = managers[0]
352359

353-
if manager.data == "pip":
360+
if manager == "pip":
354361
try:
355362
use_venv = await ConfirmPrompt(
356363
_("Create virtual environment?"), default_choice=True
@@ -374,7 +381,7 @@ async def create(
374381
config_manager = ConfigManager(working_dir=project_dir, use_venv=use_venv)
375382

376383
executor = await EnvironmentExecutor.get(
377-
manager.data,
384+
manager,
378385
toml_manager=config_manager,
379386
cwd=project_dir,
380387
executable=config_manager.python_path,

nb_cli/handlers/environment.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"pdm": "pdm.lock",
3232
"poetry": "poetry.lock",
3333
}
34-
_manager_exec = [*_manager_features.keys(), "pip"]
3534

3635
FdFile: TypeAlias = int | IO[bytes] | IO[str]
3736

@@ -62,10 +61,12 @@ def probe_environment_manager(*, cwd: Path | None = None) -> tuple[str, str]:
6261
def all_environment_managers() -> list[str]:
6362
"""Get all available environment managers on the system.
6463
64+
'pip' is also included as a fallback option.
65+
6566
Returns:
6667
A list of available environment manager names.
6768
"""
68-
return [m for m in _manager_exec if which(m) is not None]
69+
return [*(m for m in _manager_features if which(m) is not None), "pip"]
6970

7071

7172
class EnvironmentExecutor(metaclass=abc.ABCMeta):

pdm.lock

Lines changed: 54 additions & 54 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)