Skip to content

Commit 60d865e

Browse files
fix: create dir before locking; adjust OntoEnv path handling
Co-authored-by: aider (openai/gpt-5) <[email protected]>
1 parent 2630120 commit 60d865e

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lib/src/io.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ pub struct PersistentGraphIO {
245245

246246
impl PersistentGraphIO {
247247
pub fn new(path: PathBuf, offline: bool, strict: bool) -> Result<Self> {
248+
// Ensure target directory exists before creating/locking files
249+
std::fs::create_dir_all(&path)?;
248250
// Try to acquire an exclusive lock for writer; if any readers/writers hold the lock, error out immediately
249251
let lock_path = path.join("store.lock");
250252
let lock_file = std::fs::OpenOptions::new()

python/src/lib.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use pyo3::{
1515
use std::borrow::Borrow;
1616
use std::collections::{HashMap, HashSet};
1717
use std::path::PathBuf;
18+
use std::ffi::OsStr;
1819
use std::sync::{Arc, Mutex};
1920

2021
fn anyhow_to_pyerr(e: Error) -> PyErr {
@@ -211,7 +212,17 @@ impl OntoEnv {
211212
temporary: bool,
212213
no_search: bool,
213214
) -> PyResult<Self> {
214-
let root_path = path.clone().unwrap_or_else(|| PathBuf::from(root));
215+
let mut root_path = path.clone().unwrap_or_else(|| PathBuf::from(root));
216+
// If the provided path points to a '.ontoenv' directory, treat its parent as the root
217+
if root_path
218+
.file_name()
219+
.map(|n| n == OsStr::new(".ontoenv"))
220+
.unwrap_or(false)
221+
{
222+
if let Some(parent) = root_path.parent() {
223+
root_path = parent.to_path_buf();
224+
}
225+
}
215226

216227
// Strict Git-like behavior:
217228
// - temporary=True: create a temporary (in-memory) env
@@ -250,15 +261,19 @@ impl OntoEnv {
250261
// Explicit create/overwrite at root_path
251262
OntoEnvRs::init(cfg, true).map_err(anyhow_to_pyerr)?
252263
} else {
253-
// Discover upward from root_path; load if found, else error.
264+
// Discover upward from root_path; load if found. If not found and not read-only,
265+
// initialize a new environment at the requested root.
254266
match ::ontoenv::api::find_ontoenv_root_from(&root_path) {
255267
Some(found_root) => OntoEnvRs::load_from_directory(found_root, read_only)
256268
.map_err(anyhow_to_pyerr)?,
257269
None => {
258-
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
259-
"OntoEnv directory not found at: \"{}\"",
260-
root_path.join(".ontoenv").to_string_lossy()
261-
)));
270+
if read_only {
271+
return Err(PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
272+
"OntoEnv directory not found at: \"{}\" and read_only=True",
273+
root_path.join(".ontoenv").to_string_lossy()
274+
)));
275+
}
276+
OntoEnvRs::init(cfg, false).map_err(anyhow_to_pyerr)?
262277
}
263278
}
264279
};

0 commit comments

Comments
 (0)