refactor(core): lift engine selection out of the CLI
`core::settings` exists because engine choice used to live behind a private `KEY_ENGINE` in pipdock-cli, and a setting written by one head that the other cannot read is worse than no setting — its own module doc says so. The lift missed two things, and S2's `pkg_outdated` needs both of them, so it would have made a third copy. - `engine::for_id(EngineId) -> Box<dyn Engine>` is now the only place an adapter is constructed. `run::engine_for` delegates; `configured_engine` and the duplicate `KEY_ENGINE` are gone. The CLI read the raw kv value and matched on the string, which is the encoding `settings::load` owns — including its "an unrecognised value keeps the default rather than failing" rule, which the CLI copy happened to reproduce by luck. - `run::app_data_dir` was a byte-for-byte copy of `store::default_app_data`, so it is now a re-export. Two functions deriving one path are one edit away from the two heads reading different directories, which would mean the pins and snapshots the user was relying on silently not being there. `pipdock engine <x>` now writes through `settings::save` rather than a raw kv `set`, so it materializes `settings.locale` and `settings.allowExternallyManaged` alongside the engine. Reads are unchanged: `load` filters an empty locale back to `None`, and an absent allowExternallyManaged already meant false. No behaviour change, and the 46 L4 goldens are the proof — none moved. Verified by running against an isolated LOCALAPPDATA: `engine uv` still exits 4 with PD-ENG-001 (uv is not on PATH here) and `engine pip` still exits 0 and reports the version, with the three kv rows landing exactly as `settings::load` expects them. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
03954a5334
commit
6be9df9dbc
2 files changed
+42
-36
No files matched your search
@@ -7,7 +7,7 @@
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use pipdock_core::engine::{Engine, ProgressEvent, pip::PipEngine, uv::UvEngine};
|
||||
use pipdock_core::engine::{self, Engine, ProgressEvent, pip::PipEngine, uv::UvEngine};
|
||||
use pipdock_core::envs::{self, Candidate};
|
||||
use pipdock_core::errors::{Code, PdError, Result};
|
||||
use pipdock_core::flow::{
|
||||
@@ -19,7 +19,7 @@ use pipdock_core::model::{EngineId, EnvSource, PkgName, PyEnv, StepStatus};
|
||||
/// translating what the user asked for into a `PlanRequest` is flow logic the GUI needs too.
|
||||
pub use pipdock_core::flow::Intent;
|
||||
use pipdock_core::store::Store;
|
||||
use pipdock_core::{index, pins, plan, snapshot};
|
||||
use pipdock_core::{index, pins, plan, settings, snapshot};
|
||||
|
||||
use crate::{EngineArg, Exit, GlobalOpts};
|
||||
|
||||
@@ -29,31 +29,21 @@ use crate::{EngineArg, Exit, GlobalOpts};
|
||||
/// `--engine` flag overrides for one invocation.
|
||||
#[must_use]
|
||||
pub fn engine_for(opts: &GlobalOpts) -> Box<dyn Engine> {
|
||||
match opts.engine {
|
||||
Some(EngineArg::Uv) => Box::new(UvEngine),
|
||||
Some(EngineArg::Pip) => Box::new(PipEngine),
|
||||
let id = match opts.engine {
|
||||
Some(EngineArg::Uv) => EngineId::Uv,
|
||||
Some(EngineArg::Pip) => EngineId::Pip,
|
||||
// No flag: use what the user configured. Falling back to pip rather than probing for uv
|
||||
// here keeps a read-only command from silently changing behaviour based on PATH; first-run
|
||||
// uv detection belongs to Settings, where the choice is shown and can be changed.
|
||||
None => match configured_engine() {
|
||||
Some(EngineId::Uv) => Box::new(UvEngine),
|
||||
_ => Box::new(PipEngine),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// `kv` key holding the configured engine.
|
||||
const KEY_ENGINE: &str = "settings.engine";
|
||||
|
||||
/// The engine the user configured, if any. A store that cannot be opened is not worth failing a
|
||||
/// command over — the default is safe.
|
||||
fn configured_engine() -> Option<EngineId> {
|
||||
let store = Store::open(&app_data_dir()).ok()?;
|
||||
match store.get(KEY_ENGINE).ok()??.as_str() {
|
||||
"uv" => Some(EngineId::Uv),
|
||||
"pip" => Some(EngineId::Pip),
|
||||
_ => None,
|
||||
}
|
||||
//
|
||||
// A store that cannot be opened is not worth failing a command over, and `settings::load`
|
||||
// already answers pip for a missing or unrecognized value — so one `map_or` covers every
|
||||
// way this can go wrong.
|
||||
None => Store::open(&app_data_dir())
|
||||
.and_then(|store| settings::load(&store))
|
||||
.map_or(EngineId::Pip, |s| s.engine),
|
||||
};
|
||||
engine::for_id(id)
|
||||
}
|
||||
|
||||
/// `pipdock engine <pip|uv>` — set the configured engine.
|
||||
@@ -70,10 +60,7 @@ pub async fn engine_set(opts: &GlobalOpts, engine: EngineArg) -> Result<Exit> {
|
||||
// Availability is checked against the selected environment when there is one; uv is a
|
||||
// standalone binary, so it can be checked without one.
|
||||
let info = match select_env(opts).await {
|
||||
Ok(env) => match id {
|
||||
EngineId::Pip => PipEngine.info(&env).await,
|
||||
EngineId::Uv => UvEngine.info(&env).await,
|
||||
},
|
||||
Ok(env) => engine::for_id(id).info(&env).await,
|
||||
Err(e) if id == EngineId::Uv => {
|
||||
let env = PyEnv {
|
||||
interpreter: PathBuf::new(),
|
||||
@@ -99,7 +86,13 @@ pub async fn engine_set(opts: &GlobalOpts, engine: EngineArg) -> Result<Exit> {
|
||||
));
|
||||
}
|
||||
|
||||
Store::open(&app_data_dir())?.set(KEY_ENGINE, id.as_str())?;
|
||||
// Through `settings::save`, not a raw kv write: the GUI's Settings screen reads this back with
|
||||
// `settings::load`, and a key written by one head that the other cannot find is the failure
|
||||
// mode `core::settings` exists to prevent.
|
||||
let store = Store::open(&app_data_dir())?;
|
||||
let mut settings = settings::load(&store)?;
|
||||
settings.engine = id;
|
||||
settings::save(&store, &settings)?;
|
||||
println!(
|
||||
"engine set to {} {}",
|
||||
id.as_str(),
|
||||
@@ -370,13 +363,12 @@ pub async fn doctor(opts: &GlobalOpts) -> Result<Exit> {
|
||||
}
|
||||
|
||||
/// The app data root, `%LOCALAPPDATA%\PipDock` (ARCHITECTURE §6).
|
||||
#[must_use]
|
||||
pub fn app_data_dir() -> PathBuf {
|
||||
std::env::var_os("LOCALAPPDATA")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(std::env::temp_dir)
|
||||
.join(pipdock_core::APP_DATA_DIR_NAME)
|
||||
}
|
||||
///
|
||||
/// Re-exported rather than computed here. The CLI carried a byte-for-byte copy of the core's
|
||||
/// version, which is the same failure `KEY_ENGINE` was: two functions deriving one path are one
|
||||
/// edit away from the two heads reading different directories, and the pins and snapshots the
|
||||
/// user was relying on silently not being there.
|
||||
pub use pipdock_core::store::default_app_data as app_data_dir;
|
||||
|
||||
/// `pipdock snapshot list`
|
||||
///
|
||||
|
||||
@@ -150,6 +150,20 @@ pub trait Engine: Send + Sync {
|
||||
async fn upgrade_pip(&self, env: &PyEnv) -> Result<StepResult>;
|
||||
}
|
||||
|
||||
/// The adapter for `id`.
|
||||
///
|
||||
/// Both heads reach this point by different routes — the CLI from `--engine` or the stored
|
||||
/// setting, the GUI from the stored setting alone — and both then need a `Box<dyn Engine>`. The
|
||||
/// mapping itself lives here so neither can grow its own idea of what "uv" selects (G5). The CLI
|
||||
/// previously owned the only copy, along with a second declaration of the settings key it reads.
|
||||
#[must_use]
|
||||
pub fn for_id(id: EngineId) -> Box<dyn Engine> {
|
||||
match id {
|
||||
EngineId::Pip => Box::new(pip::PipEngine),
|
||||
EngineId::Uv => Box::new(uv::UvEngine),
|
||||
}
|
||||
}
|
||||
|
||||
/// pip's floor for `install --dry-run --report -`. Below this, planning is impossible and the app
|
||||
/// offers a one-click pip upgrade (`PD-ENG-002`, DATA-FLOW §7).
|
||||
pub const PIP_MIN_VERSION_FOR_REPORT: (u32, u32) = (22, 2);
|
||||
|
||||
Reference in new issue
Block a user