microsoft / microsoft/TypeScript
Compiler reformats JSON when copying
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reproduction repository and its tsconfig.json, then inspect the JSON-copy path around the referenced logic in src/compiler/emitter.ts. Compare compilation with and without outDir and verify the SHA-256 hashes of the source and emitted JSON. Done means copying JSON to outDir preserves its original bytes and formatting.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- build-system, compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100