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

goimports: nix flake check no longer works #318

Closed
jaredmontoya opened this issue Mar 3, 2025 · 9 comments
Closed

goimports: nix flake check no longer works #318

jaredmontoya opened this issue Mar 3, 2025 · 9 comments
Labels
bug Something isn't working

Comments

@jaredmontoya
Copy link

jaredmontoya commented Mar 3, 2025

Describe the bug
With the new release of GO and goimports in nixpkgs the goimports formatter in treefmt-nix now fails to execute nix flake check

To Reproduce

Steps to reproduce the behavior:

  1. Create a repository that has go code in it
  2. set up a nix flake with treefmt-nix
  3. you can use this lockfile to get the old nixpkgs version with the old working go/goimports release
{
  "nodes": {
    "flake-utils": {
      "inputs": {
        "systems": "systems"
      },
      "locked": {
        "lastModified": 1694529238,
        "narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
        "owner": "numtide",
        "repo": "flake-utils",
        "rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
        "type": "github"
      },
      "original": {
        "owner": "numtide",
        "repo": "flake-utils",
        "type": "github"
      }
    },
    "gomod2nix": {
      "inputs": {
        "flake-utils": "flake-utils",
        "nixpkgs": [
          "nixpkgs"
        ]
      },
      "locked": {
        "lastModified": 1733668782,
        "narHash": "sha256-tPsqU00FhgdFr0JiQUiBMgPVbl1jbPCY5gbFiJycL3I=",
        "owner": "nix-community",
        "repo": "gomod2nix",
        "rev": "514283ec89c39ad0079ff2f3b1437404e4cba608",
        "type": "github"
      },
      "original": {
        "owner": "nix-community",
        "repo": "gomod2nix",
        "type": "github"
      }
    },
    "nixpkgs": {
      "locked": {
        "lastModified": 1736344531,
        "narHash": "sha256-8YVQ9ZbSfuUk2bUf2KRj60NRraLPKPS0Q4QFTbc+c2c=",
        "owner": "nixos",
        "repo": "nixpkgs",
        "rev": "bffc22eb12172e6db3c5dde9e3e5628f8e3e7912",
        "type": "github"
      },
      "original": {
        "owner": "nixos",
        "ref": "nixos-unstable",
        "repo": "nixpkgs",
        "type": "github"
      }
    },
    "root": {
      "inputs": {
        "gomod2nix": "gomod2nix",
        "nixpkgs": "nixpkgs",
        "systems": "systems_2",
        "treefmt-nix": "treefmt-nix"
      }
    },
    "systems": {
      "locked": {
        "lastModified": 1681028828,
        "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
        "owner": "nix-systems",
        "repo": "default",
        "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
        "type": "github"
      },
      "original": {
        "owner": "nix-systems",
        "repo": "default",
        "type": "github"
      }
    },
    "systems_2": {
      "locked": {
        "lastModified": 1681028828,
        "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
        "owner": "nix-systems",
        "repo": "default",
        "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
        "type": "github"
      },
      "original": {
        "owner": "nix-systems",
        "repo": "default",
        "type": "github"
      }
    },
    "treefmt-nix": {
      "inputs": {
        "nixpkgs": [
          "nixpkgs"
        ]
      },
      "locked": {
        "lastModified": 1736154270,
        "narHash": "sha256-p2r8xhQZ3TYIEKBoiEhllKWQqWNJNoT9v64Vmg4q8Zw=",
        "owner": "numtide",
        "repo": "treefmt-nix",
        "rev": "13c913f5deb3a5c08bb810efd89dc8cb24dd968b",
        "type": "github"
      },
      "original": {
        "owner": "numtide",
        "repo": "treefmt-nix",
        "type": "github"
      }
    }
  },
  "root": "root",
  "version": 7
}
  1. try doing nix flake check and it will work
  2. execute nix flake update to update inputs
  3. try nix flake check and you should get an output similar to this:
ERRO formatter | goimports: failed to apply with options '[-w]': exit status 2

err: go command required, not found: exec: "go": executable file not found in $PATH: stderr: 
err: go command required, not found: exec: "go": executable file not found in $PATH: stderr: 
err: go command required, not found: exec: "go": executable file not found in $PATH: stderr: 
err: go command required, not found: exec: "go": executable file not found in $PATH: stderr: 
err: go command required, not found: exec: "go": executable file not found in $PATH: stderr: 

Instead of setting up a project from scratch you can also just perform nix flake check test before and after nix flake update on a real life example: https://github.com/danielmiessler/fabric

Expected behavior
It should work as it worked before

System information
NixOS unstable

Additional context

Maintainer ping
@gabyx

@jaredmontoya jaredmontoya added the bug Something isn't working label Mar 3, 2025
@gabyx
Copy link
Contributor

gabyx commented Mar 4, 2025

Thanks for the nice bug report.
I also will eventually run into this but have not so far.

Its pretty strange, apparently goimports needs a go executable and didnt before or might it be a packaging problem maybe in gotools? I have to check. So then it also should not work in a simple nix-shell?

@jaredmontoya
Copy link
Author

jaredmontoya commented Mar 4, 2025

What is weird to me is that nix fmt still works correctly while nix flake check fails.

@gabyx
Copy link
Contributor

gabyx commented Mar 4, 2025

@jaredmontoya : I checked ou current main today on https://github.com/danielmiessler/fabric and did a nix flake update and got the same error: nix fmt however works.

The treefmt file which is generated contains (I hope its the same as in check): cat /nix/store/fs6ql36dsjnk2bp9wqrbycvnagxql7hb-treefmt.toml:

[formatter.deadnix]
command = "/nix/store/h7jllc8dsyfy1bfhqr3bq1cdxms8ncdn-deadnix-1.2.1/bin/deadnix"
excludes = []
includes = ["*.nix"]
options = ["--edit"]

[formatter.gofmt]
command = "/nix/store/8zgc91q1igzgmg36mglxrk8qa1r2dvsd-go-1.23.6/bin/gofmt"
excludes = ["vendor/*"]
includes = ["*.go"]
options = ["-w"]

[formatter.goimports]
command = "/nix/store/8m595m24h551sa46h60mcbgmz1rqp3j4-gotools-0.30.0/bin/goimports"
excludes = ["vendor/*"]
includes = ["*.go"]
options = ["-w"]

[formatter.nixfmt]
command = "/nix/store/jh6h8md8zl2vsvr6bd3hgldm5p3kcfsl-nixfmt-unstable-2024-12-04/bin/nixfmt"
excludes = []
includes = ["*.nix"]
options = []

[formatter.statix]
command = "/nix/store/xpa3m7f826cmksbxlf5mymwn30zn7wkw-statix-fix/bin/statix-fix"
excludes = []
includes = ["*.nix"]
options = []

[global]
excludes = [
    "*.lock",
    "*.patch",
    "package-lock.json",
    "go.mod",
    "go.sum",
    ".gitignore",
    ".gitmodules",
    ".hgignore",
    ".svnignore",
]
on-unmatched = "warn"

When i executre goimports from that path it works. Not sure whats going on here.

@zimbatm
Copy link
Member

zimbatm commented Mar 4, 2025

Try running type -p go. If you get an answer it means that the go executable is on your $PATH.

@gabyx
Copy link
Contributor

gabyx commented Mar 17, 2025

@jaredmontoya : Still trying to figure out whats wrong. Apparently somehow the goimports needs go? I have a scenario where also just a nix run ...#treefmt does not work so its not only check.

@gabyx
Copy link
Contributor

gabyx commented Mar 17, 2025

@zimbatm: Do you have a solution for this. We could hack the package thing by doing pkgs.buildEnv { path = [ pkgs.goXXX pkgs.gotools]; }; or something to basically have go available. Or should that be fixed upstream in nixpkgs. Its just not sure how?

@gabyx
Copy link
Contributor

gabyx commented Mar 17, 2025

@zimbatm:

I came up with this:

{
  lib,
  pkgs,
  config,
  mkFormatterModule,
  ...
}:
let
  cfg = config.programs.goimports;

  command = pkgs.writeShellApplication {
    name = "goimports-with-go";
    runtimeInputs = [ cfg.goPackage ];
    text = ''
      "${cfg.package}/bin/goimports"
    '';
  };

in
{
  meta.maintainers = [ "gabyx" ];

  imports = [
    (mkFormatterModule {
      name = "goimports";
      package = "gotools";
      args = [ "-w" ];
      includes = [ "*.go" ];
      excludes = [ "vendor/*" ];
    })
    (
      {
        pkgs,
        lib,
        ...
      }:
      {
        options = {
          programs.goimports.goPackage = lib.mkPackageOption pkgs "goPackage" {
            default = "go_1_23";
            extraDescription = "The `go` package to use since `goimports` depends on a `go` compiler in the `PATH`.";
          };
        };
      }
    )
  ];

  config = lib.mkIf cfg.enable {
    settings.formatter.goimports = {
      command = "${command}/bin/goimports-with-go";
    };
  };
}

What do you think? (the module system is pretty cool, I think the above might work?)

But the example.sh does not generate the additional option right?

# Example generated by ../examples.sh
[formatter.goimports]
command = "goimports-with-go"
excludes = ["vendor/*"]
includes = ["*.go"]
options = ["-w"]

@jaredmontoya
Copy link
Author

jaredmontoya commented Mar 17, 2025

I think that if goimports indeed depends on go, then it has to be fixed in nixpkgs.

I am not sure if I implemented it in the best way possible but here is my nixpkgs PR that should fix this: NixOS/nixpkgs#390770

@jaredmontoya
Copy link
Author

PR merged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants