Skip to content

Commit

Permalink
fix(gatsby-source-filesystem): true fix for race condition in gatsbyj…
Browse files Browse the repository at this point in the history
  • Loading branch information
ashk001 committed Mar 24, 2022
1 parent 17b4f55 commit 9559d21
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions packages/gatsby-source-filesystem/src/extend-file-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,37 @@ module.exports = ({
fileName
)

if (!fs.existsSync(publicPath)) {
fs.copySync(
details.absolutePath,
publicPath,
{ dereference: true },
err => {
if (err) {
reporter.panic(
{
id: prefixId(CODES.MissingResource),
context: {
sourceMessage: `error copying file from ${details.absolutePath} to ${publicPath}`,
},
fs.copySync(
details.absolutePath,
publicPath,
{
overwrite: false,
dereference: true,
},
err => {
// Node.js best practices dictate to copy the file and handle the
// error without checking if it exists. Otherwise we introduce a
// race condition.
//
// It is possible we have valid errors from:
// EBUSY: file busy (being transferred)
// EOPEN: file open (being transferred)
// EEXIST: file exists (already transferred)
// What would be a true error and where we should panic:
// ENOENT: file does not exist
if (err && err.code === "ENOENT") {
reporter.panic(
{
id: prefixId(CODES.MissingResource),
context: {
sourceMessage: `error copying file from ${details.absolutePath} to ${publicPath}`,
},
err
)
}
},
err
)
}
)
}
}
)

return `${pathPrefix}/static/${fileName}`
},
Expand Down

0 comments on commit 9559d21

Please sign in to comment.