diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index bc4b62d070bf98..bfc413db1a029c 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -1,4 +1,5 @@ import path from 'node:path' +import { execSync } from 'node:child_process' import type * as net from 'node:net' import { get as httpGet } from 'node:http' import { get as httpsGet } from 'node:https' @@ -989,6 +990,26 @@ export function resolveServerOptions( allowDirs = [searchForWorkspaceRoot(root)] } + if (process.versions.pnp) { + try { + const enableGlobalCache = + execSync('yarn config get enableGlobalCache', { cwd: root }) + .toString() + .trim() === 'true' + const yarnCacheDir = execSync( + `yarn config get ${enableGlobalCache ? 'globalFolder' : 'cacheFolder'}`, + { cwd: root }, + ) + .toString() + .trim() + allowDirs.push(yarnCacheDir) + } catch (e) { + logger.warn(`Get yarn cache dir error: ${e.message}`, { + timestamp: true, + }) + } + } + allowDirs = allowDirs.map((i) => resolvedAllowDir(root, i)) // only push client dir when vite itself is outside-of-root