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

fix(gatsby-transformer-sharp): prevent duplicate copy of the same file #20620

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/gatsby-transformer-sharp/src/customize-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ const fluidNodeType = ({
}
}

/**
* Keeps track of asynchronous file copy to prevent sequence errors in the
* underlying fs-extra module during parallel copies of the same file
*/
const inProgressCopy = new Set()

const createFields = ({
pathPrefix,
getNodeAndSavePathDependency,
Expand Down Expand Up @@ -428,8 +434,16 @@ const createFields = ({
imageName
)

if (!fsExtra.existsSync(publicPath)) {
if (
!fsExtra.existsSync(publicPath) &&
!inProgressCopy.has(publicPath)
) {
// keep track of in progress copy, we should rely on `existsSync` but
// a race condition exists between the exists check and the copy
inProgressCopy.add(publicPath)
fsExtra.copy(details.absolutePath, publicPath, err => {
// this is no longer in progress
inProgressCopy.delete(publicPath)
if (err) {
console.error(
`error copying file from ${details.absolutePath} to ${publicPath}`,
Expand Down