Skip to content

Invalid models.json syntax crashes during migration without showing the file path #5418

@ACAne0320

Description

@ACAne0320

What happened?

If ~/.pi/agent/models.json has invalid JSON, pi exits with a raw JSON.parse stack trace and does not mention models.json.

> pi
<anonymous_script>:20
i}
^

SyntaxError: Expected ',' or '}' after property value in JSON at position 524 (line 20 column 1)
    at JSON.parse (<anonymous>)
    at migrateModelsJsonConfigValues (.../dist/migrations.js:131:25)
    at migrateExplicitEnvVarConfigValues (.../dist/migrations.js:173:70)
    at runMigrations (.../dist/migrations.js:391:5)
    at main (.../dist/main.js:420:79)

The normal ModelRegistry.loadCustomModels() path already reports malformed models.json with the file path. This seems to happen earlier, in the config env reference migration added for #5095 , where migrateModelsJsonConfigValues() parses models.json directly.

I’d be happy to send a PR for this.
I think the fix is to wrap the migrateModelsJsonConfigValues() parse path in the same style as ModelRegistry.loadCustomModels(): catch SyntaxError, include the resolved models.json path in the error message.

Steps to reproduce

  1. Add invalid JSON to ~/.pi/agent/models.json
  2. Run pi

Expected behavior

The error should say which file failed to parse, e.g.

Error: models.json error: Failed to parse models.json: Expected ',' or '}' after property value in JSON at position 524 (line 20 column 1)
File: /home/user/.pi/agent/models.json

Version

0.78.1

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinginprogressIssue is being worked on

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions