Skip to content

ref(options): backport sentry OptionsResolver from 5.x#2151

Merged
Litarnus merged 2 commits into
masterfrom
backport-options-resolver
Jul 14, 2026
Merged

ref(options): backport sentry OptionsResolver from 5.x#2151
Litarnus merged 2 commits into
masterfrom
backport-options-resolver

Conversation

@Litarnus

@Litarnus Litarnus commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

A while ago we introduced a custom OptionsResolver that works similar to the Symfony OptionsResolver but allows us to remove it as dependency and making the PHP SDK easier to install.

The new data collection configuration structure introduced nested configuration, which increased the necessity of the options resolver in the current major branch.

This PR replaces the symfony options resolver with the custom one but does not alter the required dependencies.

Comment thread src/Options.php
Comment thread src/OptionsResolver.php Outdated
Comment on lines +49 to +59

if (!$validation[0]) {
// Since defaults are used as fallback values if passed options are invalid, we want to
// get hard errors here to make sure we have something to fall back to.
throw new \InvalidArgumentException(\sprintf('Invalid default for option "%s"', $option));
}

$processed[$option] = $validation[1];
}

$this->defaults = $processed;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: An invalid SENTRY_DSN environment variable causes a generic InvalidArgumentException during SDK initialization, without explaining why the DSN is invalid.
Severity: HIGH

Suggested Fix

Modify the validation logic to catch the specific exception from Dsn::createFromString() and re-throw it or include its message in the InvalidArgumentException. This will provide developers with actionable information about why the DSN is considered invalid, restoring the helpfulness of the previous error reporting.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/OptionsResolver.php#L47-L59

Potential issue: The new `OptionsResolver` performs eager validation on default values
at construction time. If the `SENTRY_DSN` environment variable is set to an invalid
value, the `Options` constructor immediately throws a generic
`InvalidArgumentException('Invalid default for option "dsn"')`. This behavior is a
regression, as the previous implementation would fail later with a more descriptive
error. The current generic exception prevents developers from understanding what is
wrong with the DSN string, hindering debugging.

Did we get this right? 👍 / 👎 to inform future reviews.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want reviews to match your repository better? Bugbot Learning can learn team-specific rules from PR activity. A team admin can enable Learning in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 5b6ec69. Configure here.

Comment thread src/OptionsResolver.php
public function setDefault(string $name, $value): void
{
$validation = $this->normalizeAndValidate($name, $value);
$this->defaults[$name] = $validation[0] ? $validation[1] : null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Invalid defaults stored as null

Medium Severity

When a default fails validation, setDefaults/setDefault store null even if null is not an allowed type. That surfaces with server_name, whose default comes from gethostname() (can be false): the option becomes null, then getServerName() hits a TypeError during event capture outside the transport try/catch, which can crash the host app instead of failing soft.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 5b6ec69. Configure here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is by design

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We have tests to assert the defaults are correct and we only have the null fallback for values derived from the env

Comment thread src/Options.php
Comment on lines 155 to 161
*/
public function setEnableLogs(?bool $enableLogs): self
{
$options = array_merge($this->options, ['enable_logs' => $enableLogs]);

$this->options = $this->resolver->resolve($options);

return $this;
return $this->updateOptions(['enable_logs' => $enableLogs]);
}

/**

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The setEnableLogs method accepts null, but the resolver is not configured to allow null for enable_logs, causing it to silently fall back to the default value.
Severity: MEDIUM

Suggested Fix

Update the OptionsResolver configuration for enable_logs to allow null values. Change $resolver->setAllowedTypes('enable_logs', 'bool'); to $resolver->setAllowedTypes('enable_logs', ['null', 'bool']);. This will align the resolver's behavior with the method's type hint.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/Options.php#L155-L161

Potential issue: The `setEnableLogs` method signature is `setEnableLogs(?bool
$enableLogs)`, indicating it accepts `null`. However, the underlying `OptionsResolver`
is configured to only allow `'bool'` for the `enable_logs` option, not `null`. When
`null` is passed to the setter, the resolver silently discards the `null` value and
falls back to the default value of `false`. This is a behavioral regression from the
previous implementation which would have thrown an exception, and it leads to the option
being set incorrectly without any warning.

@Litarnus
Litarnus merged commit eda729b into master Jul 14, 2026
52 checks passed
@Litarnus
Litarnus deleted the backport-options-resolver branch July 14, 2026 12:44
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