cloudflare / cloudflare/workers-sdk

[wrangler] sortObjectRecursive throws TypeError on null inside an array

Open Beginner friendly
#14,826 4 comments 0 reactions 0 assignees View on GitHub
awaiting-response:cloudflare
Dominant language
TypeScript
Stars
4.5k
Forks
1.5k
Avg merge
3d 8h
Merged PRs (30d)
187

Description

### What versions & operating system are you using?

Wrangler 4.114.0, Node v24.11.0, macOS 15 (Darwin 24.6.0). Also present on `main` @ `3a141ed11`.

### Please provide a link to a minimal reproduction

No repo needed — the reproduction is a two-line call against the exported helper, included below.

### Describe the Bug

`sortObjectRecursive()` throws a `TypeError` when the object it is given contains an array with `null` in it.

The function guards against `null` for **object properties**, but not for **array elements**:

```ts
// packages/wrangler/src/utils/sortObjectRecursive.ts
if (Array.isArray(object)) {
return object.map((obj) => sortObjectRecursive(obj)) as T; // <-- null passed straight through
}

const objectCopy = { ...object };
for (const [key, value] of Object.entries(object)) {
if (typeof value === "object") {
if (value === null) { continue; } // <-- only protects direct properties
...
```

When a `null` array element is recursed into, `typeof null === "object"` passes the early return, `Array.isArray(null)` is false, and execution reaches `Object.entries(null)`, which throws.

Reproduction:

```js
sortObjectRecursive({ a: [null] });
// TypeError: Cannot convert undefined or null to object
```

**Expected:** `null` is preserved in place, i.e. `{ a: [null] }`, consistent with how `null` is already handled for object properties.

**Why it matters:** this helper normalises objects so they can be diffed and rendered. It is used on user-supplied configuration in the cloudchamber/containers commands (`sortObjectRecursive` feeds the config-diff rendering), so any config containing an array with a `null` entry crashes the command rather than showing a diff. JSON configs legitimately contain `null` inside arrays.

### Please provide any relevant error logs

```
TypeError: Cannot convert undefined or null to object
at Function.entries ()
at sortObjectRecursive (packages/wrangler/src/utils/sortObjectRecursive.ts)
```

Contributor guide

Open the contributing guide

Research direction

Start in packages/wrangler/src/utils/sortObjectRecursive.ts and run the two-line sortObjectRecursive({ a: [null] }) reproduction. Confirm that null array elements are preserved and the helper no longer throws while normalizing the configuration for diff rendering.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.