Skip to content

Commit

Permalink
Check and warn for the absence of fs.realpath.native (#953)
Browse files Browse the repository at this point in the history
* Revert "Remove check for fs.realpath.native support, since it's everywhere (#887)"

This reverts commit f4a880d.

* Added warning and fallback support for missing fs.realpath.native

Signed-off-by: Lam Wei Li <peteriman@mail.com>

* Removed NodeJS check and fs.realpath.native fallback

Signed-off-by: Lam Wei Li <peteriman@mail.com>

* Leaving original listener intact during test and better phasing for warning

Signed-off-by: Lam Wei Li <peteriman@mail.com>

* Using done() for proper async test

Signed-off-by: Lam Wei Li <peteriman@mail.com>

* Updated comments for clarity

Co-authored-by: Ryan Zimmerman <17342435+RyanZim@users.noreply.github.com>

Co-authored-by: Ryan Zimmerman <17342435+RyanZim@users.noreply.github.com>
  • Loading branch information
lamweili and RyanZim authored Apr 16, 2022
1 parent e0d298d commit 7bb0120
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
58 changes: 57 additions & 1 deletion lib/fs/__tests__/realpath.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
'use strict'

const fse = require('../..')
const fs = require('fs')
const path = require('path')
const assert = require('assert')

/* eslint-env mocha */

describe('realpath.native does not exist', () => {
let warning
const warningListener = error => {
if (error.name === 'Warning') {
if (error.code.startsWith('fs-extra-WARN0003')) {
warning = error
}
}
}

const realpathNativeBackup = fs.realpath.native
const clearFseCache = () => {
const fsePath = path.dirname(require.resolve('../../..'))
for (const entry in require.cache) {
if (entry.startsWith(fsePath)) {
delete require.cache[entry]
}
}
}

before(() => {
process.on('warning', warningListener)

// clear existing require.cache
clearFseCache()

// simulate fs monkey-patch
delete fs.realpath.native
})

after(() => {
process.off('warning', warningListener)

// clear stubbed require.cache
clearFseCache()

// reinstate fs.realpath.native
fs.realpath.native = realpathNativeBackup
})

it('fse should not export realpath.native', done => {
const fse = require('../..')

// next event loop to allow event emitter/listener to happen
setImmediate(() => {
assert(warning, 'fs-extra-WARN0003 should be emitted')
done()
})

assert(!fse.realpath.native)
})
})

describe('realpath.native', () => {
const fse = require('../..')

it('works with callbacks', () => {
fse.realpath.native(__dirname, (err, path) => {
assert.ifError(err)
Expand Down
11 changes: 10 additions & 1 deletion lib/fs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Object.assign(exports, fs)
api.forEach(method => {
exports[method] = u(fs[method])
})
exports.realpath.native = u(fs.realpath.native)

// We differ from mz/fs in that we still ship the old, broken, fs.exists()
// since we are a drop-in replacement for the native module
Expand Down Expand Up @@ -117,3 +116,13 @@ if (typeof fs.writev === 'function') {
})
}
}

// fs.realpath.native sometimes not available if fs is monkey-patched
if (typeof fs.realpath.native === 'function') {
exports.realpath.native = u(fs.realpath.native)
} else {
process.emitWarning(
'fs.realpath.native is not a function. Is fs being monkey-patched?',
'Warning', 'fs-extra-WARN0003'
)
}

1 comment on commit 7bb0120

@spatial25
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the native error and fixed it with
var fsRealpath = require("fs.realpath")

Please sign in to comment.