-
Notifications
You must be signed in to change notification settings - Fork 3.6k
fix(config): resolve {file:path} template shadowing bug #5779
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Conversation
…comments
Fixes a bug where {file:path} template expansions fail when the same
file path appears earlier in a commented line. The issue was caused by
findIndex() always returning the first occurrence of a match,
regardless of which specific occurrence was being processed.
Changed the logic to:
1. Track all match positions and their line numbers upfront
2. Process matches in reverse order to preserve string indices
3. Check each specific occurrence's line for comments
This ensures that commented-out file references don't prevent valid
references from being expanded.
Adds reproduction test case demonstrating the shadowing bug.
Refactors the config loading system to parse JSONC first, then expand
{env:VAR} and {file:path} templates within the resulting object tree.
Benefits of this approach:
- JSONC parser correctly handles comments (no manual checking needed)
- No string injection vulnerabilities
- Direct value replacement is safer than string manipulation
- Template expansion is context-aware
- Cleaner separation of concerns
The new architecture:
1. Parse JSONC text into JavaScript object
2. Walk the object tree recursively
3. Expand templates in string values only
This eliminates the fragile comment detection logic and makes the
template expansion more robust and maintainable.
|
/review |
|
There are 3 distinct commits - it might be a change bigger than wanted.
The first commit should be a standalone fix and should be working as is. If the change is too big, can discard the other 2 commits. |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
Did ur agent write that comment? Haha |
|
haha nope it didn't! I wrote it! it's my first time contributing here -- I came across some articles talking about the conservative approach to contributions! wanted to give a head's up so that it's easier to review! |
|
but the PR is description and code are 100% agent written 😬 |
03e3b91 to
31b5834
Compare
|
Haha sounds good |
bd8830c to
f98c605
Compare
|
@rekram1-node updated the code according with the generate command ran. |
|
hey @rekram1-node just wanted to check - is there anything else needed from me to get this merged? just making sure that it isn't getting stalled by something I should have done but didn't do. |
Summary
Fixes a bug where
{file:path}template expansions fail when the same file path appears earlier in a commented line.Closes #5731
Root Cause
The config loader performs string-based template expansion before JSON parsing. When multiple instances of the same
{file:path}exist in the config file,findIndexalways returns the index of the first occurrence. If the first occurrence is in a commented line, the loader skips all subsequent (valid) instances of that same file reference.Example of the Bug
{ "agent": { // "experimental": { // "prompt": "{file:./prompts/precise.md}" // ← findIndex finds this first // }, "precise": { "prompt": "{file:./prompts/precise.md}" // ← This never gets expanded } } }Location
packages/opencode/src/config/config.ts, theloadfunctionProblematic Code
Solution
This PR includes two commits:
1. Minimal Fix (Commit 1)
This ensures that commented-out file references don't prevent valid references from being expanded.
2. Parse-First Refactor (Commit 2)
Refactors the architecture to be more robust and maintainable:
Benefits:
Testing
Changes
packages/opencode/src/config/config.ts: Config loading implementationpackages/opencode/test/config/config.test.ts: New test casesArchitectural Issues Resolved
The original approach had several problems:
startsWith("//")doesn't understand JSON comment rulesJSON.stringify().slice(1, -1)The new parse-first architecture resolves all of these issues.
Related