feat(patching): stop using /tmp for comparison (#9251)

* feat(patching): stop using `/tmp` for comparison

* refactor: use `getStorePath`

* fix: update patching/plugin-commands-patching/src/patchCommit.ts
This commit is contained in:
Khải
2025-03-10 07:52:31 +07:00
committed by GitHub
parent 0ef86c43cd
commit 0378a9a0ff
5 changed files with 59 additions and 9 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-patching": patch
"pnpm": patch
---
`pnpm patch-commit` will now use the same filesystem as the store directory to compare and create patch files.

View File

@@ -35,6 +35,7 @@
"@pnpm/cli-utils": "workspace:*",
"@pnpm/config": "workspace:*",
"@pnpm/constants": "workspace:*",
"@pnpm/crypto.hash": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/fs.packlist": "workspace:*",
"@pnpm/lockfile.fs": "workspace:*",
@@ -48,6 +49,7 @@
"@pnpm/read-package-json": "workspace:*",
"@pnpm/read-project-manifest": "workspace:*",
"@pnpm/store-connection-manager": "workspace:*",
"@pnpm/store-path": "workspace:*",
"@pnpm/types": "workspace:*",
"chalk": "catalog:",
"enquirer": "catalog:",
@@ -60,7 +62,6 @@
"render-help": "catalog:",
"safe-execa": "catalog:",
"semver": "catalog:",
"tempy": "catalog:",
"terminal-link": "catalog:",
"tinyglobby": "catalog:"
},
@@ -78,6 +79,7 @@
"@types/normalize-path": "catalog:",
"@types/ramda": "catalog:",
"@types/semver": "catalog:",
"tempy": "catalog:",
"write-yaml-file": "catalog:"
},
"engines": {

View File

@@ -2,11 +2,14 @@ import fs from 'fs'
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { type Config, types as allTypes } from '@pnpm/config'
import { createShortHash } from '@pnpm/crypto.hash'
import { PnpmError } from '@pnpm/error'
import { packlist } from '@pnpm/fs.packlist'
import { globalWarn } from '@pnpm/logger'
import { install } from '@pnpm/plugin-commands-installation'
import { readPackageJsonFromDir } from '@pnpm/read-package-json'
import { tryReadProjectManifest } from '@pnpm/read-project-manifest'
import { getStorePath } from '@pnpm/store-path'
import { type ProjectRootDir } from '@pnpm/types'
import { glob } from 'tinyglobby'
import normalizePath from 'normalize-path'
@@ -16,8 +19,7 @@ import execa from 'safe-execa'
import escapeStringRegexp from 'escape-string-regexp'
import makeEmptyDir from 'make-empty-dir'
import renderHelp from 'render-help'
import tempy from 'tempy'
import { writePackage } from './writePackage'
import { type WritePackageOptions, writePackage } from './writePackage'
import { type ParseWantedDependencyResult, parseWantedDependency } from '@pnpm/parse-wanted-dependency'
import { type GetPatchedDependencyOptions, getVersionsFromLockfile } from './getPatchedDependency'
import { readEditDirState } from './stateFile'
@@ -79,10 +81,13 @@ export async function handler (opts: PatchCommitCommandOptions, params: string[]
virtualStoreDir: opts.virtualStoreDir,
})
}
const srcDir = tempy.directory()
await writePackage(parseWantedDependency(gitTarballUrl ? `${patchedPkgManifest.name}@${gitTarballUrl}` : nameAndVersion), srcDir, opts)
const patchedPkg = parseWantedDependency(gitTarballUrl ? `${patchedPkgManifest.name}@${gitTarballUrl}` : nameAndVersion)
const patchedPkgDir = await preparePkgFilesForDiff(userDir)
const patchContent = await diffFolders(srcDir, patchedPkgDir)
const patchContent = await getPatchContent({
patchedPkg,
patchedPkgDir,
tmpName: createShortHash(editDir),
}, opts)
if (patchedPkgDir !== userDir) {
fs.rmSync(patchedPkgDir, { recursive: true })
}
@@ -127,6 +132,31 @@ export async function handler (opts: PatchCommitCommandOptions, params: string[]
}) as Promise<undefined>
}
interface GetPatchContentContext {
patchedPkg: ParseWantedDependencyResult
patchedPkgDir: string
tmpName: string
}
type GetPatchContentOptions = Pick<PatchCommitCommandOptions, 'dir' | 'pnpmHomeDir' | 'storeDir'> & WritePackageOptions
async function getPatchContent (ctx: GetPatchContentContext, opts: GetPatchContentOptions): Promise<string> {
const storeDir = await getStorePath({
pkgRoot: opts.dir,
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
})
const srcDir = path.join(storeDir, 'tmp', 'patch-commit', ctx.tmpName)
await writePackage(ctx.patchedPkg, srcDir, opts)
const patchContent = await diffFolders(srcDir, ctx.patchedPkgDir)
try {
fs.rmSync(srcDir, { recursive: true })
} catch (error) {
globalWarn(`Failed to clean up temporary directory at ${srcDir} with error: ${String(error)}`)
}
return patchContent
}
async function diffFolders (folderA: string, folderB: string): Promise<string> {
const folderAN = folderA.replace(/\\/g, '/')
const folderBN = folderB.replace(/\\/g, '/')

View File

@@ -24,6 +24,9 @@
{
"path": "../../config/pick-registry-for-package"
},
{
"path": "../../crypto/hash"
},
{
"path": "../../fetching/pick-fetcher"
},
@@ -66,6 +69,9 @@
{
"path": "../../store/store-connection-manager"
},
{
"path": "../../store/store-path"
},
{
"path": "../../workspace/filter-packages-from-dir"
},

12
pnpm-lock.yaml generated
View File

@@ -4169,6 +4169,9 @@ importers:
'@pnpm/constants':
specifier: workspace:*
version: link:../../packages/constants
'@pnpm/crypto.hash':
specifier: workspace:*
version: link:../../crypto/hash
'@pnpm/error':
specifier: workspace:*
version: link:../../packages/error
@@ -4208,6 +4211,9 @@ importers:
'@pnpm/store-connection-manager':
specifier: workspace:*
version: link:../../store/store-connection-manager
'@pnpm/store-path':
specifier: workspace:*
version: link:../../store/store-path
'@pnpm/types':
specifier: workspace:*
version: link:../../packages/types
@@ -4244,9 +4250,6 @@ importers:
semver:
specifier: 'catalog:'
version: 7.7.1
tempy:
specifier: 'catalog:'
version: 1.0.1
terminal-link:
specifier: 'catalog:'
version: 2.1.1
@@ -4284,6 +4287,9 @@ importers:
'@types/semver':
specifier: 'catalog:'
version: 7.5.3
tempy:
specifier: 'catalog:'
version: 1.0.1
write-yaml-file:
specifier: 'catalog:'
version: 5.0.0