microsoft / microsoft/TypeScript

Compiler reformats JSON when copying

Aperta
#43,436 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Bug Report

🔎 Search Terms

json, compile, build, reformat

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about JSON.

⏯ Playground Link

n/a

💻 Code

See this repository for a reproduction of this issue: https://github.com/eventualbuddha/ts-reformat-json. Some of the relevant information is copied here:

// src/index.ts
import { createHash } from "crypto";
import { readFileSync } from "fs";
import { join } from "path";

console.log(
  createHash("sha256")
    .update(readFileSync(join(__dirname, "./j.json")))
    .digest("hex")
);
// src/data.json
{
  "hello": "world"
}
// tsconfig.json
{
  "compilerOptions": {
    "outDir": "./dist",
    "resolveJsonModule": true,
  },
  "include": ["src", "src/*.json"]
}
🙁 Actual behavior

When compiling without outDir, TypeScript sees that the source and destination of the JSON file is the same, so it leaves the data alone. That yields this output:

6df3be5b276bce2a13717dcc4d56594fabab5dd50285e4b144328a6546bab69c
{
  "hello": "world"
}

This also occurs when running directly with ts-node regardless of outDir. When using outDir, TypeScript copies the JSON file in a way that reformats its contents, yielding this output:

8c91c97296861a8e91702ab0d820c93ca1152fc79ba901ab23118c7f70963155
{
    "hello": "world"
}
🙂 Expected behavior

While it's not unreasonable to design it this way if you're treating JSON merely as resolvable/importable shorthand for an export default of the same data, it does cause problems if you care about the actual bytes of the JSON file. In my application, we use JSON files as the definition of an election including contests and candidates etc. To ensure that the data has not changed we compute the sha256 hash of the file, so two equivalent files that are JSON.parsed into the same object are not considered equal. We store demo/fixture election definitions and have tests that use them and present the computed hash to the user. We validate the hash as shown to the user, but if we compute the result based on TypeScript's reformatted version the hash does not match what we expect.

Thus, it'd be very convenient for us to have TypeScript preserve the JSON data as-is when copying it to outDir. As far as I can tell there's no actual benefit to reformatting, and it's presumably a side-effect of JSON files flowing through the TS → JS pipeline.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal repository di riproduzione e dal relativo tsconfig.json, quindi esamina il percorso di copia JSON attorno alla logica referenziata in src/compiler/emitter.ts. Confronta la compilazione con e senza outDir e verifica gli hash SHA-256 del JSON sorgente e del JSON emesso. Il lavoro è completato quando la copia del JSON in outDir preserva i byte e la formattazione originali.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
build-system, compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.