diff --git a/src/cli/init.rs b/src/cli/init.rs index d3574554c..4185c2bf9 100644 --- a/src/cli/init.rs +++ b/src/cli/init.rs @@ -6,7 +6,7 @@ use std::{ }; use clap::{Parser, ValueEnum}; -use miette::IntoDiagnostic; +use miette::{Context, IntoDiagnostic}; use minijinja::{context, Environment}; use pixi_config::{get_default_author, Config}; use pixi_consts::consts; @@ -15,6 +15,7 @@ use pixi_manifest::{ }; use pixi_utils::conda_environment_file::CondaEnvFile; use rattler_conda_types::{NamedChannelOrUrl, Platform}; +use tokio::fs::OpenOptions; use url::Url; use crate::Project; @@ -349,6 +350,30 @@ pub async fn execute(args: Args) -> miette::Result<()> { ) .unwrap(); save_manifest_file(&pyproject_manifest_path, rv)?; + let src_dir = dir.join("src").join(default_name); + tokio::fs::create_dir_all(&src_dir) + .await + .into_diagnostic() + .wrap_err_with(|| format!("Could not create directory {}.", src_dir.display()))?; + + let init_file = src_dir.join("__init__.py"); + match OpenOptions::new() + .write(true) + .create_new(true) + .open(&init_file) + .await + { + Ok(_) => (), + Err(e) if e.kind() == ErrorKind::AlreadyExists => { + // If the file already exists, do nothing + } + Err(e) => { + return Err(e).into_diagnostic().wrap_err_with(|| { + format!("Could not create file {}.", init_file.display()) + }); + } + }; + // Create a 'pixi.toml' manifest } else { // Check if the 'pixi.toml' file doesn't already exist. We don't want to diff --git a/tests/integration/test_main_cli.py b/tests/integration/test_main_cli.py index 9684832c0..4364f4ebd 100644 --- a/tests/integration/test_main_cli.py +++ b/tests/integration/test_main_cli.py @@ -234,3 +234,11 @@ def test_simple_project_setup(tmp_path: Path) -> None: ExitCode.SUCCESS, stderr_contains=["osx-arm64", "test", "Removed"], ) + + +def test_pixi_init_pyproject(tmp_path: Path) -> None: + manifest_path = tmp_path / "pyproject.toml" + # Create a new project + verify_cli_command(f"pixi init {tmp_path} --pyproject-toml", ExitCode.SUCCESS) + # Verify that install works + verify_cli_command(f"pixi install --manifest-path {manifest_path}", ExitCode.SUCCESS)