Title: readdir(p, { withFileTypes: true }) seemingly returns wrong type · Issue #30646 · nodejs/node · GitHub
Open Graph Title: readdir(p, { withFileTypes: true }) seemingly returns wrong type · Issue #30646 · nodejs/node
X Title: readdir(p, { withFileTypes: true }) seemingly returns wrong type · Issue #30646 · nodejs/node
Description: Version: v13.1.0 Platform: Darwin White-Star.local 18.7.0 Darwin Kernel Version 18.7.0: Sat Oct 12 00:02:19 PDT 2019; root:xnu-4903.278.12~1/RELEASE_X86_64 x86_64 Subsystem: fs When listing a directory with readdir and withFileTypes, at ...
Open Graph Description: Version: v13.1.0 Platform: Darwin White-Star.local 18.7.0 Darwin Kernel Version 18.7.0: Sat Oct 12 00:02:19 PDT 2019; root:xnu-4903.278.12~1/RELEASE_X86_64 x86_64 Subsystem: fs When listing a direc...
X Description: Version: v13.1.0 Platform: Darwin White-Star.local 18.7.0 Darwin Kernel Version 18.7.0: Sat Oct 12 00:02:19 PDT 2019; root:xnu-4903.278.12~1/RELEASE_X86_64 x86_64 Subsystem: fs When listing a direc...
Opengraph URL: https://github.com/nodejs/node/issues/30646
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"readdir(p, { withFileTypes: true }) seemingly returns wrong type","articleBody":"\u003c!--\r\nThank you for reporting a possible bug in Node.js.\r\n\r\nPlease fill in as much of the template below as you can.\r\n\r\nVersion: output of `node -v`\r\nPlatform: output of `uname -a` (UNIX), or version and 32 or 64-bit (Windows)\r\nSubsystem: if known, please specify the affected core module name\r\n\r\nIf possible, please provide code that demonstrates the problem, keeping it as\r\nsimple and free of external dependencies as you can.\r\n-->\r\n\r\n* **Version**: v13.1.0\r\n* **Platform**: Darwin White-Star.local 18.7.0 Darwin Kernel Version 18.7.0: Sat Oct 12 00:02:19 PDT 2019; root:xnu-4903.278.12~1/RELEASE_X86_64 x86_64\r\n* **Subsystem**: fs\r\n\r\n\u003c!-- Please provide more details below this comment. -->\r\n\r\nWhen listing a directory with `readdir` and `withFileTypes`, at least the promisified version of `fs.readdir` returns the wrong type for entities that are symbolic links to directories:\r\n\r\n```javascript\r\nconst { basename, join } = require('path');\r\nconst { readdir, lstat, stat, symlink, unlink } = require('fs').promises;\r\nconst LINK_NAME = basename(__filename) + '-link';\r\nconst LINK_PATH = join(__dirname, LINK_NAME);\r\n\r\nconst inspectEntity = (label, entity) => {\r\n console.log(label, (entity.isDirectory() ? 'dir' : '') +\r\n (entity.isFile() ? 'file' : '') +\r\n (entity.isSymbolicLink() ? 'symlink' : ''));\r\n};\r\n\r\n(async function main() {\r\n try {\r\n await symlink(__dirname, LINK_PATH, 'dir');\r\n\r\n for (const entity of await readdir(__dirname, { withFileTypes: true })) {\r\n if (entity.name === LINK_NAME) inspectEntity('readdir', entity);\r\n }\r\n inspectEntity('lstat', await lstat(LINK_PATH));\r\n inspectEntity('stat', await stat(LINK_PATH));\r\n\r\n await unlink(LINK_PATH);\r\n } catch (x) {\r\n console.error(x.stack);\r\n }\r\n})();\r\n```\r\n\r\nRunning the above script yields the following on my machine:\r\n```\r\nreaddir file\r\nlstat symlink\r\nstat dir\r\n```\r\n\r\nSince the file system entity in question is a symbolic link to a directory, I would expect the code to either report a symlink or a directory, depending on whether the implementation of `readdir` uses `stat` or `lstat` under the covers. But in *neither* case would I expect a file to be reported. Yet that's exactly what's happening on my machine. Am I missing something or is this a genuine bug?\r\n\r\nAdditional testing with a second machine suggests that this may be related to **remote file system** usage. When running the test locally on a local disk, `readdir` reports a symlink (yay!). That stands in contrast to my original testing was performed on a remotely mounted file system.\r\n\r\nAs an aside, after reading a number of old issues before filing this issue, I am guessing that `readdir` uses `stat` not `lstat` under the covers. It would seem more consistent with the rest of the API. Yet the documentation has no disclaimer on `dirent.isSymbolicLink()` as it has on `stats.isSymbolicLink()`. Either way, being more upfront about `readdir`'s behavior `{withFileTypes: true}` would go a long way towards a better developer experience. I'm happy to submit a PR for that—once I understand what's going on above.\r\n\r\nEven with better documentation though, some developers will be disappointed when whatever `readdir` uses does not meet their use case. So I am also wondering whether configurability of `stat`/`lstat` for `readdir` `withFileTypes: true` would be desirable in the long term. If that suggestion falls under the heading \"long-term costs of exposing things from core,\" I apologize. I'm gonna practice writing code *without* using `withFileTypes` for certain tonight. 😜","author":{"url":"https://github.com/apparebit","@type":"Person","name":"apparebit"},"datePublished":"2019-11-25T23:41:35Z","url":"https://github.com/nodejs/node/issues/30646"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:43d10387-984d-4efb-9cd7-2ac4df48f7b0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDAA:2B6F5F:2CDC9AB:3F329A0:6AED2C9C |
| html-safe-nonce | 38767fc7841b88b788b35387eeca00db9061c662d344abcac54fdcdd06fdd1cd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREFBOjJCNkY1RjoyQ0RDOUFCOjNGMzI5QTA6NkFFRDJDOUMiLCJ2aXNpdG9yX2lkIjoiOTE0OTgzOTI5MDg0ODMyNDc2NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 49ca3314dfbe8c7779447c0f69af3e6caa36eacc35fbf65261f97436fbf58192 |
| hovercard-subject-tag | issue:528401439 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/30646/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ff497f30828f5d395c3f3ba81ea1ca7044941117dfa01408fbb424a15ce752df/nodejs/node/issues/30646 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ff497f30828f5d395c3f3ba81ea1ca7044941117dfa01408fbb424a15ce752df/nodejs/node/issues/30646 |
| og:image:alt | Version: v13.1.0 Platform: Darwin White-Star.local 18.7.0 Darwin Kernel Version 18.7.0: Sat Oct 12 00:02:19 PDT 2019; root:xnu-4903.278.12~1/RELEASE_X86_64 x86_64 Subsystem: fs When listing a direc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | apparebit |
| hostname | github.com |
| expected-hostname | github.com |
| None | e51d0b7b5ab34b1688c05b54c3ed17b8658b2fa9bca973a9c740683ccb12cf4a |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 59ebb7b901cc154ecc179a44f266fcf69f343b30 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width