Skip to content

fix(config): Fix flag parsing with space-separated value#6

Open
tampakrap wants to merge 1 commit intocrossplane:mainfrom
tampakrap:fix_config_flag
Open

fix(config): Fix flag parsing with space-separated value#6
tampakrap wants to merge 1 commit intocrossplane:mainfrom
tampakrap:fix_config_flag

Conversation

@tampakrap
Copy link
Copy Markdown
Collaborator

Description of your changes

While reviewing/testing #3 I noticed that the space-separated --config /path/to/config.yaml ignores the config. Reproducer:

# Create a new config file
go run ./cmd/crossplane --config=/tmp/xpcfg.yaml config set features.enableAlpha true

# Correctly prints the full content of the config
go run ./cmd/crossplane --config=/tmp/xpcfg.yaml config view

# (before the fix) prints only "version: 1" ignoring the config
go run ./cmd/crossplane --config /tmp/xpcfg.yaml config view

The problem is that strings.TrimPrefix returns the original string unchanged when the prefix doesn't match, so the v != "" check can not distinguish "prefix matched and value extracted" from "prefix not matched". For --config /path this makes configFlag return "--config" as the path instead of falling through to read the next argument.

Switching to strings.CutPrefix, which returns an ok bool that makes the distinction clear.

I have:

Need help with this checklist? See the cheat sheet.

While reviewing/testing crossplane#3 I noticed that the space-separated `--config
/path/to/config.yaml` ignores the config. Reproducer:

```bash
  # Create a new config file
  go run ./cmd/crossplane --config=/tmp/xpcfg.yaml config set features.enableAlpha true

  # Correctly prints the full content of the config
  go run ./cmd/crossplane --config=/tmp/xpcfg.yaml config view

  # (before the fix) prints only "version: 1" ignoring the config
  go run ./cmd/crossplane --config /tmp/xpcfg.yaml config view
```

The problem is that `strings.TrimPrefix` returns the original string
unchanged when the prefix doesn't match, so the `v != ""` check can not
distinguish "prefix matched and value extracted" from "prefix not
matched". For `--config /path` this makes `configFlag` return "--config"
as the path instead of falling through to read the next argument.

Switching to `strings.CutPrefix`, which returns an ok bool that makes
the distinction clear.

Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>
@tampakrap tampakrap requested review from a team and jcogilvie as code owners May 8, 2026 12:55
@tampakrap tampakrap requested review from adamwg and removed request for a team May 8, 2026 12:55
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 8, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The configFlag function in cmd/crossplane/main.go now uses strings.CutPrefix to parse --config= arguments, enabling recognition of the flag even when no value is provided after the =, rather than only matching when a non-empty suffix exists.

Changes

Config Flag Parsing Logic

Layer / File(s) Summary
Flag Parsing Logic
cmd/crossplane/main.go
The --config= detection switched from strings.TrimPrefix with an empty-value guard to strings.CutPrefix, allowing the flag to match and return empty values directly instead of falling back to the next argv element.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes


Important

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Feature Gate Requirement ❓ Inconclusive No result was produced after verification. Marking as INCONCLUSIVE. Re-run the check or adjust instructions to produce a final result.
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the fix—replacing TrimPrefix with CutPrefix to properly handle space-separated config flag values—and stays well within the 72-character limit.
Description check ✅ Passed The description is directly related to the changeset, providing a detailed reproducer, root cause analysis, and explanation of why strings.CutPrefix fixes the issue.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Breaking Changes ✅ Passed PR modifies configFlag() to fix space-separated flag parsing. No public fields/flags removed, renamed, or newly required. Fixes broken behavior, not removing it.

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
cmd/crossplane/main.go (1)

139-142: ⚡ Quick win

Optional: tighten the HasPrefix guard to avoid a potential false positive.

HasPrefix(a, "--config") would also match a future flag like --config-dir or --configs. If such a flag appeared before --config in argv and CutPrefix returned false, args[i+1] would be silently consumed as the config path. The function would work correctly today since no such flags exist, but it might be worth narrowing the guard for resilience.

Could you clarify whether restricting the early-continue filter to exactly --config and --config=... was considered? Something like:

🛡️ Proposed tightening of the HasPrefix guard
-		if !strings.HasPrefix(a, "--config") {
+		if a != "--config" && !strings.HasPrefix(a, "--config=") {
 			continue
 		}

This would make the logic self-consistent with the CutPrefix check on the next line and eliminate the reliance on "no other --config* flags exist today."

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@cmd/crossplane/main.go` around lines 139 - 142, The loop's early-continue
uses strings.HasPrefix(a, "--config") which could falsely match flags like
--config-dir; change the guard to only allow the exact flag or the equals form
by checking a == "--config" OR strings.HasPrefix(a, "--config=") (so the branch
matches either --config <value> or --config=<value>), keeping the existing
strings.CutPrefix usage for the equals case and still using args[i+1] for the
space-separated form; update the condition in the for-loop (the locals "a",
"args", and the strings.HasPrefix check) accordingly to avoid consuming
args[i+1] for other --config* flags.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@cmd/crossplane/main.go`:
- Around line 139-142: The loop's early-continue uses strings.HasPrefix(a,
"--config") which could falsely match flags like --config-dir; change the guard
to only allow the exact flag or the equals form by checking a == "--config" OR
strings.HasPrefix(a, "--config=") (so the branch matches either --config <value>
or --config=<value>), keeping the existing strings.CutPrefix usage for the
equals case and still using args[i+1] for the space-separated form; update the
condition in the for-loop (the locals "a", "args", and the strings.HasPrefix
check) accordingly to avoid consuming args[i+1] for other --config* flags.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: e7842535-e472-432b-a41d-09fbf241c87b

📥 Commits

Reviewing files that changed from the base of the PR and between 10bac99 and f44f2dc.

📒 Files selected for processing (1)
  • cmd/crossplane/main.go

Copy link
Copy Markdown
Member

@adamwg adamwg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks for fixing this. Might be worth adding a test as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants