From ee40ffa256076b5216aa36909846bed70f413a1e Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 20:10:41 +0000 Subject: [PATCH 1/2] lint: enforce aitools package layering with depguard ## 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 --- .golangci.yaml | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/.golangci.yaml b/.golangci.yaml index f05060c41ae..d0f08a756dc 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -61,6 +61,44 @@ linters: deny: - pkg: "github.com/databricks/cli/experimental" desc: "must not import experimental/ packages; use an interface or move the dependency" + # aitools layers: cmd/aitools (cobra commands) -> libs/aitools/installer + # -> libs/aitools/agents -> shared libs. Lower layers must not reach up. + aitools-libs-no-commands: + files: + - "**/libs/aitools/**" + deny: + - pkg: "github.com/databricks/cli/cmd$" + desc: "libs/aitools must not import command packages; pass what it needs in from cmd/aitools" + - pkg: "github.com/databricks/cli/cmd/" + desc: "libs/aitools must not import command packages; pass what it needs in from cmd/aitools" + aitools-commands-only-root: + list-mode: lax + files: + - "**/cmd/aitools/**" + allow: + - "github.com/databricks/cli/cmd/root" + - "github.com/databricks/cli/cmd/aitools" + deny: + - pkg: "github.com/databricks/cli/cmd/" + desc: "cmd/aitools may import only cmd/root and itself; share code through libs/aitools instead of another command group" + libs-no-aitools: + files: + - "**/libs/**" + - "!**/libs/aitools/**" + deny: + - pkg: "github.com/databricks/cli/libs/aitools" + desc: "shared libs must not depend on the aitools feature packages" + # cmd/root/root.go calls the aitools user-agent and hint hooks from the + # root pre-run and the auto-install hook from the root post-run; keep + # those calls in that one file. Tests may import aitools to stub them. + root-aitools-hooks-in-root-go: + files: + - "**/cmd/root/**" + - "!**/cmd/root/root.go" + - "!$test" + deny: + - pkg: "github.com/databricks/cli/libs/aitools" + desc: "aitools hooks belong in cmd/root/root.go only" no-legacy-rand: deny: - pkg: "math/rand$" From 5fae5cad96b985ddd5c737bd83494ad3b747cf05 Mon Sep 17 00:00:00 2001 From: Anton Nekipelov <226657+anton-107@users.noreply.github.com> Date: Thu, 19 Nov 2026 23:04:14 +0000 Subject: [PATCH 2/2] lint: anchor aitools depguard rules at the repository root and tighten 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 --- .golangci.yaml | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/.golangci.yaml b/.golangci.yaml index d0f08a756dc..2e593c475e1 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -61,43 +61,55 @@ linters: deny: - pkg: "github.com/databricks/cli/experimental" desc: "must not import experimental/ packages; use an interface or move the dependency" - # aitools layers: cmd/aitools (cobra commands) -> libs/aitools/installer - # -> libs/aitools/agents -> shared libs. Lower layers must not reach up. + # aitools layers: cmd/aitools (cobra commands) -> libs/aitools -> shared + # libs. Lower layers must not reach up. The aitools rules anchor their + # files patterns at ${config-path} (the repository root) so that a + # libs or cmd directory above the checkout does not match. aitools-libs-no-commands: files: - - "**/libs/aitools/**" + - "${config-path}/libs/aitools/**" deny: - pkg: "github.com/databricks/cli/cmd$" desc: "libs/aitools must not import command packages; pass what it needs in from cmd/aitools" - pkg: "github.com/databricks/cli/cmd/" desc: "libs/aitools must not import command packages; pass what it needs in from cmd/aitools" aitools-commands-only-root: + # lax: allow everything except cmd and cmd/*; the longer allow entries + # win over the cmd/ deny. list-mode: lax files: - - "**/cmd/aitools/**" + - "${config-path}/cmd/aitools/**" allow: - - "github.com/databricks/cli/cmd/root" - - "github.com/databricks/cli/cmd/aitools" + - "github.com/databricks/cli/cmd/root$" + - "github.com/databricks/cli/cmd/root/" + - "github.com/databricks/cli/cmd/aitools$" + - "github.com/databricks/cli/cmd/aitools/" deny: + - pkg: "github.com/databricks/cli/cmd$" + desc: "cmd/aitools may import only cmd/root and itself; share code through libs/aitools instead of another command group" - pkg: "github.com/databricks/cli/cmd/" desc: "cmd/aitools may import only cmd/root and itself; share code through libs/aitools instead of another command group" libs-no-aitools: files: - - "**/libs/**" - - "!**/libs/aitools/**" + - "${config-path}/libs/**" + - "!${config-path}/libs/aitools/**" deny: - - pkg: "github.com/databricks/cli/libs/aitools" + - pkg: "github.com/databricks/cli/libs/aitools$" + desc: "shared libs must not depend on the aitools feature packages" + - pkg: "github.com/databricks/cli/libs/aitools/" desc: "shared libs must not depend on the aitools feature packages" # cmd/root/root.go calls the aitools user-agent and hint hooks from the # root pre-run and the auto-install hook from the root post-run; keep # those calls in that one file. Tests may import aitools to stub them. root-aitools-hooks-in-root-go: files: - - "**/cmd/root/**" - - "!**/cmd/root/root.go" + - "${config-path}/cmd/root/**" + - "!${config-path}/cmd/root/root.go" - "!$test" deny: - - pkg: "github.com/databricks/cli/libs/aitools" + - pkg: "github.com/databricks/cli/libs/aitools$" + desc: "aitools hooks belong in cmd/root/root.go only" + - pkg: "github.com/databricks/cli/libs/aitools/" desc: "aitools hooks belong in cmd/root/root.go only" no-legacy-rand: deny: