Skip to content

Running on a .json file inserts a JS comment header and breaks the JSON #120

Description

@Shinrai

Problem

Pointing fix-headers at a .json file writes a /** … */ JS comment block at the top. JSON has no comments, so the file becomes invalid.

Reproduced on @cldmv/fix-headers@2.1.3:

npx fix-headers --input package.json --verbose
# fix-headers complete: scanned=1, updated=1, dryRun=false
# updated: package.json
head -3 package.json
# /**
#  *
#  *	@Project: fh-repro

Node then refuses the package. Every npm command in that directory, and any require() resolving through it, fails with:

Error: Invalid package config .../package.json.  code: 'ERR_INVALID_PACKAGE_CONFIG'

It happened for real during the CJS-entry sweep (CLDMV/vitest-runner#69): running it on package.json broke npm run build until the edit was reverted. The README lists JSON among the supported formats, so nothing warns you off doing this.

Fix

JSON (.json, .jsonc aside) can't carry a comment header. Skip strict-JSON files, or handle them explicitly in a way that keeps the file valid, and never write a comment into one. If --input names a file that is skipped, report it as skipped rather than "updated". Add a test asserting that package.json (and any *.json) is unchanged and still parses after a run.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreTouches core library / runtime source codepriority: highNeeds attention soon — not blocking, but don't let it sitstatus: implementedBuilt and deployed, but not yet fully tested/verifiedtype: bugSomething is broken or not behaving as expected

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions