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

Feature Request: lula go import grouping #749

Open
mildwonkey opened this issue Oct 18, 2024 · 0 comments
Open

Feature Request: lula go import grouping #749

mildwonkey opened this issue Oct 18, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@mildwonkey
Copy link
Contributor

As a go developer, especially one new to a project, when I open a go file, I find it very helpful when I can easily read and reason about which stdlib, external, and internal dependencies we have. Sorting the local (github.com/defenseunicorns/lula) packages to the bottom of the imports list is a decently widespread practice and (in my biased opinion) the sorting makes codebases look more professional/polished.

Developers can automate this with goimports, adding the -local github.com/defenseunicorns/lula flag. Modern code editors (and emacs 😉) can be configured to do this automatically on save and the linters in #742 include goimports and gofmt, so we'll get errors (non-blocking - FOR NOW) if folks forget to sort imports.

There is one annoying recurring issue with the go import sorting - something which I've already fixed in a couple of files, so it's not specific to the local package sorting, but (especially when refactoring) sometimes you'll end up with extra newlines between blocks of imports that need to be manually removed. Neither goimports nor gofmt catch that because it's something we could validly and explicitly do - we could for e.g. also have a block of non-lula defense unicorns packages (not a recommendation). But it can get annoying and I wanted to be open about that.


The following is just way too much explanation no one asked for, why did I write this, i don't know 🤷🏻

Here's a sneak peak at the before and after (in case anyone is still on the fence and wants to see):

import (
	"os"
	"testing"
	"time"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
	"github.com/defenseunicorns/lula/src/internal/testhelpers"
	"github.com/defenseunicorns/lula/src/internal/tui/common"
	"github.com/defenseunicorns/lula/src/internal/tui/component"
	"github.com/defenseunicorns/lula/src/pkg/common/oscal"
	"github.com/muesli/termenv"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

vs

import (
	"os"
	"testing"
	"time"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	oscalTypes_1_1_2 "github.com/defenseunicorns/go-oscal/src/types/oscal-1-1-2"
	"github.com/muesli/termenv"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/defenseunicorns/lula/src/internal/testhelpers"
	"github.com/defenseunicorns/lula/src/internal/tui/common"
	"github.com/defenseunicorns/lula/src/internal/tui/component"
	"github.com/defenseunicorns/lula/src/pkg/common/oscal"
)

While I do absolutely admit to a generic sorting bias, I find the bottom example easier to read and I can get an idea of what's happening in this file before reading the code in a way I could not do before.

I only have vscode config off the top off my head, but:
Vscode config reference (not actually very helpful): https://github.com/golang/vscode-go/wiki/advanced#formatting-code-and-organizing-imports

The actual addition to settings.json:

    "gopls": {
        "formatting.local": "github.com/defenseunicorns/lula/"
    }

Or run goimports

@mildwonkey mildwonkey added the enhancement New feature or request label Oct 18, 2024
@github-actions github-actions bot added the triage Awaiting triage from the team label Oct 18, 2024
@brandtkeller brandtkeller removed the triage Awaiting triage from the team label Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: 🆕 New
Development

No branches or pull requests

2 participants