ActivityPub-federated video streaming platform using P2P directly in your web browser
Handle pnpm store directory change
Chocobozzzc61c016eb852
diff --git a/packages/tests/src/plugins/pnpm-store-location.ts b/packages/tests/src/plugins/pnpm-store-location.tsnew file mode 100644index 0000000000..1af12b5614--- /dev/null+++ b/packages/tests/src/plugins/pnpm-store-location.ts@@ -0,0 +1,56 @@+/* oxlint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */++import { cleanupTests, createSingleServer, PeerTubeServer, setAccessTokensToServers } from '@peertube/peertube-server-commands'+import { expect } from 'chai'+import { remove } from 'fs-extra/esm'+import { tmpdir } from 'os'+import { join } from 'path'++describe('Test plugin installation when pnpm store changes', function () {+ let server: PeerTubeServer+ let firstStoreDir: string+ let secondStoreDir: string++ before(async function () {+ this.timeout(90000)++ const id = Date.now()+ firstStoreDir = join(tmpdir(), `peertube-plugin-store-a-${id}`)+ secondStoreDir = join(tmpdir(), `peertube-plugin-store-b-${id}`)++ server = await createSingleServer(1, undefined, {+ env: { pnpm_config_store_dir: firstStoreDir }+ })++ await setAccessTokensToServers([ server ])+ })++ it('Should run pnpm install and retry command if store location changed', async function () {+ this.timeout(90000)++ await server.plugins.install({ npmName: 'peertube-theme-dark' })++ await server.kill()++ await server.run(undefined, {+ env: { pnpm_config_store_dir: secondStoreDir }+ })++ await setAccessTokensToServers([ server ])++ await server.plugins.install({ npmName: 'peertube-plugin-hello-world' })++ await server.servers.waitUntilLog('Cannot exec pnpm because of an unexpected store location.')++ const packageJSON = await server.plugins.getPackageJSON('peertube-plugin-hello-world')++ expect(packageJSON.name).to.equal('peertube-plugin-hello-world')+ })++ after(async function () {+ await cleanupTests([ server ])++ await remove(firstStoreDir)+ await remove(secondStoreDir)+ })+})diff --git a/server/core/lib/plugins/package-manager.ts b/server/core/lib/plugins/package-manager.tsindex 9c4b3398b9..01afbed97c 100644--- a/server/core/lib/plugins/package-manager.ts+++ b/server/core/lib/plugins/package-manager.ts@@ -1,7 +1,7 @@import { isStableOrUnstableVersionValid } from '@server/helpers/custom-validators/misc.js'import { execa } from 'execa'import { outputJSON, pathExists, remove } from 'fs-extra/esm'-import { readFile, writeFile } from 'fs/promises'+import { readFile, rename, writeFile } from 'fs/promises'import { join } from 'path'import { isNpmPluginNameValid } from '../../helpers/custom-validators/plugins.js'import { logger } from '../../helpers/logger.js'@@ -40,17 +40,76 @@ export async function rebuildNativePlugins () {// ---------------------------------------------------------------------------async function execPNPM (commands: string[]) {- try {- const pluginDirectory = CONFIG.STORAGE.PLUGINS_DIR+ const runCommand = () => execa('pnpm', commands, { cwd: CONFIG.STORAGE.PLUGINS_DIR })- return execa('pnpm', commands, { cwd: pluginDirectory })+ try {+ return await runCommand()} catch (result) {- logger.error('Cannot exec pnpm.', { commands, err: result.err, stderr: result.stderr })+ if (isPNPMUnexpectedStoreError(result)) {+ logger.warn('Cannot exec pnpm because of an unexpected store location. Running pnpm install and retrying command.', { commands })++ const nodeModulesPath = join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules')+ const nodeModulesBackupPath = join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules.bak')++ try {+ await rename(nodeModulesPath, nodeModulesBackupPath)++ await execa('pnpm', [ 'install' ], { cwd: CONFIG.STORAGE.PLUGINS_DIR })++ const commandResult = await runCommand()++ remove(nodeModulesBackupPath)+ .catch(err => logger.error('Cannot remove node_modules backup after reinstalling dependencies.', { err }))++ return commandResult+ } catch (retryResult) {+ rename(nodeModulesBackupPath, nodeModulesPath)+ .catch(err => logger.error('Cannot restore node_modules backup after failing to reinstall dependencies.', { err }))++ const err = getErrorFromExecaResult(retryResult)++ logger.error('Cannot recover pnpm command after reinstalling dependencies.', { commands, err, stderr: retryResult?.stderr })++ throw err+ }+ }- throw result.err as Error+ const err = getErrorFromExecaResult(result)++ logger.error('Cannot exec pnpm.', { commands, err, stderr: result?.stderr })++ throw err}}+function getErrorFromExecaResult (result: unknown): Error {+ const execaResult = result as any++ if (execaResult?.err instanceof Error) return execaResult.err+ if (execaResult instanceof Error) return execaResult++ const message = typeof execaResult?.message === 'string'+ ? execaResult.message+ : 'Unknown pnpm execution error'++ return new Error(message)+}++function isPNPMUnexpectedStoreError (result: unknown) {+ const execaResult = result as any++ const output = [+ execaResult?.stdout,+ execaResult?.stderr,+ execaResult?.shortMessage,+ execaResult?.message+ ].filter(value => typeof value === 'string').join('\n')++ return output.includes('ERR_PNPM_UNEXPECTED_STORE') || output.includes('Unexpected store location')+}++// ---------------------------------------------------------------------------+function checkNpmPluginNameOrThrow (name: string) {if (!isNpmPluginNameValid(name)) throw new Error('Invalid NPM plugin name to install')}