-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
readFileSync with 'utf-8' option broken by node v20.5.0 #377
Comments
fwiw, anyone who hits this, you can replace Of course you will lose the performance improvements in the nodejs change, so it's probably worth fixing mock-fs. |
Implementing the workaround mentioned in the linked issue. re tschaub/mock-fs#377
Implementing the workaround mentioned in the linked issue. The only downside of this change is losing out on the performance improvements in v20.5. re tschaub/mock-fs#377 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
Unfortunately in my case I depend on a library that uses |
See tschaub/mock-fs#377 Signed-off-by: Chad Wilson <[email protected]>
See tschaub/mock-fs#377 Signed-off-by: Chad Wilson <[email protected]>
See tschaub/mock-fs#377 Signed-off-by: Chad Wilson <[email protected]>
* Use canonical encoding/readFile command to take advantage of fast path in Node 20 Fast path doesn't appear to check encoding options case insensitive, 'utf8' seems more canonical. Signed-off-by: Chad Wilson <[email protected]> * Apply workaround for mock-fs Node 20 issue See tschaub/mock-fs#377 Signed-off-by: Chad Wilson <[email protected]> * Migrate build to Node 20 Node 16 is nearly EOL, and Node 20 is nearly LTS, so let's go straight there. This remove use of a deprecated npm flag, replacing with alternative compatible with npm 7/NodeJS 16 onwards (2021+, Node 16 recently went EOL in Sep 2023). The package-lock is also now only compatible with Node 16/npm 7 onwards. Since this is technically a breaking change in npm/node compatibility, bumps the major version and updates the documentation as such. Signed-off-by: Chad Wilson <[email protected]> --------- Signed-off-by: Chad Wilson <[email protected]>
…her versions (possible related to tschaub/mock-fs#377)
* Fix #57: Bump supported Node.js engine from 4.0.0 to 18.0.0 * Test & update some devDependencies * Fix #58: Remove Gitorious features * Restore missing close parens * Fix #56: Deprecated package warnings Note: some outstanding warnings due to cliffano/bagofrequest#6 * Update Node.js versions to test against * Use 20.4 as the max test version since mock-fs currently fails on higher versions (possible related to tschaub/mock-fs#377) * Remove unused file with output of `npm ls --all` * Update sinon & mock-fs versions
* Fix #57: Bump supported Node.js engine from 4.0.0 to 18.0.0 * Test & update some devDependencies * Fix #58: Remove Gitorious features * Restore missing close parens * Fix #56: Deprecated package warnings Note: some outstanding warnings due to cliffano/bagofrequest#6 * Update Node.js versions to test against * Use 20.4 as the max test version since mock-fs currently fails on higher versions (possible related to tschaub/mock-fs#377) * Remove unused file with output of `npm ls --all` * Update sinon & mock-fs versions
Workaround for tschaub/mock-fs#377, which causes `fs.readFileSync()` to fail on Node.js >=20.5.0. Fixes #2097.
Also removes `fs-extra`, which seems less necessary now than it did back when I added it, and updates a bunch of stuff to use async I/O. If we update all of the output-generating tests to put the output in a temp directory, then I think we could remove `mock-fs` entirely.
It has some nasty, hard-to-fix bugs in current versions of Node.js (tschaub/mock-fs#377, tschaub/mock-fs#380) and an uncertain future (tschaub/mock-fs#383). Also, this package's tests don't really need to mock the entire filesystem; they just need to write files to a temp directory that can be deleted when the tests are complete.
I guess this PR relates to this as well: https://github.com/tschaub/mock-fs/pull/381/files. |
// fsExt.js
import fs from "fs";
function existsSync(path) {
try {
fs.statSync(path);
return true;
} catch (error) {
if(error.code === "ENOENT") {
return false;
} else {
throw error;
}
}
}
function readFileSync(path, encoding) {
return fs.readFileSync(path).toString(encoding);
}
function writeFileSync(path, data) {
const stream = fs.openSync(path, "w");
fs.writeSync(stream, data, 0, "utf8");
fs.closeSync(stream);
}
export {
existsSync,
readFileSync,
writeFileSync
}; // elsewhere in your code where you need above file operations
// import { existsSync, writeFileSync, readFileSync } from "fs";
import { existsSync, writeFileSync, readFileSync } from "./fsExt.js";
... p.s. above code doesn't fully wrap fs functions, it was enough in my case. |
Node v20.5.0 introduced an optimization to
fs.readFileSync
for utf-8 files. Unfortunately, this breaks mock-fs. Here is a minimal test case that works with Node v20.4.0 but breaks with Node v20.5.0:In Node v20.5.0, I get the error
Error: ENOENT: no such file or directory, open 'file'
.The above test case also works in Node v20.5.0 if I remove the
'utf-8'
option fromreadFileSync
. This issue specifically occurs forreadFileSync
with'utf-8'
option.Here is the relevant commit: nodejs/node#48658
The text was updated successfully, but these errors were encountered: