Skip to content

Duplicate Code: repeated package.json parse boilerplate in repoPolicyCheck handlers #27019

@github-actions

Description

@github-actions

🔍 Duplicate Code Detected: PackageJson parse boilerplate in repoPolicyCheck

Analysis of commit 73a892497061d9677f4ebe0fd3394476ad60c98f

Assignee: @copilot

Summary

Multiple repo policy-check handlers repeat the same package.json read/parse/try-catch boilerplate (and closely related follow-up checks). This appears 20+ times and makes it easy for behavior to drift (e.g., inconsistent error return shapes).

Duplication Details

Pattern: Read + parse package.json with identical try/catch

  • Severity: Medium

  • Occurrences: 22+ instances (20 in npmPackages.ts, 2 in fluidBuildTasks.ts)

  • Locations (examples):

    • build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts (lines 775–781)
    • build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts (lines 872–876)
    • build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts (lines 895–899)
    • build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts (lines 1175–1179)
    • build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts (lines 770–775)
    • build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts (lines 829–834)
  • Code Sample (representative):

    let json: PackageJson;
    try {
        json = JSON.parse(readFile(file)) as PackageJson;
    } catch {
        return `Error parsing JSON file: \$\{file}`;
    }

Impact Analysis

  • Maintainability: Changes to parsing (e.g., JSON5 support, better error details, validation) must be made in many places.
  • Bug Risk: Some handlers return different error shapes on parse failure (string vs { resolved: false, message }), increasing drift risk.
  • Code Bloat: The same 6–8 line block repeated 20+ times adds noise and makes the actual policy logic harder to scan.

Refactoring Recommendations

  1. Extract a shared helper

    • Suggested location: build-tools/packages/build-cli/src/library/repoPolicyCheck/common.ts
    • Example shape:
      • tryReadPackageJson(file): { ok: true; json: PackageJson } | { ok: false; message: string }
      • (Optionally) readPackageJsonOrThrow + centralized catch-to-message wrapper.
    • Benefits: one place to improve error messages/validation and keep return conventions consistent.
  2. Standardize parse-error handling across handlers

    • Pick one convention for parse failures (e.g., always return a string message) and adapt the few handlers that currently return objects.

Implementation Checklist

  • Introduce helper in repoPolicyCheck/common.ts (or similar)
  • Replace repeated try/catch blocks in npmPackages.ts and fluidBuildTasks.ts
  • Ensure handler return types remain correct (string vs { resolved, message } patterns)
  • Run existing build/lint/test for build-cli as appropriate

Analysis Metadata

  • Analyzed Files (focused subset): build-tools/packages/build-cli/src/library/repoPolicyCheck/npmPackages.ts, build-tools/packages/build-cli/src/library/repoPolicyCheck/fluidBuildTasks.ts
  • Detection Method: Serena semantic pattern search (search_for_pattern) + targeted line inspection
  • Commit: 73a892497061d9677f4ebe0fd3394476ad60c98f
  • Analysis Date: 2026-04-13T21:56:03.874Z

Generated by Duplicate Code Detector ·

To install this agentic workflow, run

gh aw add github/gh-aw/.github/workflows/duplicate-code-detector.md@94662b1dee8ce96c876ba9f33b3ab8be32de82a4

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions