Repository navigation
Fix panic when devbox.lock contains null values - #3154
RyoKusnadi wants to merge 2 commits into
Conversation
Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
[Medium impact] The change reaches project commands that load lockfiles and multi-project lockfile sync. The PR appears safe to merge, with one non-blocking gap in the sync regression test.
|
| oldLock := write(filepath.Join(root, "old"), `{"lockfile_version": "1", "packages": {"go@latest": {"last_modified": "2024-01-01T00:00:00Z"}, "hello": null}}`) | ||
| write(filepath.Join(root, "new"), `{"lockfile_version": "1", "packages": {"go@latest": {"last_modified": "2024-06-01T00:00:00Z"}, "hello": null}}`) |
There was a problem hiding this comment.
Both fixtures set hello to null, so latestPackages drops that key before the update loop runs. This never exercises the new pkg != nil check in SyncLockfiles; removing that check would leave the test passing.
Add a valid, dated hello entry in one lockfile and keep hello: null in another. That would catch a regression in the second panic path this PR fixes.
Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/boxcli/multi/sync_test.go
Line: 78-79
Comment:
**Test misses second panic path**
Both fixtures set `hello` to `null`, so `latestPackages` drops that key before the update loop runs. This never exercises the new `pkg != nil` check in `SyncLockfiles`; removing that check would leave the test passing.
Add a valid, dated `hello` entry in one lockfile and keep `hello: null` in another. That would catch a regression in the second panic path this PR fixes.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Yeah, both fixtures had hello as null, so it never got past the latest-packages scan and the update loop check wasn't tested. Now the old lockfile has hello null and the new one has a valid hello, so it reaches the loop. It panics at sync.go:44 without the check. Fixed in 17f94bb.
Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
Summary
Problem: devbox panics (nil pointer or "assignment to entry in nil map") when
devbox.lockhas a JSONnullwhere an object is expected. Fixes #3101.How it happens: devbox never writes these nulls itself. They come from a hand edit, a merge conflict fixed by hand, or a script that rewrites the lockfile (a Go program that marshals a nil map writes
null). Once the file has one, any command that loads the project hits it.Steps to reproduce: put
{"lockfile_version": "1", "packages": null}(or"packages": {"hello@latest": null}, or"systems": {"x86_64-linux": null}) indevbox.lock, then rundevbox add hello.Expected: the null is treated like a missing entry.
Actual: panic.
Cause:
lock.GetFiledecodes straight intoFile, sonullleavesPackagesnil (Resolvethen writes to a nil map) and leaves nil*Package/*SystemInfoentries thatensurePackagesHaveOutputs,resolvedEntryandHasAllowInsecurePackagesdereference.Fix:
GetFilenow normalizes right after parsing: nilPackagesbecomes an empty map, and null package and system entries are dropped. A dropped package is resolved again on the next use, the same as one missing from the file. The issue title says "returning an error"; I chose to recover because the file has no real data for those entries, so there is nothing to refuse.Scope:
internal/boxcli/multi/sync.go(devbox updateacross several projects) reads lockfiles withoutGetFileand had the same nil dereference on a null package entry, so it now skips nulls. Otherlock.Fileusers go throughGetFile.How was it tested?
New tests:
TestGetFileNullValues(4 cases) andTestSyncLockfilesNullPackage. The sync test has one lockfile wherehellois null and another where it is valid, so the null entry reaches the update loop inSyncLockfiles; a third key is null everywhere to cover the latest-packages scan. Before the fix,TestGetFileNullValuesfails withPackages is nil, want an empty map, and the sync test panics with a nil pointer dereference inSyncLockfiles(sync.go:44 with the update-loop check removed, sync.go:90 with the scan check removed). After: pass.Commands:
GOTOOLCHAIN=local go vet,go test ./internal/lock/ ./internal/boxcli/multi/,gofmt -l,golangci-lint runon both packages (0 issues).Environment: fix branch @ 17f94bb, go1.27.4, darwin/arm64
Community Contribution License
All community contributions in this pull request are licensed to the project
maintainers under the terms of the
Apache 2 License.
By creating this pull request, I represent that I have the right to license the
contributions to the project maintainers under the Apache 2 License as stated in
the
Community Contribution License.