Skip to content

Commit

Permalink
fix(fetch-engine): Prevent macOS from killing node on engine update
Browse files Browse the repository at this point in the history
Fix #14058
  • Loading branch information
SevInf committed Jul 14, 2022
1 parent 8d146db commit dd599aa
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
8 changes: 4 additions & 4 deletions packages/client/src/generation/generateClient.ts
@@ -1,4 +1,4 @@
import { BinaryType } from '@prisma/fetch-engine'
import { BinaryType, overwriteFile } from '@prisma/fetch-engine'
import type { BinaryPaths, DataSource, DMMF, GeneratorConfig } from '@prisma/generator-helper'
import type { Platform } from '@prisma/internals'
import { ClientEngineType, getClientEngineType, getEngineVersion } from '@prisma/internals'
Expand Down Expand Up @@ -275,7 +275,7 @@ export async function generateClient(options: GenerateClientOptions): Promise<vo
// If the target doesn't exist yet, copy it
if (!targetFileSize) {
if (fs.existsSync(filePath)) {
await copyFile(filePath, target)
await overwriteFile(filePath, target)
continue
} else {
throw new Error(`File at ${filePath} is required but was not present`)
Expand All @@ -284,7 +284,7 @@ export async function generateClient(options: GenerateClientOptions): Promise<vo

// If target !== source size, they're definitely different, copy it
if (targetFileSize && sourceFileSize && targetFileSize !== sourceFileSize) {
await copyFile(filePath, target)
await overwriteFile(filePath, target)
continue
}
const binaryName =
Expand All @@ -298,7 +298,7 @@ export async function generateClient(options: GenerateClientOptions): Promise<vo
if (sourceVersion && targetVersion && sourceVersion === targetVersion) {
// skip
} else {
await copyFile(filePath, target)
await overwriteFile(filePath, target)
}
}
}
Expand Down
9 changes: 4 additions & 5 deletions packages/fetch-engine/src/download.ts
Expand Up @@ -16,13 +16,12 @@ import { flatMap } from './flatMap'
import { getHash } from './getHash'
import { getLatestTag } from './getLatestTag'
import { getBar } from './log'
import { getCacheDir, getDownloadUrl } from './util'
import { getCacheDir, getDownloadUrl, overwriteFile } from './util'

const debug = Debug('prisma:download')
const writeFile = promisify(fs.writeFile)
const exists = promisify(fs.exists)
const readFile = promisify(fs.readFile)
const copyFile = promisify(fs.copyFile)
const utimes = promisify(fs.utimes)

const channel = 'master'
Expand Down Expand Up @@ -276,12 +275,12 @@ async function binaryNeedsToBeDownloaded(
// Workaround for https://github.com/prisma/prisma/issues/7037
await utimes(cachedFile, new Date(), new Date())

await copyFile(cachedFile, job.targetFilePath)
await overwriteFile(cachedFile, job.targetFilePath)
}
const targetSha256 = await getHash(job.targetFilePath)
if (sha256File !== targetSha256) {
debug(`overwriting ${job.targetFilePath} with ${cachedFile} as hashes do not match`)
await copyFile(cachedFile, job.targetFilePath)
await overwriteFile(cachedFile, job.targetFilePath)
}
return false
} else {
Expand Down Expand Up @@ -446,7 +445,7 @@ async function saveFileToCache(
const cachedSha256ZippedPath = path.join(cacheDir, job.binaryName + '.gz.sha256')

try {
await copyFile(job.targetFilePath, cachedTargetPath)
await overwriteFile(job.targetFilePath, cachedTargetPath)
await writeFile(cachedSha256Path, sha256)
await writeFile(cachedSha256ZippedPath, zippedSha256)
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion packages/fetch-engine/src/downloadZip.ts
Expand Up @@ -10,6 +10,7 @@ import { promisify } from 'util'
import zlib from 'zlib'

import { getProxyAgent } from './getProxyAgent'
import { overwriteFile } from './util'

const debug = Debug('prisma:downloadZip')
const del = promisify(rimraf)
Expand Down Expand Up @@ -113,7 +114,8 @@ export async function downloadZip(
onFailedAttempt: (err) => debug(err),
},
)
fs.copyFileSync(partial, target)

await overwriteFile(partial, target)

// it's ok if the unlink fails
try {
Expand Down
1 change: 1 addition & 0 deletions packages/fetch-engine/src/index.ts
@@ -1,3 +1,4 @@
export * from './download'
export { getAllUrls, getLatestTag, urlExists } from './getLatestTag'
export { getProxyAgent } from './getProxyAgent'
export { overwriteFile } from './util'
19 changes: 19 additions & 0 deletions packages/fetch-engine/src/util.ts
Expand Up @@ -69,3 +69,22 @@ export function getDownloadUrl(

return `${baseUrl}/${channel}/${version}/${platform}/${binaryName}${finalExtension}`
}

export async function overwriteFile(sourcePath: string, targetPath: string) {
// without removing the file first,
// macOS Gatekeeper can sometimes complain
// about incorrect binary signature and kill node process
// https://openradar.appspot.com/FB8914243
await removeFileIfExists(targetPath)
await fs.promises.copyFile(sourcePath, targetPath)
}

async function removeFileIfExists(filePath: string) {
try {
await fs.promises.unlink(filePath)
} catch (e) {
if (e.code !== 'ENOENT') {
throw e
}
}
}

0 comments on commit dd599aa

Please sign in to comment.