Skip to content

Commit

Permalink
fix(dependencies-hierarchy): print version paths rel to starting project
Browse files Browse the repository at this point in the history
  • Loading branch information
gluxon committed Jan 2, 2023
1 parent a3e45a1 commit da750dc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ async function dependenciesHierarchyForPackage (
importers: currentLockfile.importers,
includeOptionalDependencies: opts.include.optionalDependencies,
lockfileDir: opts.lockfileDir,
rewriteLinkVersionDir: projectPath,
maxDepth: opts.depth,
modulesDir,
registries: opts.registries,
Expand All @@ -127,6 +128,7 @@ async function dependenciesHierarchyForPackage (
const packageInfo = getPkgInfo({
alias,
currentPackages: currentLockfile.packages ?? {},
rewriteLinkVersionDir: projectPath,
linkedPathBaseDir: projectPath,
modulesDir,
ref,
Expand Down
22 changes: 19 additions & 3 deletions reviewing/dependencies-hierarchy/src/getPkgInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from '@pnpm/lockfile-utils'
import { Registries } from '@pnpm/types'
import { depPathToFilename, refToRelative } from '@pnpm/dependency-path'
import normalizePath from 'normalize-path'

export interface GetPkgInfoOpts {
readonly alias: string
Expand All @@ -24,6 +25,15 @@ export interface GetPkgInfoOpts {
* The base dir if the `ref` argument is a `"link:"` relative path.
*/
readonly linkedPathBaseDir: string

/**
* If the `ref` argument is a `"link:"` relative path, the ref is reused for
* the version field. (Since the true semver may not be known.)
*
* Optionally rewrite this relative path to a base dir before writing it to
* version.
*/
readonly rewriteLinkVersionDir?: string
}

export function getPkgInfo (opts: GetPkgInfoOpts) {
Expand Down Expand Up @@ -62,15 +72,21 @@ export function getPkgInfo (opts: GetPkgInfoOpts) {
name = opts.alias
version = opts.ref
}
const fullPackagePath = depPath
? path.join(opts.modulesDir, '.pnpm', depPathToFilename(depPath))
: path.join(opts.linkedPathBaseDir, opts.ref.slice(5))

if (version.startsWith('link:') && opts.rewriteLinkVersionDir) {
version = `link:${normalizePath(path.relative(opts.rewriteLinkVersionDir, fullPackagePath))}`
}

const packageInfo = {
alias: opts.alias,
isMissing,
isPeer: Boolean(opts.peers?.has(opts.alias)),
isSkipped,
name,
path: depPath
? path.join(opts.modulesDir, '.pnpm', depPathToFilename(depPath))
: path.join(opts.linkedPathBaseDir, opts.ref.slice(5)),
path: fullPackagePath,
version,
}
if (resolved) {
Expand Down
2 changes: 2 additions & 0 deletions reviewing/dependencies-hierarchy/src/getTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { serializeTreeNodeId, TreeNodeId } from './TreeNodeId'

interface GetTreeOpts {
maxDepth: number
rewriteLinkVersionDir: string
modulesDir: string
includeOptionalDependencies: boolean
lockfileDir: string
Expand Down Expand Up @@ -123,6 +124,7 @@ function getTreeHelper (
const packageInfo = getPkgInfo({
alias,
currentPackages: opts.currentPackages,
rewriteLinkVersionDir: opts.rewriteLinkVersionDir,
linkedPathBaseDir,
modulesDir: opts.modulesDir,
peers,
Expand Down
3 changes: 3 additions & 0 deletions reviewing/dependencies-hierarchy/test/getTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('getTree', () => {

const getTreeArgs = {
maxDepth: 0,
rewriteLinkVersionDir: '',
modulesDir: '',
importers: {},
includeOptionalDependencies: false,
Expand Down Expand Up @@ -158,6 +159,7 @@ describe('getTree', () => {
// result in incorrect output if the cache was used when it's not supposed to.
describe('prints at expected depth for cache regression testing cases', () => {
const commonMockGetTreeArgs = {
rewriteLinkVersionDir: '',
modulesDir: '',
importers: {},
includeOptionalDependencies: false,
Expand Down Expand Up @@ -292,6 +294,7 @@ describe('getTree', () => {
// result in incorrect output if the cache was used when it's not supposed to.
describe('fully visited cache optimization handles requested depth correctly', () => {
const commonMockGetTreeArgs = {
rewriteLinkVersionDir: '',
modulesDir: '',
importers: {},
includeOptionalDependencies: false,
Expand Down
4 changes: 2 additions & 2 deletions reviewing/dependencies-hierarchy/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,12 @@ test('on a package with nested workspace links', async () => {
dependencies: [
expect.objectContaining({
alias: '@scope/b',
version: 'link:../b',
version: 'link:packages/b',
path: path.join(workspaceWithNestedWorkspaceDeps, 'packages/b'),
dependencies: [
expect.objectContaining({
alias: '@scope/c',
version: 'link:../c',
version: 'link:packages/c',
path: path.join(workspaceWithNestedWorkspaceDeps, 'packages/c'),
}),
expect.objectContaining({
Expand Down

0 comments on commit da750dc

Please sign in to comment.