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

feat: the global store location should be inside the pnpm home directory #4522

Merged
merged 5 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: the global store location should be inside the pnpm home directory
  • Loading branch information
zkochan committed Apr 2, 2022
commit 2a98d39f269c1631b50188f873cdbc8a4e1c427a
1 change: 1 addition & 0 deletions packages/mount-modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"@pnpm/cafs": "workspace:4.0.0",
"@pnpm/config": "workspace:14.0.0",
"@pnpm/lockfile-file": "workspace:5.0.0",
"@pnpm/lockfile-utils": "workspace:4.0.0",
"@pnpm/store-path": "workspace:5.0.0",
Expand Down
11 changes: 10 additions & 1 deletion packages/mount-modules/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import getConfig from '@pnpm/config'
import { promises as fs } from 'fs'
import path from 'path'

Expand All @@ -7,7 +8,15 @@ import createFuseHandlers from './createFuseHandlers'
(async () => { /* eslint-disable-line */
const mnt = path.join(process.cwd(), 'node_modules')
await fs.mkdir(mnt, { recursive: true })
const cafsDir = path.join(await getStorePath(process.cwd()), 'files')
const { config } = await getConfig({
cliOptions: {},
packageManager: { name: '', version: '' },
})
const cafsDir = path.join(await getStorePath({
pkgRoot: process.cwd(),
storePath: config.storeDir,
pnpmHomeDir: config.pnpmHomeDir,
}), 'files')
const fuse = new Fuse(mnt, await createFuseHandlers(process.cwd(), cafsDir), { debug: true })
fuse.mount(function (err?: Error) {
if (err != null) console.error(err)
Expand Down
3 changes: 3 additions & 0 deletions packages/mount-modules/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
{
"path": "../cafs"
},
{
"path": "../config"
},
{
"path": "../dependency-path"
},
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin-commands-env/src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ async function installNode (fetch: FetchFromRegistry, wantedNodeVersion: string,
},
timeout: opts.fetchTimeout,
})
const storeDir = await storePath(process.cwd(), opts.storeDir)
const storeDir = await storePath({
pkgRoot: process.cwd(),
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
})
const cafsDir = path.join(storeDir, 'files')
const cafs = createCafsStore(cafsDir)
const { filesIndex } = await fetchTarball(cafs, { tarball }, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface StrictRebuildOptions {
useLockfile: boolean
registries: Registries
dir: string
pnpmHomeDir: string

reporter: (logObj: LogBase) => void
production: boolean
Expand Down
6 changes: 5 additions & 1 deletion packages/plugin-commands-server/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export default async (
if (opts.background && !Diable.isDaemon()) {
Diable()
}
const storeDir = await storePath(opts.dir, opts.storeDir)
const storeDir = await storePath({
pkgRoot: opts.dir,
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
})
const connectionInfoDir = serverConnectionInfoDir(storeDir)
const serverJsonPath = path.join(connectionInfoDir, 'server.json')
await fs.mkdir(connectionInfoDir, { recursive: true })
Expand Down
8 changes: 6 additions & 2 deletions packages/plugin-commands-server/src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import { serverConnectionInfoDir, tryLoadServerJson } from '@pnpm/store-connecti
import storePath from '@pnpm/store-path'

export default async (
opts: Pick<Config, 'dir' | 'storeDir'>
opts: Pick<Config, 'dir' | 'pnpmHomeDir' | 'storeDir'>
) => {
const storeDir = await storePath(opts.dir, opts.storeDir)
const storeDir = await storePath({
pkgRoot: opts.dir,
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
})
const connectionInfoDir = serverConnectionInfoDir(storeDir)
const serverJson = await tryLoadServerJson({
serverJsonPath: path.join(connectionInfoDir, 'server.json'),
Expand Down
7 changes: 6 additions & 1 deletion packages/plugin-commands-server/src/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ export default async (
opts: {
storeDir?: string
dir: string
pnpmHomeDir: string
}
) => {
const storeDir = await storePath(opts.dir, opts.storeDir)
const storeDir = await storePath({
pkgRoot: opts.dir,
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
})
const connectionInfoDir = serverConnectionInfoDir(storeDir)
const serverJson = await tryLoadServerJson({
serverJsonPath: path.join(connectionInfoDir, 'server.json'),
Expand Down
12 changes: 10 additions & 2 deletions packages/plugin-commands-store/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ export async function handler (opts: StoreCommandOptions, params: string[]) {
case 'status':
return statusCmd(opts)
case 'path':
return storePath(opts.dir, opts.storeDir)
return storePath({
pkgRoot: opts.dir,
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
})
case 'prune': {
store = await createOrConnectStoreController(opts)
const storePruneOptions = Object.assign(opts, {
Expand All @@ -102,7 +106,11 @@ export async function handler (opts: StoreCommandOptions, params: string[]) {

async function statusCmd (opts: StoreCommandOptions) {
const modifiedPkgs = await storeStatus(Object.assign(opts, {
storeDir: await storePath(opts.dir, opts.storeDir),
storeDir: await storePath({
pkgRoot: opts.dir,
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
}),
}))
if (!modifiedPkgs || (modifiedPkgs.length === 0)) {
logger.info({
Expand Down
13 changes: 11 additions & 2 deletions packages/store-connection-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { createNewStoreController, serverConnectionInfoDir }
export type CreateStoreControllerOptions = Omit<CreateNewStoreControllerOptions, 'storeDir'> & Pick<Config,
| 'storeDir'
| 'dir'
| 'pnpmHomeDir'
| 'useRunningStoreServer'
| 'useStoreServer'
| 'workspaceDir'
Expand All @@ -26,7 +27,11 @@ export async function createOrConnectStoreControllerCached (
storeControllerCache: Map<string, Promise<{ctrl: StoreController, dir: string}>>,
opts: CreateStoreControllerOptions
) {
const storeDir = await storePath(opts.dir, opts.storeDir)
const storeDir = await storePath({
pkgRoot: opts.dir,
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
})
if (!storeControllerCache.has(storeDir)) {
storeControllerCache.set(storeDir, createOrConnectStoreController(opts))
}
Expand All @@ -39,7 +44,11 @@ export async function createOrConnectStoreController (
ctrl: StoreController
dir: string
}> {
const storeDir = await storePath(opts.workspaceDir ?? opts.dir, opts.storeDir)
const storeDir = await storePath({
pkgRoot: opts.workspaceDir ?? opts.dir,
storePath: opts.storeDir,
pnpmHomeDir: opts.pnpmHomeDir,
})
const connectionInfoDir = serverConnectionInfoDir(storeDir)
const serverJsonPath = path.join(connectionInfoDir, 'server.json')
let serverJson = await tryLoadServerJson({ serverJsonPath, shouldRetryOnNoent: false })
Expand Down
33 changes: 22 additions & 11 deletions packages/store-path/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,23 @@ import touch from 'touch'
const STORE_VERSION = 'v3'

export default function (
pkgRoot: string,
storePath?: string
{
pkgRoot,
storePath,
pnpmHomeDir,
}: {
pkgRoot: string
storePath?: string
pnpmHomeDir: string
}
) {
if (!storePath || isHomepath(storePath)) {
const relStorePath = storePath ? storePath.substring(2) : '.pnpm-store'
return storePathRelativeToHome(pkgRoot, relStorePath)
if (!storePath) {
return storePathRelativeToHome(pkgRoot, 'store', pnpmHomeDir)
}

if (isHomepath(storePath)) {
const homedir = getHomedir()
return storePathRelativeToHome(pkgRoot, storePath.substring(2), homedir)
}

const storeBasePath = pathAbsolute(storePath, pkgRoot)
Expand All @@ -27,16 +38,16 @@ export default function (
return path.join(storeBasePath, STORE_VERSION)
}

async function storePathRelativeToHome (pkgRoot: string, relStore: string) {
async function storePathRelativeToHome (pkgRoot: string, relStore: string, homedir: string) {
const tempFile = pathTemp(pkgRoot)
await fs.mkdir(path.dirname(tempFile), { recursive: true })
await touch(tempFile)
const homedir = getHomedir()
const storeInHomeDir = path.join(homedir, relStore, STORE_VERSION)
if (await canLinkToSubdir(tempFile, homedir)) {
await fs.unlink(tempFile)
// If the project is on the drive on which the OS home directory
// then the store is placed in the home directory
return path.join(homedir, relStore, STORE_VERSION)
return storeInHomeDir
}
try {
let mountpoint = await rootLinkTarget(tempFile)
Expand All @@ -50,13 +61,13 @@ async function storePathRelativeToHome (pkgRoot: string, relStore: string) {
// If linking works only in the project folder
// then prefer to place the store inside the homedir
if (dirsAreEqual(pkgRoot, mountpoint)) {
return path.join(homedir, relStore, STORE_VERSION)
return storeInHomeDir
}
return path.join(mountpoint, relStore, STORE_VERSION)
return path.join(mountpoint, '.pnpm-store', STORE_VERSION)
} catch (err) {
// this is an unlikely situation but if there is no way to find
// a linkable place on the disk, create the store in homedir
return path.join(homedir, relStore, STORE_VERSION)
return storeInHomeDir
} finally {
await fs.unlink(tempFile)
}
Expand Down
15 changes: 12 additions & 3 deletions packages/store-path/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@ jest.mock('fs')
const skipOnWindows = isWindows() ? test.skip : test

skipOnWindows('when a link can be created to the homedir', async () => {
expect(await storePath('/can-link-to-homedir')).toBe('/home/user/.pnpm-store/v3')
expect(await storePath({
pkgRoot: '/can-link-to-homedir',
pnpmHomeDir: '/local/share/pnpm',
})).toBe('/local/share/pnpm/store/v3')
})

skipOnWindows('a link can be created to the root of the drive', async () => {
expect(await storePath('/src/workspace/project')).toBe('/.pnpm-store/v3')
expect(await storePath({
pkgRoot: '/src/workspace/project',
pnpmHomeDir: '/local/share/pnpm',
})).toBe('/.pnpm-store/v3')
})

skipOnWindows('a link can be created to the a subdir in the root of the drive', async () => {
expect(await storePath('/mnt/project')).toBe('/mnt/.pnpm-store/v3')
expect(await storePath({
pkgRoot: '/mnt/project',
pnpmHomeDir: '/local/share/pnpm',
})).toBe('/mnt/.pnpm-store/v3')
})
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.