diff --git a/CHANGELOG.md b/CHANGELOG.md index cf9a84e8a457..15a500fc0aec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,11 @@ **Fixes**: +- `prqlc fmt` on a file without a `.prql` extension, or a directory with no + `.prql` files, reports that no `.prql` files were found. It previously exited + successfully without formatting anything. `lex`, `debug annotate` and + `experimental highlight` report the same rather than claiming to have found + multiple sources. (@prql-bot, #6587) - `sql.mysql` compiles a milliseconds interval such as `5milliseconds` to `INTERVAL 5000 MICROSECOND`, since MySQL has no `MILLISECOND` unit, and `sql.postgres` compiles a timestamp or time with an offset or `Z` to diff --git a/prqlc/prqlc/src/cli/mod.rs b/prqlc/prqlc/src/cli/mod.rs index 823d4fe0a3fe..19fbaeb26ac7 100644 --- a/prqlc/prqlc/src/cli/mod.rs +++ b/prqlc/prqlc/src/cli/mod.rs @@ -625,6 +625,11 @@ fn read_files(input: &mut clio::ClioPath) -> Result { sources.insert(path, file_contents); } + if sources.is_empty() { + let error = + prqlc::Error::new_simple(format!("No `.prql` files found in `{}`", root.display())); + return Err(prqlc::ErrorMessages::from(error).into()); + } Ok(SourceTree::new(sources, Some(root.to_path_buf()))) } diff --git a/prqlc/prqlc/src/cli/test.rs b/prqlc/prqlc/src/cli/test.rs index c280410a3bd6..d2a2af3d208a 100644 --- a/prqlc/prqlc/src/cli/test.rs +++ b/prqlc/prqlc/src/cli/test.rs @@ -613,7 +613,36 @@ fn compile_no_prql_files() { ----- stdout ----- ----- stderr ----- - Error: No `.prql` files found in the source tree + Error: No `.prql` files found in `README.md` + "); +} + +#[test] +fn no_prql_files() { + let temp_dir = TempDir::new().expect("Failed to create temp directory"); + let txt = temp_dir.path().join("query.txt"); + std::fs::write(&txt, "from tracks\n").unwrap(); + + // `fmt` previously exited successfully without touching the file + assert_cmd_snapshot!(prqlc_command().args(["fmt", "query.txt"]).current_dir(temp_dir.path()), @" + success: false + exit_code: 1 + ----- stdout ----- + + ----- stderr ----- + Error: No `.prql` files found in `query.txt` + "); + assert_eq!(std::fs::read_to_string(&txt).unwrap(), "from tracks\n"); + + // `lex` previously reported finding multiple sources + std::fs::create_dir(temp_dir.path().join("empty")).unwrap(); + assert_cmd_snapshot!(prqlc_command().args(["lex", "empty"]).current_dir(temp_dir.path()), @" + success: false + exit_code: 1 + ----- stdout ----- + + ----- stderr ----- + Error: No `.prql` files found in `empty` "); }