@@ -353,15 +353,21 @@ func (npc *NpmPublishCommand) readPackageInfoFromTarball(packedFilePath string)
353353
354354 // Log which package.json was selected if it's not in the standard location
355355 if packageJsonPath != "package/package.json" {
356- log .Info ("Using non-standard package.json location: " + packageJsonPath )
356+ log .Warn ("Using non-standard package.json location: " + packageJsonPath )
357357 }
358358
359359 // Parse the package.json content
360360 npc .packageInfo , err = biutils .ReadPackageInfo (packageJson , npc .npmVersion )
361361 return err
362362}
363363
364- // findBestPackageJson scans the tarball and returns the best package.json file based on priority
364+ // findBestPackageJson scans the tarball and returns the most appropriate package.json file based on priority.
365+ // Some npm packages may contain multiple package.json files in different locations within the tarball.
366+ // This function prioritizes them as follows:
367+ // 1. Standard location: "package/package.json" (highest priority)
368+ // 2. Root-level package.json files like "node/package.json"
369+ // 3. Other locations based on directory nesting depth (lower priority)
370+ // This ensures we select the most relevant package.json file, similar to how npm itself handles non-standard structures.
365371func findBestPackageJson (tarReader * tar.Reader ) (string , []byte , error ) {
366372 type packageJsonCandidate struct {
367373 path string
@@ -374,7 +380,7 @@ func findBestPackageJson(tarReader *tar.Reader) (string, []byte, error) {
374380
375381 // Scan all entries in the tarball
376382 for {
377- hdr , err := tarReader .Next ()
383+ tarHeader , err := tarReader .Next ()
378384 if err != nil {
379385 if err == io .EOF {
380386 break
@@ -383,18 +389,18 @@ func findBestPackageJson(tarReader *tar.Reader) (string, []byte, error) {
383389 }
384390
385391 // Check if this is a package.json file
386- if strings .HasSuffix (hdr .Name , "package.json" ) {
392+ if strings .HasSuffix (tarHeader .Name , "package.json" ) {
387393 // Read content
388394 content , err := io .ReadAll (tarReader )
389395 if err != nil {
390- log .Debug ("Error reading " + hdr .Name + ": " + err .Error ())
396+ log .Warn ("Error reading " + tarHeader .Name + ": " + err .Error ())
391397 continue
392398 }
393399
394400 // Calculate priority based on path
395- priority := calculatePackageJsonPriority (hdr .Name )
401+ priority := calculatePackageJsonPriority (tarHeader .Name )
396402 candidate := & packageJsonCandidate {
397- path : hdr .Name ,
403+ path : tarHeader .Name ,
398404 priority : priority ,
399405 content : content ,
400406 }
@@ -405,7 +411,7 @@ func findBestPackageJson(tarReader *tar.Reader) (string, []byte, error) {
405411 }
406412
407413 // Standard location is highest priority, can stop searching
408- if hdr .Name == "package/package.json" {
414+ if tarHeader .Name == "package/package.json" {
409415 break
410416 }
411417 }
@@ -429,12 +435,14 @@ func calculatePackageJsonPriority(path string) int {
429435
430436 parts := strings .Split (path , "/" )
431437
432- // Root directory package.json (like node/package.json) gets second priority
438+ // Root directory package.json gets second priority
439+ // (like node/package.json or main/package.json)
433440 if len (parts ) == 2 && parts [1 ] == "package.json" {
434441 return 2
435442 }
436443
437444 // Other locations get lower priority based on nesting depth
445+ // This handles cases where multiple package.json files exist in subdirectories
438446 return 3 + len (parts )
439447}
440448
0 commit comments