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

fix --stdin behaviour to match the Rust version #310

Closed
brianmcgee opened this issue Jun 2, 2024 · 9 comments · Fixed by #348
Closed

fix --stdin behaviour to match the Rust version #310

brianmcgee opened this issue Jun 2, 2024 · 9 comments · Fixed by #348
Assignees
Labels
bug Something isn't working

Comments

@brianmcgee
Copy link
Member

Describe the bug

See #274 for more info, but the short version is that --stdin doesn't work as it's supposed to.

@brianmcgee brianmcgee added the bug Something isn't working label Jun 2, 2024
@brianmcgee brianmcgee self-assigned this Jun 2, 2024
@nifoc
Copy link

nifoc commented Jul 9, 2024

I don't think this is fixed, or maybe I'm running into a different issue?

Version 1:

$ treefmt --version
treefmt 0.6.1

$ cat home/config/nvim/after/ftplugin/elixir.fnl | treefmt elixir.fnl --stdin
[INFO ] running #fnlfmt
(let [lo vim.opt_local]
  (set lo.spell true)
  (set lo.textwidth 120)
  (set lo.colorcolumn :+1))

Version 2:

$ treefmt --version
treefmt v2.0.2

$ cat home/config/nvim/after/ftplugin/elixir.fnl | treefmt elixir.fnl --stdin
fnlfmt error:
File not found: _9bc10y_0w0000gn/T/1209928017.fnl

treefmt: error: formatting failure: formatter '/nix/store/ihkzf1njld48inl1kjsgdk1v61f1hqjx-fnlfmt-0.3.1/bin/fnlfmt' with options '[--fix]' failed to apply: exit status 1

Edit

Not related specifically to fnlfmt:

$ treefmt --version
treefmt v2.0.2

$ cat flake.nix | treefmt --stdin flake.nix
nixpkgs-fmt error:
No such file or directory (os error 2)

treefmt: error: formatting failure: formatter '/nix/store/0zgggwjqgxisg5ba8g902z18jkf580df-nixpkgs-fmt-1.3.0/bin/nixpkgs-fmt' with options '[]' failed to apply: exit status 1

@brianmcgee
Copy link
Member Author

@nifoc 🤔 thanks for the test case; I think I have some time this afternoon to look at this.

@brianmcgee brianmcgee reopened this Jul 9, 2024
@brianmcgee
Copy link
Member Author

@nifoc in the meantime can you run your test command with -vv: cat flake.nix | treefmt --stdin -vv flake.nix

I just checked out the v2.0.2 branch and ran the following from the root directory without issue:

✦ ❯ cat flake.nix | nix run .# -- --config-file test/examples/nixpkgs.toml --stdin flake.nix
{
  description = "Treefmt: once CLI to format your repo";

  nixConfig = {
    extra-substituters = [ "https://numtide.cachix.org" ];
    extra-trusted-public-keys = [ "numtide.cachix.org-1:2ps1kLBUWjxIneOy1Ik6cQjb41X0iXVXeHigGmycPPE=" ];
  };

  inputs = {
    blueprint = {
      url = "github:numtide/blueprint";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    devshell = {
      url = "github:numtide/devshell";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    gomod2nix = {
      url = "github:nix-community/gomod2nix";
      inputs.nixpkgs.follows = "nixpkgs";
      inputs.flake-utils.follows = "devshell/flake-utils";
    };
    flake-compat.url = "github:nix-community/flake-compat";
    nix-filter.url = "github:numtide/nix-filter";
    nix-github-actions = {
      url = "github:nix-community/nix-github-actions";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    treefmt-nix = {
      url = "github:numtide/treefmt-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs:
    let
      inherit (inputs.nixpkgs) lib;
    in
    inputs.blueprint
      {
        inherit inputs;
        prefix = "nix/";
        nixpkgs.config = {
          allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "terraform" ];
        };
      }
    // {
      githubActions = inputs.nix-github-actions.lib.mkGithubMatrix {
        checks =
          lib.getAttrs [
            "x86_64-linux"
            "x86_64-darwin"
          ]
            inputs.self.checks;
      };
    };
}

@brianmcgee
Copy link
Member Author

@nifoc and can you provide your treefmt.toml as well.

@nifoc
Copy link

nifoc commented Jul 9, 2024

Not sure if it's relevant, but I make use of treefmt using the flake-parts module and I'm on macOS.

I also tried treefmt without the flake-parts "wrapper" (but using the same config file) and ran into the same issue.

cat flake.nix | treefmt --stdin -vv flake.nix
DEBU format: config-file=/nix/store/p65hy55vr5pbhv765cp2j1zirs49phjp-treefmt.toml tree-root=/Users/daniel/.config/nixpkgs
DEBU format | nixpkgs-fmt: match: /var/folders/wf/l_vqfv6976qc30_9bc10y_0w0000gn/T/3014005140.nix
DEBU format | nixpkgs-fmt: executing: /nix/store/0zgggwjqgxisg5ba8g902z18jkf580df-nixpkgs-fmt-1.3.0/bin/nixpkgs-fmt _9bc10y_0w0000gn/T/3014005140.nix
nixpkgs-fmt error:
No such file or directory (os error 2)

treefmt: error: formatting failure: formatter '/nix/store/0zgggwjqgxisg5ba8g902z18jkf580df-nixpkgs-fmt-1.3.0/bin/nixpkgs-fmt' with options '[]' failed to apply: exit status 1

Config:

$ cat /nix/store/p65hy55vr5pbhv765cp2j1zirs49phjp-treefmt.toml
[formatter]
[formatter.fnlfmt]
command = "/nix/store/ihkzf1njld48inl1kjsgdk1v61f1hqjx-fnlfmt-0.3.1/bin/fnlfmt"
excludes = []
includes = ["*.fnl"]
options = ["--fix"]

[formatter.nixpkgs-fmt]
command = "/nix/store/0zgggwjqgxisg5ba8g902z18jkf580df-nixpkgs-fmt-1.3.0/bin/nixpkgs-fmt"
excludes = []
includes = ["*.nix"]
options = []

[formatter.shfmt]
command = "/nix/store/whzcdk13r9l10nbs24dbld7395vhma2v-shfmt-3.8.0/bin/shfmt"
excludes = []
includes = ["*.sh", "*.bash", "*.envrc"]
options = ["-i", "2", "-s", "-w"]

[formatter.yamlfmt]
command = "/nix/store/3k0q89mvqmn833vkchy4k45xk897sdva-yamlfmt-0.13.0/bin/yamlfmt"
excludes = []
includes = ["*.yaml", "*.yml"]
options = []

[global]
excludes = ["*.lock", "*.patch", ".gitignore", ".gitmodules", ".hgignore", ".svnignore"]

@brianmcgee
Copy link
Member Author

The path that was matched:

DEBU format | nixpkgs-fmt: match: /var/folders/wf/l_vqfv6976qc30_9bc10y_0w0000gn/T/3014005140.nix

The same path passed to the formatter is truncated:

DEBU format | nixpkgs-fmt: executing: /nix/store/0zgggwjqgxisg5ba8g902z18jkf580df-nixpkgs-fmt-1.3.0/bin/nixpkgs-fmt _9bc10y_0w0000gn/T/3014005140.nix

I ran this on my mac:

cat flake.nix | result/bin/treefmt --config-file test/examples/nixpkgs.toml  -stdin flake.nix -vv
DEBU format: config-file=/Users/brian/Development/com/github/numtide/treefmt/test/examples/nixpkgs.toml tree-root=/Users/brian/Development/com/github/numtide/treefmt/test/examples
DEBU format | nixpkgs-fmt[2]: match: /var/folders/3r/0x4fl3z97l3ffql__8xst2cm0000gn/T/3984232970.nix
DEBU format | deadnix[1]: match: /var/folders/3r/0x4fl3z97l3ffql__8xst2cm0000gn/T/3984232970.nix
DEBU format | deadnix[1]: executing: /nix/store/wlqykhn3yvq9hcn8sf1d2s34d732cjfy-devshell-dir/bin/deadnix --edit ../../../../../../../../../var/folders/3r/0x4fl3z97l3ffql__8xst2cm0000gn/T/3984232970.nix
INFO format | deadnix[1]: 1 files processed in 5.322125ms
DEBU format | nixpkgs-fmt[2]: executing: /nix/store/wlqykhn3yvq9hcn8sf1d2s34d732cjfy-devshell-dir/bin/nixpkgs-fmt ../../../../../../../../../var/folders/3r/0x4fl3z97l3ffql__8xst2cm0000gn/T/3984232970.nix
INFO format | nixpkgs-fmt[2]: 1 files processed in 5.374042ms
{
  description = "Treefmt: once CLI to format your repo";

  nixConfig = {
    extra-substituters = [ "https://numtide.cachix.org" ];
    extra-trusted-public-keys = [ "numtide.cachix.org-1:2ps1kLBUWjxIneOy1Ik6cQjb41X0iXVXeHigGmycPPE=" ];
  };

  inputs = {
    blueprint = {
      url = "github:numtide/blueprint";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    devshell = {
      url = "github:numtide/devshell";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    gomod2nix = {
      url = "github:nix-community/gomod2nix";
      inputs.nixpkgs.follows = "nixpkgs";
      inputs.flake-utils.follows = "devshell/flake-utils";
    };
    flake-compat.url = "github:nix-community/flake-compat";
    nix-filter.url = "github:numtide/nix-filter";
    nix-github-actions = {
      url = "github:nix-community/nix-github-actions";
      inputs.nixpkgs.follows = "nixpkgs";
    };
    nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
    treefmt-nix = {
      url = "github:numtide/treefmt-nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = inputs:
    let
      inherit (inputs.nixpkgs) lib;
    in
    inputs.blueprint
      {
        inherit inputs;
        prefix = "nix/";
        nixpkgs.config = {
          allowUnfreePredicate = pkg: builtins.elem (lib.getName pkg) [ "terraform" ];
        };
      }
    // {
      githubActions = inputs.nix-github-actions.lib.mkGithubMatrix {
        checks =
          lib.getAttrs [
            "x86_64-linux"
            "x86_64-darwin"
          ]
            inputs.self.checks;
      };
    };
}

It is able to resolve the relative path to the temp file, unlike in your example. 🤔

@brianmcgee
Copy link
Member Author

@nifoc I'm pretty sure this is the culprit:

relPathFn := func(path string) (string, error) {
// quick optimisation for the majority of use cases
// todo check that root is a prefix in path?
if len(path) >= relPathOffset {
return path[relPathOffset:], nil
}
return filepath.Rel(f.root, path)
}

@brianmcgee
Copy link
Member Author

@nifoc I have a fix inbound #348

You should be able to try it out by adding a treefmt flake input with url = github:numtide/treefmt/fix/relative-path-resolution and override the package option in the flake.parts module to point to inputs.treefmt.packages.${system}.treefmt.

@nifoc
Copy link

nifoc commented Jul 10, 2024

Sorry for not being able to respond earlier.

It works for me, too. Thank you for the very quick fix!

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

Successfully merging a pull request may close this issue.

2 participants