diff --git a/Cargo.lock b/Cargo.lock index 7947106e66..6e929168f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3086,9 +3086,9 @@ checksum = "c007b1ae3abe1cb6f85a16305acd418b7ca6343b953633fee2b76d8f108b830f" [[package]] name = "file_test_runner" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "05b23dcc1b671771c6f59fdace6da685735c925f859733e8fd07fba6cae6462a" +checksum = "cf50901549edf2241e33d1715aec0575adc5510a09724877a1e0afe7ffafb0fb" dependencies = [ "anyhow", "crossbeam-channel", diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index c221a6097b..ae87a9914f 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -4454,11 +4454,7 @@ fn op_load<'s>( == NodeModuleKind::Cjs, }) }; - - lsp_warn!("op_load {} {}", &specifier, maybe_load_response.is_some()); - let serialized = serde_v8::to_v8(scope, maybe_load_response)?; - state.performance.measure(mark); Ok(serialized) } diff --git a/cli/standalone/virtual_fs.rs b/cli/standalone/virtual_fs.rs index d1084f016c..be7e937ee1 100644 --- a/cli/standalone/virtual_fs.rs +++ b/cli/standalone/virtual_fs.rs @@ -634,7 +634,7 @@ impl FileBackedVfsFile { } fn read_to_buf(&self, buf: &mut [u8]) -> FsResult { - let pos = { + let read_pos = { let mut pos = self.pos.lock(); let read_pos = *pos; // advance the position due to the read @@ -643,12 +643,12 @@ impl FileBackedVfsFile { }; self .vfs - .read_file(&self.file, pos, buf) + .read_file(&self.file, read_pos, buf) .map_err(|err| err.into()) } fn read_to_end(&self) -> FsResult> { - let pos = { + let read_pos = { let mut pos = self.pos.lock(); let read_pos = *pos; // todo(dsherret): should this always set it to the end of the file? @@ -658,12 +658,12 @@ impl FileBackedVfsFile { } read_pos }; - if pos > self.file.len { + if read_pos > self.file.len { return Ok(Vec::new()); } - let size = (self.file.len - pos) as usize; + let size = (self.file.len - read_pos) as usize; let mut buf = vec![0; size]; - self.vfs.read_file(&self.file, pos, &mut buf)?; + self.vfs.read_file(&self.file, read_pos, &mut buf)?; Ok(buf) } } @@ -893,8 +893,9 @@ impl FileBackedVfs { buf: &mut [u8], ) -> std::io::Result { let read_range = self.get_read_range(file, pos, buf.len() as u64)?; - buf.copy_from_slice(&self.vfs_data[read_range]); - Ok(buf.len()) + let read_len = read_range.len(); + buf[..read_len].copy_from_slice(&self.vfs_data[read_range]); + Ok(read_len) } fn get_read_range( @@ -903,15 +904,15 @@ impl FileBackedVfs { pos: u64, len: u64, ) -> std::io::Result> { - let data = &self.vfs_data; - let start = self.fs_root.start_file_offset + file.offset + pos; - let end = start + len; - if end > data.len() as u64 { + if pos > file.len { return Err(std::io::Error::new( std::io::ErrorKind::UnexpectedEof, "unexpected EOF", )); } + let file_offset = self.fs_root.start_file_offset + file.offset; + let start = file_offset + pos; + let end = file_offset + std::cmp::min(pos + len, file.len); Ok(start as usize..end as usize) } diff --git a/cli/tools/fmt.rs b/cli/tools/fmt.rs index 9c2c709129..c2c2a6bb6b 100644 --- a/cli/tools/fmt.rs +++ b/cli/tools/fmt.rs @@ -549,7 +549,11 @@ pub fn format_sql( // Add single new line to the end of file. formatted_str.push('\n'); - Ok(Some(formatted_str)) + Ok(if formatted_str == file_text { + None + } else { + Some(formatted_str) + }) } /// Formats a single TS, TSX, JS, JSX, JSONC, JSON, MD, IPYNB or SQL file. diff --git a/ext/node/polyfills/_fs/_fs_readFile.ts b/ext/node/polyfills/_fs/_fs_readFile.ts index cf7e0305d8..029e57c502 100644 --- a/ext/node/polyfills/_fs/_fs_readFile.ts +++ b/ext/node/polyfills/_fs/_fs_readFile.ts @@ -88,7 +88,7 @@ export function readFile( } const buffer = maybeDecode(data, encoding); (cb as BinaryCallback)(null, buffer); - }, (err) => cb && cb(denoErrorToNodeError(err))); + }, (err) => cb && cb(denoErrorToNodeError(err, { path, syscall: "open" }))); } } @@ -122,7 +122,7 @@ export function readFileSync( try { data = Deno.readFileSync(path); } catch (err) { - throw denoErrorToNodeError(err); + throw denoErrorToNodeError(err, { path, syscall: "open" }); } const encoding = getEncoding(opt); if (encoding && encoding !== "binary") { diff --git a/tests/Cargo.toml b/tests/Cargo.toml index ede509e380..31cc022ce2 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -45,7 +45,7 @@ deno_semver.workspace = true deno_terminal.workspace = true deno_tls.workspace = true fastwebsockets = { workspace = true, features = ["upgrade", "unstable-split"] } -file_test_runner = "0.7.2" +file_test_runner = "0.7.3" flaky_test = "=0.2.2" hickory-client = "=0.24" hickory-server = "=0.24" diff --git a/tests/specs/compile/include/buffered_reads/__test__.jsonc b/tests/specs/compile/include/buffered_reads/__test__.jsonc new file mode 100644 index 0000000000..7640fed56b --- /dev/null +++ b/tests/specs/compile/include/buffered_reads/__test__.jsonc @@ -0,0 +1,27 @@ +{ + "tempDir": true, + "steps": [{ + "args": "run -A setup.js", + "output": "[WILDCARD]" + }, { + "if": "unix", + "args": "compile --allow-read=data --include data --output main main.ts", + "output": "[WILDCARD]" + }, { + "if": "unix", + "commandName": "./main", + "args": [], + "output": "[WILDCARD]", + "exitCode": 0 + }, { + "if": "windows", + "args": "compile --allow-read=data --include data --output main.exe main.ts", + "output": "[WILDCARD]" + }, { + "if": "windows", + "commandName": "./main.exe", + "args": [], + "output": "[WILDCARD]", + "exitCode": 0 + }] +} diff --git a/tests/specs/compile/include/buffered_reads/main.ts b/tests/specs/compile/include/buffered_reads/main.ts new file mode 100644 index 0000000000..7f78eab612 --- /dev/null +++ b/tests/specs/compile/include/buffered_reads/main.ts @@ -0,0 +1,57 @@ +// buffer larger than file +{ + using file = Deno.openSync(import.meta.dirname + "/data/1.txt"); + const data = new Uint8Array(13); + const len = file.readSync(data); + if (len !== 13) { + throw new Error("Unexpected read length"); + } + if (file.readSync(new Uint8Array(1024)) !== null) { + throw new Error("Unexpected."); + } + const textData = new TextDecoder().decode(data); + if (textData !== "Hello, world!") { + throw new Error("Unexpected file data (1): " + textData); + } +} + +// buffer smaller than file +{ + using file = Deno.openSync(import.meta.dirname + "/data/1.txt"); + const finalData = new Uint8Array(13); + const data = new Uint8Array(2); + let pos = 0; + while (true) { + const len = file.readSync(data); + if (len === 0 || len == null) { + break; + } + finalData.set(data.subarray(0, len), pos); + pos += len; + } + const textData = new TextDecoder().decode(finalData); + if (textData !== "Hello, world!") { + throw new Error("Unexpected file data (2): " + textData); + } +} + +// large amount of data, small reads +{ + const bytes = new Uint8Array((1024 ** 2) * 20); + using file = Deno.openSync(import.meta.dirname + "/data/2.dat"); + const buffer = new Uint8Array(2); + let pos = 0; + while (true) { + const len = file.readSync(buffer); + if (len === 0 || len == null) { + break; + } + bytes.set(buffer.subarray(0, len), pos); + pos += len; + } + for (let i = 0; i < bytes.length; i++) { + if (bytes[i] !== i % 256) { + throw new Error("Unexpected data."); + } + } +} diff --git a/tests/specs/compile/include/buffered_reads/setup.js b/tests/specs/compile/include/buffered_reads/setup.js new file mode 100644 index 0000000000..39cd5cb8b0 --- /dev/null +++ b/tests/specs/compile/include/buffered_reads/setup.js @@ -0,0 +1,7 @@ +Deno.mkdirSync("data"); +Deno.writeTextFileSync("data/1.txt", "Hello, world!"); +const bytes = new Uint8Array((1024 ** 2) * 20); +for (let i = 0; i < bytes.length; i++) { + bytes[i] = i % 256; +} +Deno.writeFileSync("data/2.dat", bytes); diff --git a/tests/specs/compile/include_data_files/__test__.jsonc b/tests/specs/compile/include/data_files/__test__.jsonc similarity index 100% rename from tests/specs/compile/include_data_files/__test__.jsonc rename to tests/specs/compile/include/data_files/__test__.jsonc diff --git a/tests/specs/compile/include_data_files/data-file.txt b/tests/specs/compile/include/data_files/data-file.txt similarity index 100% rename from tests/specs/compile/include_data_files/data-file.txt rename to tests/specs/compile/include/data_files/data-file.txt diff --git a/tests/specs/compile/include_data_files/main.js b/tests/specs/compile/include/data_files/main.js similarity index 100% rename from tests/specs/compile/include_data_files/main.js rename to tests/specs/compile/include/data_files/main.js diff --git a/tests/specs/compile/include_data_files/non_existent.out b/tests/specs/compile/include/data_files/non_existent.out similarity index 100% rename from tests/specs/compile/include_data_files/non_existent.out rename to tests/specs/compile/include/data_files/non_existent.out diff --git a/tests/specs/compile/include_data_files/output.out b/tests/specs/compile/include/data_files/output.out similarity index 100% rename from tests/specs/compile/include_data_files/output.out rename to tests/specs/compile/include/data_files/output.out diff --git a/tests/specs/compile/include_folder/__test__.jsonc b/tests/specs/compile/include/folder/__test__.jsonc similarity index 100% rename from tests/specs/compile/include_folder/__test__.jsonc rename to tests/specs/compile/include/folder/__test__.jsonc diff --git a/tests/specs/compile/include_folder/data/a.txt b/tests/specs/compile/include/folder/data/a.txt similarity index 100% rename from tests/specs/compile/include_folder/data/a.txt rename to tests/specs/compile/include/folder/data/a.txt diff --git a/tests/specs/compile/include_folder/data/b.txt b/tests/specs/compile/include/folder/data/b.txt similarity index 100% rename from tests/specs/compile/include_folder/data/b.txt rename to tests/specs/compile/include/folder/data/b.txt diff --git a/tests/specs/compile/include_folder/main.js b/tests/specs/compile/include/folder/main.js similarity index 100% rename from tests/specs/compile/include_folder/main.js rename to tests/specs/compile/include/folder/main.js diff --git a/tests/specs/compile/include_folder/output.out b/tests/specs/compile/include/folder/output.out similarity index 100% rename from tests/specs/compile/include_folder/output.out rename to tests/specs/compile/include/folder/output.out diff --git a/tests/specs/fmt/sql/__test__.jsonc b/tests/specs/fmt/sql/__test__.jsonc index a335e79c24..27c08abd83 100644 --- a/tests/specs/fmt/sql/__test__.jsonc +++ b/tests/specs/fmt/sql/__test__.jsonc @@ -7,7 +7,7 @@ }, "flag": { "args": "fmt --unstable-sql", - "output": "[UNORDERED_START]\n[WILDLINE]badly_formatted.sql\n[WILDLINE]well_formatted.sql\n[WILDLINE]wrong_file_ignore.sql\n[UNORDERED_END]\nChecked 7 files\n" + "output": "[UNORDERED_START]\n[WILDLINE]badly_formatted.sql\n[WILDLINE]wrong_file_ignore.sql\n[UNORDERED_END]\nChecked 7 files\n" }, "config_file": { "steps": [{ @@ -18,8 +18,12 @@ "output": "[WILDCARD]" }, { "args": "fmt", - "output": "[UNORDERED_START]\n[WILDLINE]badly_formatted.sql\n[WILDLINE]well_formatted.sql\n[WILDLINE]wrong_file_ignore.sql\n[UNORDERED_END]\nChecked 8 files\n" + "output": "[UNORDERED_START]\n[WILDLINE]badly_formatted.sql\n[WILDLINE]wrong_file_ignore.sql\n[UNORDERED_END]\nChecked 8 files\n" }] + }, + "well_formatted_check": { + "args": "fmt --unstable-sql --check well_formatted.sql", + "output": "Checked 1 file\n" } } } diff --git a/tests/unit_node/_fs/_fs_readFile_test.ts b/tests/unit_node/_fs/_fs_readFile_test.ts index ea36b9d866..a75f12d1f6 100644 --- a/tests/unit_node/_fs/_fs_readFile_test.ts +++ b/tests/unit_node/_fs/_fs_readFile_test.ts @@ -2,7 +2,7 @@ import { assertCallbackErrorUncaught } from "../_test_utils.ts"; import { promises, readFile, readFileSync } from "node:fs"; import * as path from "@std/path"; -import { assert, assertEquals } from "@std/assert"; +import { assert, assertEquals, assertMatch } from "@std/assert"; const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); const testData = path.resolve(moduleDir, "testdata", "hello.txt"); @@ -121,3 +121,26 @@ Deno.test("fs.promises.readFile with no arg call rejects with error correctly", // @ts-ignore no arg call needs to be supported await promises.readFile().catch((_e) => {}); }); + +Deno.test("fs.readFile error message contains path + syscall", async () => { + const path = "/does/not/exist"; + const err = await new Promise((resolve) => { + readFile(path, "utf-8", (err) => resolve(err)); + }); + if (err instanceof Error) { + assert(err.message.includes(path), "Path not found in error message"); + assertMatch(err.message, /[,\s]open\s/); + } +}); + +Deno.test("fs.readFileSync error message contains path + syscall", () => { + const path = "/does/not/exist"; + try { + readFileSync(path, "utf-8"); + } catch (err) { + if (err instanceof Error) { + assert(err.message.includes(path), "Path not found in error message"); + assertMatch(err.message, /[,\s]open\s/); + } + } +});