Repository: TypeScript
Issue: Compiler reformats JSON when copying
Labels: Suggestion,Awaiting More Feedback

# Bug Report

### 🔎 Search Terms

json, compile, build, reformat

### 🕗 Version & Regression Information

<!-- When did you start seeing this bug occur?

"Bugs" that have existed in TS for a long time are very likely to be FAQs; refer to
  https://github.com/Microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

If possible, please try testing the nightly version of TS to see if it's already been fixed.
For npm: `typescript@next`
This is also the 'Nightly' version in the playground: http://www.typescriptlang.org/play/?ts=Nightly

Note: The TypeScript Playground can be used to try older versions of TypeScript.

Please keep and fill in the line that best applies:
-->
This is the behavior in every version I tried, and I reviewed the FAQ for entries about JSON.

### ⏯ Playground Link

<!--
  A link to a TypeScript Playground "Share" link which shows this behavior

  The TypeScript Workbench can be used for more complex setups, try
  https://www.typescriptlang.org/dev/bug-workbench/

  As a last resort, you can link to a repo, but these will be slower for us to investigate.
-->
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:

```ts
// 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")
);
```

```jsonc
// src/data.json
{
  "hello": "world"
}
```

```jsonc
// 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](https://github.com/microsoft/TypeScript/blob/294a5a7d784a5a95a8048ee990400979a6bc3a1c/src/compiler/emitter.ts#L94-L96). 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.parse`d into the same object are not considered equal. We store [demo/fixture election definitions](https://github.com/votingworks/vxsuite/tree/main/libs/fixtures/src/data) 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.