-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
fix(installer): replace fs-extra with native node:fs to prevent file loss #2253
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // Drop-in replacement for fs-extra using native node:fs APIs. | ||
| // Eliminates graceful-fs monkey-patching that causes non-deterministic | ||
| // file loss during multi-module installs on macOS (issue #1779). | ||
| const fsp = require('node:fs/promises'); | ||
| const fs = require('node:fs'); | ||
| const path = require('node:path'); | ||
|
|
||
| async function pathExists(p) { | ||
| try { | ||
| await fsp.access(p); | ||
| return true; | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| async function ensureDir(dir) { | ||
| await fsp.mkdir(dir, { recursive: true }); | ||
| } | ||
|
|
||
| async function remove(p) { | ||
| await fsp.rm(p, { recursive: true, force: true }); | ||
| } | ||
|
|
||
| async function copy(src, dest, options = {}) { | ||
| const filterFn = options.filter; | ||
| const overwrite = options.overwrite !== false; | ||
| const srcStat = await fsp.stat(src); | ||
|
|
||
| if (srcStat.isFile()) { | ||
| if (filterFn && !(await filterFn(src, dest))) return; | ||
| await fsp.mkdir(path.dirname(dest), { recursive: true }); | ||
| if (!overwrite) { | ||
| try { | ||
| await fsp.access(dest); | ||
| if (options.errorOnExist) throw new Error(`${dest} already exists`); | ||
| return; | ||
| } catch (error) { | ||
| if (error.message.includes('already exists')) throw error; | ||
| } | ||
| } | ||
| await fsp.copyFile(src, dest); | ||
| return; | ||
| } | ||
|
|
||
| if (srcStat.isDirectory()) { | ||
| if (filterFn && !(await filterFn(src, dest))) return; | ||
| await fsp.mkdir(dest, { recursive: true }); | ||
| const entries = await fsp.readdir(src, { withFileTypes: true }); | ||
| for (const entry of entries) { | ||
| await copy(path.join(src, entry.name), path.join(dest, entry.name), options); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function move(src, dest) { | ||
| try { | ||
| await fsp.rename(src, dest); | ||
| } catch (error) { | ||
| if (error.code === 'EXDEV') { | ||
| await copy(src, dest); | ||
| await fsp.rm(src, { recursive: true, force: true }); | ||
| } else { | ||
| throw error; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function readJsonSync(p) { | ||
| return JSON.parse(fs.readFileSync(p, 'utf8')); | ||
| } | ||
|
|
||
| async function writeJson(p, data, options = {}) { | ||
| const spaces = options.spaces ?? 2; | ||
| await fsp.writeFile(p, JSON.stringify(data, null, spaces) + '\n', 'utf8'); | ||
| } | ||
|
|
||
| module.exports = { | ||
|
bmadcode marked this conversation as resolved.
|
||
| // Native async (node:fs/promises) | ||
| readFile: fsp.readFile, | ||
| writeFile: fsp.writeFile, | ||
| stat: fsp.stat, | ||
| readdir: fsp.readdir, | ||
| access: fsp.access, | ||
| rename: fsp.rename, | ||
| unlink: fsp.unlink, | ||
| chmod: fsp.chmod, | ||
| mkdir: fsp.mkdir, | ||
| mkdtemp: fsp.mkdtemp, | ||
| copyFile: fsp.copyFile, | ||
| rm: fsp.rm, | ||
|
|
||
| // fs-extra compatible helpers (native implementations) | ||
| pathExists, | ||
| ensureDir, | ||
| remove, | ||
| copy, | ||
| move, | ||
| readJsonSync, | ||
| writeJson, | ||
|
|
||
| // Sync methods from core node:fs | ||
| existsSync: fs.existsSync.bind(fs), | ||
| readFileSync: fs.readFileSync.bind(fs), | ||
| writeFileSync: fs.writeFileSync.bind(fs), | ||
| createReadStream: fs.createReadStream.bind(fs), | ||
| pathExistsSync: fs.existsSync.bind(fs), | ||
|
|
||
| // Constants | ||
| constants: fs.constants, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.