@@ -2,7 +2,8 @@ import path from 'path';
22import fs from 'fs' ;
33import YAML from 'yaml' ;
44import process from 'node:process' ;
5- import { depPathToFilename } from '@pnpm/dependency-path' ;
5+ import { depPathToFilename as depPathToFilename2 } from '@pnpm/dependency-path-2' ;
6+ import { depPathToFilename as depPathToFilename5 } from '@pnpm/dependency-path-5' ;
67
78import {
89 ILockfile ,
@@ -11,7 +12,6 @@ import {
1112 LogMessageIdentifier ,
1213 IPnpmSyncJson ,
1314 IVersionSpecifier ,
14- ILockfilePackage ,
1515 ITargetFolder
1616} from './interfaces' ;
1717import { pnpmSyncGetJsonVersion } from './utilities' ;
@@ -220,9 +220,9 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
220220 const pnpmVersion : string | undefined = pnpmModulesYaml ?. packageManager ?. split ( '@' ) [ 1 ] ;
221221
222222 // currently, only support pnpm v8
223- if ( ! pnpmVersion || ! pnpmVersion . startsWith ( '8' ) ) {
223+ if ( ! pnpmVersion || ! pnpmVersion . startsWith ( '8' ) || ! pnpmVersion . startsWith ( '9' ) ) {
224224 logMessageCallback ( {
225- message : `The pnpm version is not supported; pnpm-sync requires pnpm version 8.x` ,
225+ message : `The pnpm version is not supported; pnpm-sync requires pnpm version 8.x, 9.x ` ,
226226 messageKind : LogMessageKind . ERROR ,
227227 details : {
228228 messageIdentifier : LogMessageIdentifier . PREPARE_ERROR_UNSUPPORTED_PNPM_VERSION ,
@@ -240,9 +240,9 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
240240
241241 // currently, only support lockfileVersion 6.x, which is pnpm v8
242242 const lockfileVersion : string | undefined = pnpmLockfile ?. lockfileVersion . toString ( ) ;
243- if ( ! lockfileVersion || ! lockfileVersion . startsWith ( '6' ) ) {
243+ if ( ! lockfileVersion || ! lockfileVersion . startsWith ( '6' ) || ! lockfileVersion . startsWith ( '9' ) ) {
244244 logMessageCallback ( {
245- message : `The pnpm-lock.yaml format is not supported; pnpm-sync requires lockfile version 6` ,
245+ message : `The pnpm-lock.yaml format is not supported; pnpm-sync requires lockfile version 6, 9 ` ,
246246 messageKind : LogMessageKind . ERROR ,
247247 details : {
248248 messageIdentifier : LogMessageIdentifier . PREPARE_ERROR_UNSUPPORTED_FORMAT ,
@@ -282,12 +282,17 @@ export async function pnpmSyncPrepareAsync(options: IPnpmSyncPrepareOptions): Pr
282282 injectedDependencyToFilePathSet . set ( injectedDependencyPath , [ ] ) ;
283283 }
284284
285- const fullPackagePath = path . join (
286- dotPnpmFolder ,
287- depPathToFilename ( injectedDependencyVersion ) ,
288- 'node_modules' ,
289- injectedDependency
290- ) ;
285+ const packageDirname = ( ( ) => {
286+ if ( pnpmVersion . startsWith ( '8' ) ) {
287+ return depPathToFilename2 ( injectedDependencyVersion ) ;
288+ }
289+ if ( pnpmVersion . startsWith ( '9' ) ) {
290+ return depPathToFilename5 ( injectedDependency + '@' + injectedDependencyVersion , 120 ) ;
291+ }
292+ return '' ;
293+ } ) ( ) ;
294+
295+ const fullPackagePath = path . join ( dotPnpmFolder , packageDirname , 'node_modules' , injectedDependency ) ;
291296
292297 injectedDependencyToFilePathSet . get ( injectedDependencyPath ) ?. push ( fullPackagePath ) ;
293298 }
@@ -376,47 +381,65 @@ function processTransitiveInjectedDependency(
376381 pnpmLockfile : ILockfile | undefined ,
377382 injectedDependencyToVersion : Map < string , Set < string > >
378383) : void {
384+ if ( ! pnpmLockfile ) {
385+ return ;
386+ }
387+
388+ const { lockfileVersion } = pnpmLockfile ;
389+
379390 const potentialTransitiveInjectedDependencyVersionQueue : Array < string > = [ ] ;
380- for ( const injectedDependencyVersion of [ ...injectedDependencyToVersion . values ( ) ] ) {
381- potentialTransitiveInjectedDependencyVersionQueue . push ( ...injectedDependencyVersion ) ;
391+ for ( const [ packageName , injectedDependencyVersion ] of [ ...injectedDependencyToVersion . entries ( ) ] ) {
392+ if ( lockfileVersion . toString ( ) . startsWith ( '6' ) ) {
393+ potentialTransitiveInjectedDependencyVersionQueue . push ( ...injectedDependencyVersion ) ;
394+ } else if ( lockfileVersion . toString ( ) . startsWith ( '9' ) ) {
395+ potentialTransitiveInjectedDependencyVersionQueue . push (
396+ ...[ ...injectedDependencyVersion ] . map ( ( version ) => packageName + '@' + version )
397+ ) ;
398+ }
382399 }
383400
384- const lockfilePackages : Record < string , ILockfilePackage > | undefined = pnpmLockfile ?. packages ;
385-
386- if ( lockfilePackages ) {
387- while ( potentialTransitiveInjectedDependencyVersionQueue . length > 0 ) {
388- const transitiveInjectedDependencyVersion : string | undefined =
389- potentialTransitiveInjectedDependencyVersionQueue . shift ( ) ;
390- if ( transitiveInjectedDependencyVersion ) {
391- const { dependencies , optionalDependencies } = lockfilePackages [ transitiveInjectedDependencyVersion ] ;
392- processInjectedDependencies (
393- dependencies ,
394- injectedDependencyToVersion ,
395- potentialTransitiveInjectedDependencyVersionQueue
396- ) ;
397- processInjectedDependencies (
398- optionalDependencies ,
399- injectedDependencyToVersion ,
400- potentialTransitiveInjectedDependencyVersionQueue
401- ) ;
402- }
401+ const { packages : lockfilePackages } = pnpmLockfile ;
402+
403+ while ( potentialTransitiveInjectedDependencyVersionQueue . length > 0 ) {
404+ const transitiveInjectedDependencyVersion : string | undefined =
405+ potentialTransitiveInjectedDependencyVersionQueue . shift ( ) ;
406+ if ( transitiveInjectedDependencyVersion ) {
407+ const { dependencies , optionalDependencies } = lockfilePackages [ transitiveInjectedDependencyVersion ] ;
408+ processInjectedDependencies (
409+ dependencies ,
410+ injectedDependencyToVersion ,
411+ potentialTransitiveInjectedDependencyVersionQueue ,
412+ pnpmLockfile
413+ ) ;
414+ processInjectedDependencies (
415+ optionalDependencies ,
416+ injectedDependencyToVersion ,
417+ potentialTransitiveInjectedDependencyVersionQueue ,
418+ pnpmLockfile
419+ ) ;
403420 }
404421 }
405422}
406423function processInjectedDependencies (
407424 dependencies : Record < string , string > | undefined ,
408425 injectedDependencyToVersion : Map < string , Set < string > > ,
409- potentialTransitiveInjectedDependencyVersionQueue : Array < string >
426+ potentialTransitiveInjectedDependencyVersionQueue : Array < string > ,
427+ pnpmLockfile : ILockfile
410428) : void {
411429 if ( dependencies ) {
430+ const { lockfileVersion } = pnpmLockfile ;
412431 for ( const [ dependency , version ] of Object . entries ( dependencies ) ) {
413432 // if the version is set with file: protocol, then it is a transitive injected dependency
414433 if ( version . startsWith ( 'file:' ) ) {
415434 if ( ! injectedDependencyToVersion . has ( dependency ) ) {
416435 injectedDependencyToVersion . set ( dependency , new Set ( ) ) ;
417436 }
418437 injectedDependencyToVersion . get ( dependency ) ?. add ( version ) ;
419- potentialTransitiveInjectedDependencyVersionQueue . push ( version ) ;
438+ if ( lockfileVersion . toString ( ) . startsWith ( '6' ) ) {
439+ potentialTransitiveInjectedDependencyVersionQueue . push ( version ) ;
440+ } else if ( lockfileVersion . toString ( ) . startsWith ( '9' ) ) {
441+ potentialTransitiveInjectedDependencyVersionQueue . push ( dependency + '@' + version ) ;
442+ }
420443 }
421444 }
422445 }
0 commit comments