Skip to content

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

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

Merged
merged 8 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions private/buf/bufctl/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ func (c *controller) GetTargetImageWithConfigs(
if err != nil {
return nil, err
}
lintConfig := bufconfig.DefaultLintConfigV2
breakingConfig := bufconfig.DefaultBreakingConfigV2
lintConfig := bufconfig.DefaultLintConfigV1
breakingConfig := bufconfig.DefaultBreakingConfigV1
bufYAMLFile, err := bufconfig.GetBufYAMLFileForPrefixOrOverride(
ctx,
bucket,
Expand Down
10 changes: 8 additions & 2 deletions private/bufpkg/bufconfig/buf_yaml_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ type BufYAMLFile interface {
ModuleConfigs() []ModuleConfig
// DefaultLintConfig returns the DefaultLintConfig for the File.
//
// For v1 buf.yaml files, this will be the default v1 lint config.
// For v1 buf.yaml files, there is only ever a single ModuleConfig, so that is returned.
// For v2 buf.yaml files, if a top-level lint config exists, then it will be the top-level
// lint config. Otherwise it is the default v2 lint config.
DefaultLintConfig() LintConfig
// DefaultBreakingConfig returns the DefaultBreakingConfig for the File.
//
// For v1 buf.yaml, this will be the default v1 breaking config.
// For v1 buf.yaml files, there is only ever a single ModuleConfig, so that is returned.
// For v2 buf.yaml files, if a top-level breaking config exists, then it will be the top-level
// breaking config. Otherwise it is the default v2 breaking config.
DefaultBreakingConfig() BreakingConfig
Expand Down Expand Up @@ -301,10 +301,16 @@ func (c *bufYAMLFile) ModuleConfigs() []ModuleConfig {
}

func (c *bufYAMLFile) DefaultLintConfig() LintConfig {
if c.defaultLintConfig == nil {
return c.moduleConfigs[0].LintConfig()
}
return c.defaultLintConfig
}

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.

}
return c.defaultBreakingConfig
}

Expand Down
2 changes: 1 addition & 1 deletion private/bufpkg/bufconfig/lint_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

)
)

Expand Down