Skip to content

Commit

Permalink
fix(deploy): ensure that the deploy target is generated in the same d…
Browse files Browse the repository at this point in the history
…irectory

docs: add changeset

fix: test
  • Loading branch information
await-ovo committed Jul 25, 2023
1 parent 5656424 commit e6b74d7
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 1 deletion.
8 changes: 8 additions & 0 deletions .changeset/moody-bobcats-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@pnpm/plugin-commands-installation": patch
"@pnpm/plugin-commands-deploy": patch
"@pnpm/types": patch
"pnpm": patch
---

Ensure that the deploy target is generated in the same directory [#6858](https://github.com/pnpm/pnpm/issues/6858)
1 change: 1 addition & 0 deletions packages/types/src/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface Project {
dir: string
manifest: ProjectManifest
writeProjectManifest: (manifest: ProjectManifest, force?: boolean | undefined) => Promise<void>
modulesDir?: string
}

export type ProjectsGraph = Record<string, { dependencies: string[], package: Project }>
1 change: 1 addition & 0 deletions pkg-manager/plugin-commands-installation/src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export type InstallCommandOptions = Pick<Config,
| 'bail'
| 'bin'
| 'cliOptions'
| 'dedupePeerDependents'
| 'deployAllFiles'
| 'depth'
| 'dev'
Expand Down
1 change: 1 addition & 0 deletions pkg-manager/plugin-commands-installation/src/recursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ function getAllProjects (manifestsByPath: ManifestsByPath, allProjectsGraph: Pro
buildIndex,
manifest: manifestsByPath[rootDir].manifest,
rootDir,
modulesDir: allProjectsGraph[rootDir].package.modulesDir,
}))).flat()
}

Expand Down
14 changes: 13 additions & 1 deletion releasing/plugin-commands-deploy/src/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PnpmError } from '@pnpm/error'
import rimraf from '@zkochan/rimraf'
import renderHelp from 'render-help'
import { deployHook } from './deployHook'
import { type ProjectsGraph } from '@pnpm/types'

export const shorthands = install.shorthands

Expand Down Expand Up @@ -76,6 +77,9 @@ export async function handler (
await fs.promises.mkdir(deployDir, { recursive: true })
const includeOnlyPackageFiles = !opts.deployAllFiles
await copyProject(deployedDir, deployDir, { includeOnlyPackageFiles })
const absoluteModulesDir = path.join(deployDir, 'node_modules')
extendProjectModulesDir(opts.allProjectsGraph, absoluteModulesDir)

await install.handler({
...opts,
confirmModulesPurge: false,
Expand All @@ -91,7 +95,7 @@ export async function handler (
preferFrozenLockfile: false,
saveLockfile: false,
virtualStoreDir: path.join(deployDir, 'node_modules/.pnpm'),
modulesDir: path.relative(deployedDir, path.join(deployDir, 'node_modules')),
modulesDir: path.relative(deployedDir, absoluteModulesDir),
rawLocalConfig: {
...opts.rawLocalConfig,
// This is a workaround to prevent frozen install in CI envs.
Expand All @@ -106,3 +110,11 @@ async function copyProject (src: string, dest: string, opts: { includeOnlyPackag
const importPkg = createIndexedPkgImporter('clone-or-copy')
await importPkg(dest, { filesMap: filesIndex, force: true, fromStore: true })
}

function extendProjectModulesDir (allProjectsGraph: ProjectsGraph | undefined, absoluteModulesDir: string) {
if (allProjectsGraph) {
Object.keys(allProjectsGraph).forEach(prefix => {
allProjectsGraph[prefix].package.modulesDir = path.relative(allProjectsGraph[prefix].package.dir, absoluteModulesDir)
})
}
}
51 changes: 51 additions & 0 deletions releasing/plugin-commands-deploy/test/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,54 @@ test('deploy', async () => {
expect(fs.existsSync('deploy/node_modules/.pnpm/file+project-3/node_modules/project-3/test.js')).toBeFalsy()
expect(fs.existsSync('pnpm-lock.yaml')).toBeFalsy() // no changes to the lockfile are written
})

test('deploy with dedupePeerDependents true', async () => {
preparePackages([
{
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
},
{
location: './sub-dir/project-2',
package: {
name: 'project-2',
version: '2.0.0',
dependencies: {
'is-odd': '1.0.0',
},
},
},
{
name: 'project-3',
version: '2.0.0',
dependencies: {
'is-number': '1.0.0',
},
},
])

const { allProjects, selectedProjectsGraph, allProjectsGraph } = await readProjects(process.cwd(), [{ namePattern: 'project-1' }])

await deploy.handler({
...DEFAULT_OPTS,
allProjects,
allProjectsGraph,
dir: process.cwd(),
dev: false,
production: true,
recursive: true,
selectedProjectsGraph,
sharedWorkspaceLockfile: true,
lockfileDir: process.cwd(),
workspaceDir: process.cwd(),
dedupePeerDependents: true,
}, ['deploy'])
const project = assertProject(path.resolve('deploy'))
await project.has('is-positive')
await project.has('is-odd')
await project.has('is-number')
expect(fs.existsSync('sub-dir/deploy')).toBe(false)
})

0 comments on commit e6b74d7

Please sign in to comment.