Repository navigation
lint: enforce aitools package layering with depguard - #7829
Conversation
## Changes Adds four depguard rules to `.golangci.yaml` that pin down which packages the aitools code may import: - `aitools-libs-no-commands`: nothing under `libs/aitools/` may import `github.com/databricks/cli/cmd` or any package below it. - `aitools-commands-only-root`: `cmd/aitools/` may import only `cmd/root` and `cmd/aitools` from the `cmd/` tree. External test packages in `cmd/aitools` can still import the package they test. - `libs-no-aitools`: other packages under `libs/` may not import `libs/aitools`. - `root-aitools-hooks-in-root-go`: in `cmd/root/`, only `root.go` may import `libs/aitools`. That is where the root pre-run and post-run call the aitools user-agent, hint and auto-install hooks. Test files are exempt so they can stub those hooks. The config has no other changes. ## Why The aitools code is layered: the cobra commands in `cmd/aitools` sit on top of `libs/aitools`, which sits on top of the shared libraries. Right now nothing stops a lower layer from importing a higher one, or another command group from coupling to aitools. These rules turn the intended direction into a lint error, so a wrong import fails CI and the error message points to where the code should go. They also keep the aitools hooks in the root command limited to a single file. ## Tests - `golangci-lint run --enable-only depguard ./...` reports 0 issues on this branch, so existing code already follows the rules. - Only `.golangci.yaml` changes. No Go code or behavior changes. Co-authored-by: Isaac <no-reply@databricks.com>
There was a problem hiding this comment.
Automated review at ee40ffa. Each finding was verified against the code.
No blocking issues found.
This PR adds four depguard rules to .golangci.yaml that enforce the intended layering of the aitools packages: libs/aitools cannot import cmd/, cmd/aitools can only import cmd/root and itself, other libs/ packages cannot import libs/aitools, and only cmd/root/root.go (plus tests) may import libs/aitools from cmd/root. The rules are syntactically correct, the glob patterns and package matchers are well-formed, and the configuration follows the existing depguard conventions in the file. I found no concrete defects that would warrant blocking or improving the change.
Co-authored-by: Isaac <no-reply@databricks.com>
✅ Integration test reportCommit: 3801369
Top 3 slowest tests (at least 2 minutes):
|
simonfaltum
left a comment
There was a problem hiding this comment.
🤖 Automated review from simonfaltum's review agent (pr-review skill) at ee40ffa. Each finding was verified against the code; simonfaltum may follow up.
No blocking issues found, approving. 5 non-blocking comment(s) and 3 question(s) below.
This PR adds four depguard rules that enforce the aitools package layering. Checked against the pinned depguard and golangci-lint source, the rules work as described: list-mode: lax, the longest-match allow/deny resolution, the cmd$ exact match and the !$test exclusion all behave correctly, and the full-repo lint in CI passes with 0 issues. Nothing is blocking. The main suggestion is to anchor the new files globs with ${config-path}, so a checkout under a libs directory doesn't produce false lint failures. The rest are nits on matching precision and on comments that describe more than the rules enforce, plus three non-blocking questions: whether other command groups should be restricted, the file pin in cmd/root, and negative checks for the rules.
Questions
- The Why section says these rules stop "another command group from coupling to aitools", but they only limit what
cmd/aitoolsitself imports. Todaycmd/apps/init.go,cmd/version/version.goandcmd/air/image.goimportlibs/aitools,experimental/aitools/cmd/aitools.goimportscmd/aitools, and nothing stops a new command group from doing the same. Should the description just change, or did you mean to add a rule on**/cmd/**that deniesgithub.com/databricks/cli/cmd/aitools(exemptingcmd/cmd.go,main.goandexperimental/aitools/cmd)? (non-blocking) root-aitools-hooks-in-root-gopins which file incmd/rootmay call aitools, butcmd/rootstill depends onlibs/aitoolsas a package.cmd/rootsplits user-agent code intouser_agent_*.gofiles, so moving theinstaller.With*InUserAgentcalls into auser_agent_aitools.gowould fail lint even though the dependency is the same. Did you consider registering these hooks frommain.go, the wayautoinstall.Runalready is (main.go:29), so a plain "cmd/root must not import libs/aitools" rule could apply? Or is the file pin worth its friction? (non-blocking)- The Tests section shows only that the rules raise no false errors today. A mistyped glob or pkg would also report 0 issues and leave a rule silently dead. Could you add a one-off negative check per rule to the description, for example temporarily importing
cmd/authfromcmd/aitoolsandlibs/aitools/installerfromcmd/root/auth.go, and confirm depguard reports each with the expected message? (non-blocking)
…n matches
Anchor the aitools rules' files patterns at ${config-path} so a libs or cmd
directory above the checkout does not match. Deny the top-level cmd package
from cmd/aitools and match cmd/root, cmd/aitools and libs/aitools exactly or
by subpackage, so similarly named packages are not caught. Narrow the layering
comment to the boundaries the rules check and explain the lax list mode.
Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
❌ Integration test reportCommit: 3e563c9
27 interesting tests: 23 RECOVERED, 4 flaky
Top 50 slowest tests (at least 2 minutes):
|
Changes
Adds four depguard rules to
.golangci.yamlthat pin down which packages the aitools code may import:aitools-libs-no-commands: nothing underlibs/aitools/may importgithub.com/databricks/cli/cmdor any package below it.aitools-commands-only-root:cmd/aitools/may import onlycmd/rootandcmd/aitoolsfrom thecmd/tree. External test packages incmd/aitoolscan still import the package they test.libs-no-aitools: other packages underlibs/may not importlibs/aitools.root-aitools-hooks-in-root-go: incmd/root/, onlyroot.gomay importlibs/aitools. That is where the root pre-run and post-run call the aitools user-agent, hint and auto-install hooks. Test files are exempt so they can stub those hooks.The config has no other changes.
Why
The aitools code is layered: the cobra commands in
cmd/aitoolssit on top oflibs/aitools, which sits on top of the shared libraries. Right now nothing stops a lower layer from importing a higher one, or another command group from coupling to aitools. These rules turn the intended direction into a lint error, so a wrong import fails CI and the error message points to where the code should go. They also keep the aitools hooks in the root command limited to a single file.Tests
golangci-lint run --enable-only depguard ./...reports 0 issues on this branch, so existing code already follows the rules..golangci.yamlchanges. No Go code or behavior changes.