Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"command": "uv run python -m pytest tests/e2e/fanout_test.py tests/e2e/fresh_declared_repros_test.py tests/e2e/extension_package_test.py tests/e2e/parent_then_child_test.py tests/e2e/on_file_query_test.py tests/e2e/parent_then_child_server_test.py tests/e2e/plugin_discovery_test.py tests/e2e/on_file_value_types_test.py tests/e2e/interpret_removed_test.py tests/e2e/orphan_query_test.py tests/e2e/on_file_failure_test.py tests/e2e/declared_table_name_test.py tests/e2e/use_case_dossier_test.py tests/e2e/plugin_extension_discovery_test.py tests/e2e/relative_root_chdir_test.py tests/e2e/root_at_construction_test.py tests/e2e/written_during_build_test.py tests/e2e/query_contract_test.py tests/e2e/use_case_join_test.py tests/e2e/use_case_transcripts_test.py -x -q",
"ran_at": 1795026128,
"exit_code": 0,
"commit": "4c3d8a17a12240c7e4f93321c4696ddb67365cf4",
"branch": "bot/1771-missing-index-root-1118"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import os
import re

import pytest

Expand Down Expand Up @@ -45,3 +46,14 @@ async def it_resolves_a_relative_root_against_the_cwd_of_construction(
rows = await db.query("SELECT path FROM './**'")

assert rows == [{"path": "a.json"}]

@pytest.mark.asyncio
async def it_raises_when_the_root_does_not_exist(tmp_path):
missing = tmp_path / "absent"
db = DirSQL(str(missing))

with pytest.raises(
Exception,
match=rf"^root directory {re.escape(str(missing))} does not exist$",
):
await db.ready()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**Changed**

- **An index root that does not exist now fails the build**, with `root directory <dir> does not exist`. Before, the build succeeded with no file watcher, so the instance stayed empty and later files under the root went unseen. Every SDK raises the error.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### A missing index root fails the build

#### Summary

Building a `DirSQL` (Rust, Python, TypeScript) whose index root does not exist now fails with `root directory <dir> does not exist`. Builds that used to succeed over a missing root now fail. The CLI always roots at the current directory, so it is unaffected.

#### Required changes

| Before | After |
| --- | --- |
| `DirSQL("/path/not/yet/created")` built an empty instance | Create the directory before building |

#### Deprecations removed

_None._

#### Behavior changes without code changes

The build raises instead of succeeding. Before, it also started no file watcher, so files created under the root later went unseen.

#### Verification

In Python, `await DirSQL("/no/such/dir").ready()` inside a coroutine raises `RuntimeError: root directory /no/such/dir does not exist`.
41 changes: 34 additions & 7 deletions packages/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ pub enum DirSqlError {
#[error("duplicate table name: {0}")]
DuplicateTable(String),

#[error("root directory {} does not exist", .0.display())]
MissingRoot(PathBuf),

#[error("table '{table}': its directory {} does not exist", .dir.display())]
MissingAnchor { table: String, dir: PathBuf },

Expand Down Expand Up @@ -573,6 +576,16 @@ impl FreshState {
}
}

/// Fails when `exists` says the index root is missing: nothing above it is
/// watched, so it could never be seen to appear.
fn check_root(root: &Path, exists: &dyn Fn(&Path) -> bool) -> Result<()> {
if exists(root) {
Ok(())
} else {
Err(DirSqlError::MissingRoot(root.to_path_buf()))
}
}

/// Fails on the first table whose own anchor `exists` says is missing:
/// nothing watches above an anchor, so it could never be seen to appear.
fn check_anchors(tables: &[Table], exists: &dyn Fn(&Path) -> bool) -> Result<()> {
Expand Down Expand Up @@ -1853,9 +1866,10 @@ impl DirSQL {
}

/// Test-seam build path: identical to [`build_from_resolved`] but stores
/// the supplied [`FileSystem`] double on the resulting instance. The
/// prepare phase still uses [`RealFs`] (it has no instance yet), so
/// callers must build over an empty temp dir.
/// the supplied [`FileSystem`] double on the resulting instance, and it
/// skips the root check, so a double may stand in for a root that is not
/// on disk. The prepare phase still uses [`RealFs`] (it has no instance
/// yet), so callers must build over an empty or missing directory.
#[cfg(test)]
pub(crate) fn with_ignore_and_fs<I, S>(
root: impl Into<PathBuf>,
Expand All @@ -1881,7 +1895,7 @@ impl DirSQL {
no_ignore: false,
no_watch: false,
};
let prepared = Self::prepare_resolved(resolved)?;
let prepared = Self::prepare_resolved_with(resolved, &scan_directory_reporting)?;
Self::finish_build_with_fs(prepared, fs, Progress::indexing())
}

Expand All @@ -1896,6 +1910,7 @@ impl DirSQL {
/// JS main thread for the napi-rs TypeScript binding).
#[doc(hidden)]
pub fn prepare_resolved(resolved: ResolvedBuild) -> Result<PreparedBuild> {
check_root(&resolved.root, &|root| root.exists())?;
Self::prepare_resolved_with(resolved, &scan_directory_reporting)
}

Expand Down Expand Up @@ -1971,8 +1986,8 @@ impl DirSQL {
} else {
(HashMap::new(), Vec::new())
};
// A watcher that cannot start yet (the index root not created) does
// not fail the build; watching starts it.
// A watcher whose directory vanished after the checks above does not
// fail the build; watching starts it.
let watcher = if table_names.is_empty() || no_watch {
None
} else {
Expand Down Expand Up @@ -8286,6 +8301,18 @@ mod internal_tests {
assert_eq!(err.to_string(), "watcher error: denied");
}

#[test]
fn check_root_names_a_missing_root() {
let err = check_root(Path::new("/gone"), &|_| false).unwrap_err();

assert_eq!(err.to_string(), "root directory /gone does not exist");
}

#[test]
fn check_root_passes_an_existing_root() {
assert!(check_root(Path::new("/here"), &|root| root == Path::new("/here")).is_ok());
}

#[test]
fn check_anchors_names_the_first_table_whose_anchor_is_missing() {
let tables = vec![
Expand Down Expand Up @@ -8314,7 +8341,7 @@ mod internal_tests {
}

#[test]
fn a_build_over_a_missing_index_root_starts_no_watcher() {
fn a_build_whose_watched_directory_vanished_starts_no_watcher() {
assert_eq!(
watcher_at_build::<()>(Err(path_not_found_error())).unwrap(),
None
Expand Down
30 changes: 1 addition & 29 deletions packages/rust/tests/code_review_findings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Tests for code-review findings; each test name carries its finding ID.

use dirsql::{DirSQL, DirSqlError, Row, Table, Value};
use std::error::Error as _;
use dirsql::{DirSQL, Row, Table, Value};
use std::time::Duration;
use tempfile::TempDir;

Expand Down Expand Up @@ -141,33 +140,6 @@ fn p7_builder_exposes_poll_interval() {
.unwrap();
}

#[test]
fn i3_watch_error_exposes_underlying_source() {
let dir = TempDir::new().unwrap();
let missing = dir.path().join("missing");
let db = DirSQL::new(
&missing,
vec![Table::new(
"t",
"CREATE TABLE t (id TEXT)",
"*.json",
|_| vec![],
)],
)
.unwrap();
let err = db
.poll_events(std::time::Duration::ZERO)
.expect_err("watching a missing root must fail");
match &err {
DirSqlError::Watch { .. } => {}
other => panic!("expected DirSqlError::Watch, got: {other:?}"),
}
assert!(
err.source().is_some(),
"DirSqlError::Watch must expose its underlying source (got None)"
);
}

#[test]
fn i6_parser_versions_json_no_longer_lists_removed_parsers() {
let s = dirsql::persist::PARSER_VERSIONS_JSON;
Expand Down
41 changes: 41 additions & 0 deletions packages/rust/tests/missing_root.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! An index root that does not exist fails the build, naming the root.

use dirsql::{DirSQL, Table};
use tempfile::TempDir;

fn table() -> Table {
Table::new("t", "CREATE TABLE t (x TEXT)", "*.json", |_| vec![])
}

fn build_error(result: dirsql::Result<DirSQL>) -> String {
match result {
Ok(_) => panic!("the build succeeded"),
Err(e) => e.to_string(),
}
}

#[test]
fn a_missing_root_fails_the_build() {
let dir = TempDir::new().unwrap();
let missing = dir.path().join("absent");

let err = build_error(DirSQL::new(&missing, vec![table()]));

assert_eq!(
err,
format!("root directory {} does not exist", missing.display())
);
}

#[test]
fn a_missing_root_fails_the_build_with_no_tables() {
let dir = TempDir::new().unwrap();
let missing = dir.path().join("absent");

let err = build_error(DirSQL::builder().root(&missing).build());

assert_eq!(
err,
format!("root directory {} does not exist", missing.display())
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"command": "pnpm test:e2e",
"ran_at": 1795024670,
"exit_code": 0,
"commit": "406eb2434f15c9a5ab61164f553eaeb9dfb449b9",
"branch": "bot/1771-missing-index-root-1118"
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ describe("root at construction", () => {
{ path: "a.json" },
]);
});

it("rejects a root that does not exist, naming it", async () => {
const missing = join(dir, "absent");
const db = new DirSQL({ root: missing });

await expect(db.ready).rejects.toThrow(
`root directory ${missing} does not exist`,
);
});
});
Loading