Repository: astral-sh/uv
File: crates/uv-requirements/src/specification.rs (Rust)

Code before:
                    Ok(contents) => contents,
                    Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
                        return Err(anyhow::anyhow!("File not found: `{}`", path.user_display()));
                    }
                    Err(err) => {
                        return Err(anyhow::anyhow!(
                            "Failed to read `{}`: {}",
                            path.user_display(),
                            err
                        ));
                    }
                };
                let _ = toml::from_str::<PyProjectToml>(&contents)
                    .with_context(|| format!("Failed to parse: `{}`", path.user_display()))?;

                Self {
                    source_trees: vec![path.clone()],
                    ..Self::default()
                }
            }
            RequirementsSource::Pep723Script(path) => {
                let script = match Pep723Script::read(&path).await {
                    Ok(Some(script)) => Pep723Item::Script(script),
                    Ok(None) => {
                        return Err(anyhow::anyhow!(
                            "`{}` does not contain a PEP 723 metadata tag; run `{}` to initialize the script",
                            path.user_display(),
                            format!("uv init --script {}", path.user_display())
                        ));
                    }
                    Err(Pep723Error::Io(err)) if err.kind() == std::io::ErrorKind::NotFound => {
                        return Err(anyhow::anyhow!(
                            "Failed to read `{}` (not found); run `{}` to create a PEP 723 script",
                            path.user_display(),
                            format!("uv init --script {}", path.user_display())
                        ));
                    }
                    Err(err) => return Err(err.into()),
                };

                let metadata = script.metadata();

                let requirements = metadata
                    .dependencies
                    .as_ref()
                    .map(|dependencies| {
                        dependencies
                            .iter()
                            .map(|dependency| {
                                UnresolvedRequirementSpecification::from(Requirement::from(
                                    dependency.to_owned(),

Review comment (suggestion):
I'd probably omit the `run ... to initialize` hint. That belongs outside the error message. The dependencies would be empty too, so it'd have no meaningful effect for this kind of usage.

Code after:
                    Ok(contents) => contents,
                    Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
                        return Err(anyhow::anyhow!("File not found: `{}`", path.user_display()));
                    }
                    Err(err) => {
                        return Err(anyhow::anyhow!(
                            "Failed to read `{}`: {}",
                            path.user_display(),
                            err
                        ));
                    }
                };
                let _ = toml::from_str::<PyProjectToml>(&contents)
                    .with_context(|| format!("Failed to parse: `{}`", path.user_display()))?;

                Self {
                    source_trees: vec![path.clone()],
                    ..Self::default()
                }
            }
            RequirementsSource::Pep723Script(path) => {
                let script = match Pep723Script::read(&path).await {
                    Ok(Some(script)) => Pep723Item::Script(script),
                    Ok(None) => {
                        return Err(anyhow::anyhow!(
                            "`{}` does not contain inline script metadata",
                            path.user_display(),
                        ));
                    }
                    Err(Pep723Error::Io(err)) if err.kind() == std::io::ErrorKind::NotFound => {
                        return Err(anyhow::anyhow!(
                            "Failed to read `{}` (not found)",
                            path.user_display(),
                        ));
                    }
                    Err(err) => return Err(err.into()),
                };

                let metadata = script.metadata();

                let requirements = metadata
                    .dependencies
                    .as_ref()
                    .map(|dependencies| {
                        dependencies
                            .iter()
                            .map(|dependency| {
                                UnresolvedRequirementSpecification::from(Requirement::from(
                                    dependency.to_owned(),
                                ))
                            })