feat(forge_config): inject $schema key into generated config for editor validation#2741
Open
tusharmath wants to merge 5 commits intomainfrom
Open
feat(forge_config): inject $schema key into generated config for editor validation#2741tusharmath wants to merge 5 commits intomainfrom
tusharmath wants to merge 5 commits intomainfrom
Conversation
Comment on lines
+32
to
+34
| let config_toml = toml_edit::ser::to_string_pretty(&self.config)?; | ||
| let contents = | ||
| format!("\"$schema\" = \"https://forgecode.dev/schema.json\"\n\n{config_toml}"); |
Contributor
There was a problem hiding this comment.
The $schema key is a JSON Schema convention that is not recognized by TOML parsers or editors. While the code successfully writes "$schema" = "https://forgecode.dev/schema.json" as a valid TOML key-value pair, this will not enable IDE validation, auto-complete, or inline documentation as described in the PR. Modern editors only recognize $schema for JSON files, not TOML files. The schema file forge.schema.json is a JSON Schema designed for JSON, but the config file being written is TOML format (using toml_edit::ser::to_string_pretty).
To fix this, either:
- Switch the config format from TOML to JSON, or
- Use a TOML-specific schema mechanism if one exists (though TOML has no standard schema validation like JSON Schema)
- Document that users need to manually configure their editor to associate the schema with TOML files (which most editors don't support)
Suggested change
| let config_toml = toml_edit::ser::to_string_pretty(&self.config)?; | |
| let contents = | |
| format!("\"$schema\" = \"https://forgecode.dev/schema.json\"\n\n{config_toml}"); | |
| let config_json = serde_json::to_string_pretty(&self.config)?; | |
| let mut schema_obj: serde_json::Value = serde_json::from_str(&config_json)?; | |
| if let serde_json::Value::Object(ref mut map) = schema_obj { | |
| map.insert( | |
| "$schema".to_string(), | |
| serde_json::Value::String("https://forgecode.dev/schema.json".to_string()), | |
| ); | |
| } | |
| let contents = serde_json::to_string_pretty(&schema_obj)?; |
Spotted by Graphite
Is this helpful? React 👍 or 👎 to let us know.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Automatically inject a
$schemakey into generated Forge config files, enabling real-time editor validation and IntelliSense auto-complete for all configuration fields.Context
Forge generates a TOML config file (
forge.yaml/forge.default.yaml) that users edit by hand, but editors had no way to validate its fields or offer completions. The project already ships aforge.schema.json, but nothing linked the generated config to that schema. This change wires the two together automatically so every generated config benefits from editor tooling without any manual setup.As a secondary improvement, the doc comments on every
ForgeConfigfield and the correspondingdescriptionstrings inforge.schema.jsonhave been rewritten for accuracy, completeness, and consistent style.Changes
crates/forge_config/src/writer.rs: Prepends"$schema" = "https://forgecode.dev/schema.json"as the first key of every config file written byConfigWriter::write. The rest of the serialized TOML follows unchanged.crates/forge_config/src/config.rs: Rewrote all Rust doc comments onForgeConfigfields to be more precise and consistent (e.g. clarifying units, edge-case behaviour whenNone, and correct capitalisation of "Forge").forge.schema.json: Synced alldescriptionstrings to match the updated Rust doc comments so the schema tooltips shown in editors are accurate.Key Implementation Details
The schema injection is done at the string level after TOML serialisation, keeping the change minimal and independent of
serdeortoml_editinternals. The schema URL (https://forgecode.dev/schema.json) is the canonical public location for the schema, so editors that support JSON Schema for TOML (VS Code with Even Better TOML, JetBrains IDEs, Neovim withSchemaStore, etc.) will fetch and apply it automatically.Use Cases
forge.default.yaml(or any generated config) in VS Code and get instant field descriptions, type checking, and auto-complete.Testing
Links
forge.schema.jsoncrates/forge_config/src/writer.rs