Skip to content
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

fs.writeFileSync(path) literaly writes "undefined" to the file #31025

Closed
aalexgabi opened this issue Dec 19, 2019 · 2 comments
Closed

fs.writeFileSync(path) literaly writes "undefined" to the file #31025

aalexgabi opened this issue Dec 19, 2019 · 2 comments
Labels
confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system.

Comments

@aalexgabi
Copy link

When executing:

require('fs').writeFileSync('/tmp/empty-file');

It creates a file containing "undefined" string:

% hexdump -C /tmp/empty-file
00000000  75 6e 64 65 66 69 6e 65  64                       |undefined|
00000009

This is unexpected behavior as I just wanted to create an empty file. I expect it to either write an empty file or throw but in no case have "undefined" written as the file content.

@pd4d10
Copy link
Contributor

pd4d10 commented Dec 19, 2019

Perhaps this is what you expected?

require('fs').writeFileSync('/tmp/empty-file', '')

@Trott
Copy link
Member

Trott commented Dec 19, 2019

The documentation for writeFileSync() indicates that the second argument is not optional. So undefined is correct in a way, but yeah, that's surprising, and I would expect it to either throw an error or create an empty file instead.

@BridgeAR BridgeAR added confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system. labels Dec 20, 2019
BridgeAR added a commit to BridgeAR/node that referenced this issue Jan 13, 2020
The input was not validated so far and that caused unwanted side
effects. E.g., `undefined` became the string `'undefined'`. It was
expected to fail or to end up as empty string.
Now all input is validated to be either some type of array buffer
view or a string. That way it's always clear what the user intents.

Fixes: nodejs#31025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. fs Issues and PRs related to the fs subsystem / file system.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants