Skip to content
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

Set top-level LintConfig and BreakingConfig and use for image inputs #3111

Merged
merged 8 commits into from
Jun 27, 2024

Conversation

doriable
Copy link
Member

@doriable doriable commented Jun 25, 2024

This PR exposes the top-level lint and breaking configs for buf.yaml files.

For v1 buf.yaml files, there is only ever one ModuleConfig, so the
corresponding lint and breaking configs are used.

For v2 buf.yaml files, if a top-level lint and/or breaking config exists, then
we return those.
Otherwise, TopLevelLintConfig()/TopLevelBreakingConfig will return nil.

We use the top-level lint/breaking configs, if possible, with image inputs, since we
cannot determine which specific v2 module config to use with images. In
the case where there is no top-level config, we fall back to defaults.

Fixes #3080

@doriable doriable requested a review from bufdev June 25, 2024 15:53
@doriable
Copy link
Member Author

I've also tested this against the user repro in the issue.

@@ -34,7 +34,7 @@ var (
false,
false,
"",
false,
true, // We default to allowing comment ignores in v2
Copy link
Member Author

Choose a reason for hiding this comment

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

@pkwarren caught an issue with the default v2 lint config, which should default to allowing comment ignores (https://buf.build/docs/configuration/v2/buf-yaml)

  # v1 configurations had an allow_comment_ignores option to opt-in to comment ignores.
  #
  # In v2, we allow comment ignores by default, and allow opt-out from comment ignores
  # with the disallow_comment_ignores option.
  disallow_comment_ignores: false

private/buf/bufctl/controller.go Outdated Show resolved Hide resolved
private/bufpkg/bufconfig/buf_yaml_file.go Outdated Show resolved Hide resolved
private/bufpkg/bufconfig/buf_yaml_file.go Outdated Show resolved Hide resolved
private/bufpkg/bufconfig/buf_yaml_file.go Outdated Show resolved Hide resolved
private/bufpkg/bufconfig/buf_yaml_file.go Outdated Show resolved Hide resolved
Co-authored-by: Philip K. Warren <[email protected]>

func (c *bufYAMLFile) DefaultBreakingConfig() BreakingConfig {
if c.defaultBreakingConfig == nil {
return c.moduleConfigs[0].BreakingConfig()
Copy link
Member

Choose a reason for hiding this comment

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

This isn't actually correct - this is just choosing a single configuration among potentially many and calling it the default, which is what we've tried to avoid pretty consistently with v2. I think what you're actually looking for in this PR is some concept of "if there is a single configuration, give it to me, otherwise tell me it is not present". What this logic looks like:

SingleLintConfig() (LintConfig, bool)
  • If v1, return the v1 configuration.
  • If v2:
    • If there is one module with attached configuration, return the attached configuration. Else, return the top-level configuation.
    • If there is more than one module, return false

If you get false, do nothing, as we do right now.

Copy link
Member Author

@doriable doriable Jun 26, 2024

Choose a reason for hiding this comment

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

So if a user passes the following file to --config:

version: v2
lint:
  use:
    - MINIMAL
breaking:
  use:
    - WIRE
modules:
  - path: foo
  - path: bar
    lint:
      use:
        - DEFAULT
      except:
        - IMPORT_USED
  - path: vendor

Wouldn't we want them to pick up the top-level configs for the default in the case of:

$ buf breaking base.bin --against new.bin --config custom.yaml

Copy link
Member

Choose a reason for hiding this comment

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

We'd probably want to error, or at least print a warning along the lines of:

WARN Configuration {from flag,at $PATH was used} but this configuration had multiple modules. Buf cannot deduce which {lint,breaking} configuration to use with your image. Ignoring your configuration and using the default configuration instead.

I could see an argument to also modify the logic to "if >1 modules, return the top-level module", but that may just leave us open to even more support stuff in the long-term.

Going a different direction, we could take the position that for image inputs only, we always take the top-level configuration, and never look into specific module configs. This would mean that if you had the following:

version: v2
modules:
  - path: .
     lint: ...

The modules[0].LintConfig would not be picked up and propagated. The position here would be that module-specific configuration never applies to an image, and we treat configuration for an image as if it were a "new" module in the modules list.

This may be what moduleConfigs[0] does in practice for v1, but not for v2, but I'd also be comfortable with adopting this position, it seems intellectually consistent.

Copy link
Member Author

Choose a reason for hiding this comment

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

I am a fan of the latter approach for images. For v1, the behaviour will stay the same since there should only ever be a single lint/breaking config. For v2, we check for a top-level config, and if nothing is there, then we would use whatever the version defaults are. And yes, in the case you provided, where there is a single module, path: ., that would not be considered the default configuration. Changes incoming.

@doriable doriable changed the title Set top-level lint and breaking configs a defaults for v2 buf.yaml Set top-level module config and use for image inputs Jun 26, 2024
@doriable doriable changed the title Set top-level module config and use for image inputs Set top-level ModuleConfig and use for image inputs Jun 26, 2024
@doriable doriable requested review from bufdev and pkwarren June 26, 2024 19:40
// For v2 buf.yaml files, if a top-level module config exists, then it will be the top-level
// module config. Otherwise, this will return nil, so callers should be aware this may be
// empty.
TopLevelModuleConfig() ModuleConfig
Copy link
Member

Choose a reason for hiding this comment

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

Why is this a ModuleConfig? We only want LintConfig and BreakingConfig, I don't know what a top-level ModuleConfig is in terms of v2

Copy link
Member Author

Choose a reason for hiding this comment

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

I used ModuleConfig because it worked better for the warnings and also to account for situations like:

version: v2
name: <remote>/<owner>/<module>

But in that case, it would be the default anyway, so it doesn't super matter, and also since we're not printing out the warnings, I can swap it back to LintConfig and BreakingConfig.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated back to LintConfig and BreakingConfig, and updated the PR title + description.

@doriable doriable changed the title Set top-level ModuleConfig and use for image inputs Set top-level LintConfig and BreakingConfig and use for image inputs Jun 26, 2024
@doriable doriable requested a review from bufdev June 26, 2024 23:01
@doriable doriable merged commit f8b235a into main Jun 27, 2024
11 checks passed
@doriable doriable deleted the 3080-v2-buf-yaml-defaults-fix branch June 27, 2024 16:08
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.

buf 1.32.2 breaking doesn't respect v2 config file when using binpb input
4 participants