@@ -275,16 +275,48 @@ export async function api (opts: PackOptions): Promise<PackResult> {
275275 const filesMap = Object . fromEntries ( files . map ( ( file ) => [ `package/${ file } ` , path . join ( dir , file ) ] ) )
276276 // cspell:disable-next-line
277277 if ( opts . workspaceDir != null && dir !== opts . workspaceDir && ! files . some ( ( file ) => / L I C E N [ C S ] E (?: \. .+ ) ? / i. test ( file ) ) ) {
278- const licenses = await glob ( [ LICENSE_GLOB ] , { cwd : opts . workspaceDir , expandDirectories : false } )
279- for ( const license of licenses ) {
280- filesMap [ `package/${ license } ` ] = path . join ( opts . workspaceDir , license )
281- }
278+ const { workspaceDir } = opts
279+ const licenses = await glob ( [ LICENSE_GLOB ] , { cwd : workspaceDir , expandDirectories : false } )
280+ await Promise . all ( licenses . map ( async ( license ) => {
281+ const licensePath = path . join ( workspaceDir , license )
282+ // Only inject a regular file. A symlink could point outside the workspace and leak its
283+ // target's bytes into the published tarball, so `lstat()` (which does not follow symlinks)
284+ // rejects it — matching pacquet's inject_workspace_license.
285+ const stats = await fs . promises . lstat ( licensePath )
286+ if ( stats . isFile ( ) ) {
287+ filesMap [ `package/${ license } ` ] = licensePath
288+ }
289+ } ) )
282290 }
283291 const destDir = packDestination
284292 ? ( path . isAbsolute ( packDestination ) ? packDestination : path . join ( dir , packDestination ?? '.' ) )
285293 : dir
286294 if ( ! opts . dryRun ) {
287295 await fs . promises . mkdir ( destDir , { recursive : true } )
296+ }
297+ // Derive `contents` and `unpackedSize` from `filesMap` (the full set of tar entries) rather than
298+ // from `files` (the packlist subset) so that:
299+ // - workspace LICENSE files appended to `filesMap` after the packlist call are included; and
300+ // - `package.yaml` / `package.json5` entries are reported under the name they actually have in
301+ // the tar (`package.json`), since `packPkg()` rewrites them.
302+ // The `stat()` pass must run before `postpack`, which may delete prepack-generated files that
303+ // were packed. See https://github.com/pnpm/pnpm/issues/12775.
304+ const sizes = await Promise . all ( Object . entries ( filesMap ) . map ( async ( [ name , source ] ) => {
305+ if ( isManifestEntry ( name ) ) {
306+ return Buffer . byteLength ( JSON . stringify ( publishManifest , null , 2 ) )
307+ }
308+ const stat = await fs . promises . stat ( source )
309+ return stat . size
310+ } ) )
311+ const unpackedSize = sizes . reduce ( ( acc , size ) => acc + size , 0 )
312+ const packedContents = Array . from ( new Set (
313+ Object . keys ( filesMap ) . map ( ( name ) =>
314+ isManifestEntry ( name )
315+ ? 'package.json'
316+ : name . replace ( / ^ p a c k a g e \/ / , '' )
317+ )
318+ ) ) . sort ( ( a , b ) => a . localeCompare ( b , 'en' ) )
319+ if ( ! opts . dryRun ) {
288320 await packPkg ( {
289321 destFile : path . join ( destDir , tarballName ) ,
290322 filesMap,
@@ -307,26 +339,6 @@ export async function api (opts: PackOptions): Promise<PackResult> {
307339 } else {
308340 packedTarballPath = path . relative ( opts . dir , path . join ( dir , tarballName ) )
309341 }
310- // Derive `contents` and `unpackedSize` from `filesMap` (the full set of tar entries) rather than
311- // from `files` (the packlist subset) so that:
312- // - workspace LICENSE files appended to `filesMap` after the packlist call are included; and
313- // - `package.yaml` / `package.json5` entries are reported under the name they actually have in
314- // the tar (`package.json`), since `packPkg()` rewrites them.
315- const sizes = await Promise . all ( Object . entries ( filesMap ) . map ( async ( [ name , source ] ) => {
316- if ( / ^ p a c k a g e \/ p a c k a g e \. (?: j s o n | j s o n 5 | y a m l ) $ / . test ( name ) ) {
317- return Buffer . byteLength ( JSON . stringify ( publishManifest , null , 2 ) )
318- }
319- const stat = await fs . promises . stat ( source )
320- return stat . size
321- } ) )
322- const unpackedSize = sizes . reduce ( ( acc , size ) => acc + size , 0 )
323- const packedContents = Array . from ( new Set (
324- Object . keys ( filesMap ) . map ( ( name ) =>
325- / ^ p a c k a g e \/ p a c k a g e \. (?: j s o n | j s o n 5 | y a m l ) $ / . test ( name )
326- ? 'package.json'
327- : name . replace ( / ^ p a c k a g e \/ / , '' )
328- )
329- ) ) . sort ( ( a , b ) => a . localeCompare ( b , 'en' ) )
330342 return {
331343 publishedManifest : publishManifest ,
332344 contents : packedContents ,
@@ -343,6 +355,13 @@ export interface PackResult {
343355 unpackedSize : number
344356}
345357
358+ // True when a `package/<path>` tar key names the package manifest, which is
359+ // packed as a single serialized `package/package.json` entry and reported as
360+ // `package.json` in the contents listing regardless of the source file name.
361+ function isManifestEntry ( name : string ) : boolean {
362+ return name === 'package/package.json' || name === 'package/package.json5' || name === 'package/package.yaml'
363+ }
364+
346365function stripBuildMetadata ( version : string ) : string {
347366 const plusIndex = version . indexOf ( '+' )
348367 return plusIndex === - 1 ? version : version . slice ( 0 , plusIndex )
@@ -379,7 +398,7 @@ async function packPkg (opts: {
379398 await Promise . all ( Object . entries ( filesMap ) . map ( async ( [ name , source ] ) => {
380399 const isExecutable = bins . some ( ( bin ) => path . relative ( bin , source ) === '' )
381400 const mode = isExecutable ? 0o755 : 0o644
382- if ( / ^ p a c k a g e \/ p a c k a g e \. (?: j s o n | j s o n 5 | y a m l ) $ / . test ( name ) ) {
401+ if ( isManifestEntry ( name ) ) {
383402 pack . entry ( { mode, mtime, name : 'package/package.json' } , JSON . stringify ( manifest , null , 2 ) )
384403 return
385404 }
0 commit comments