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

workflows/check-nixf-tidy.yml: init #330066

Merged
merged 1 commit into from
Jul 27, 2024
Merged

Conversation

Aleksanaa
Copy link
Member

@Aleksanaa Aleksanaa commented Jul 26, 2024

Description of changes

CC @inclyc @Sigmanificient (I saw you were working on such clean-ups) @rhendric (#313981 (comment))

Here is an example test: https://github.com/Aleksanaa/nixpkgs/pull/1/files

This check will fail on:

  1. Parsing error (Thanks to the recursive decent parser, it is possible to get better error reporting than nix itself) https://github.com/nix-community/nixd/tree/main/libnixf#background--motivation
  2. Unused variables (function arguments, variable definition, unused rec, with, etc). This addresses Tracking issue: Removal of uneeded argument #313981
  3. Escaping with (reference variables outside of with expression). This also partially addresses Tracking issue: remove overuses of with lib; #208242

It uses nixf-tidy in nixf package. The json output is processed by jq to form GitHub workflow messages, which will be rendered in PR's 'Files changed' overview.

Like the workflow of nixfmt check, this workflow only fails when regression occurs (that is, the old file has no errors, but errors occur after the change; or the new file has errors). This is to avoid some larger files that cannot pass nixf-tidy (such as all-packages.nix). A feature that filters errors based on the lines affected (in git diff) may be implemented, but may require nixf improvements and more testing to work well.


Add a 👍 reaction to pull requests you find important.

@Sigmanificient
Copy link
Member

Well i did't knew about nixf-tidy, used deadnix --json for cleanup, but it can report false negatives

@Aleksanaa Aleksanaa force-pushed the ci-nixf-tidy branch 8 times, most recently from 2323b7e to 1c24741 Compare July 26, 2024 11:38
@Aleksanaa Aleksanaa marked this pull request as ready for review July 26, 2024 12:00
@Aleksanaa Aleksanaa requested review from Mic92, zowoq and a team as code owners July 26, 2024 12:00
@Aleksanaa Aleksanaa changed the title [WIP] workflow/check-nixf-tidy.yml: init workflow/check-nixf-tidy.yml: init Jul 26, 2024
name: Check changed Nix files with nixf-tidy (experimental)

on:
pull_request_target:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to run on the pull_request_target? Would pull_request not also work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good overall!

.github/workflows/check-nixf-tidy.yml Outdated Show resolved Hide resolved
.github/workflows/check-nixf-tidy.yml Outdated Show resolved Hide resolved
.github/workflows/check-nixf-tidy.yml Outdated Show resolved Hide resolved
@Sigmanificient
Copy link
Member

Sigmanificient commented Jul 27, 2024

for the CI output, maybe printing the line in question would be wanted?
This would provide more context than just the affected line/col

As an example:

  [line 30, column 38 ~ line 30, column 45] sema-escaping-with: this variable comes from the scope outside of the `with` expression
    [line 5, column 2 ~ line 5, column 9] var-bind-to-this: variable will bind to this definition

would become

  [line 30, column 38 ~ line 30, column 45]
  `     maintainers = with maintainers; [ version ];`
  sema-escaping-with: this variable comes from the scope outside of the `with` expression

    [line 5, column 2 ~ line 5, column 9]
    `  version = "0.35.1";`
    var-bind-to-this: variable will bind to this definition

@infinisil
Copy link
Member

Ohh! GitHub Actions can actually do this very nicely with a workflow command:

echo "::warning file=app.js,line=1,col=5,endColumn=7::Missing semicolon"

This should highlight the problematic lines in the file view of PRs!

@Sigmanificient
Copy link
Member

Yeah that would be even better

@Aleksanaa Aleksanaa force-pushed the ci-nixf-tidy branch 2 times, most recently from b482882 to e2bea1c Compare July 27, 2024 01:50
@Aleksanaa
Copy link
Member Author

Ohh! GitHub Actions can actually do this very nicely with a workflow command:

echo "::warning file=app.js,line=1,col=5,endColumn=7::Missing semicolon"

This should highlight the problematic lines in the file view of PRs!

Great idea! It works now: https://github.com/Aleksanaa/nixpkgs/pull/1/files

@infinisil

This comment was marked as resolved.

@Aleksanaa
Copy link
Member Author

Still another problem, it looks like the old error message is not cleared when the new error message appears. If you change the title or the like, multiple error messages may be generated

@Sigmanificient
Copy link
Member

Sigmanificient commented Jul 27, 2024

will an empty maintainer list mantainers = with lib.mantainers; [] tigger a warning CI?
We might want to filter this one if it causes problem

@Aleksanaa
Copy link
Member Author

will an empty maintainer list mantainers = with lib.mantainers; [] tigger a warning CI? We might want to filter this one if it causes problem

Yes, it's unused with. But I want it to report this error because we don't really need that with. Also, it will not throw new errors for files that already contain errors before editing.

@infinisil
Copy link
Member

Still another problem, it looks like the old error message is not cleared when the new error message appears. If you change the title or the like, multiple error messages may be generated

This is getting more messy, but I believe you should be able to only conditionally output the errors using something like

if [[ "${{github.event_name}}" == 'edited' ]]; then
  echo "Edited the PR"
  if [[ -z "${{github.event.edited.changes.base}}" ]]; then
    echo "But didn't change the base branch, only the description/title"
    # Don't output errors
  else
    # Do output errors, rare case of changing the base branch, which might cause different errors. Can't void the old ones unfortunately though, but should happen rarely.
  fi
else
  # Do output errors
fi

@Aleksanaa
Copy link
Member Author

This is getting more messy, but I believe you should be able to only conditionally output the errors using something like

This works now

@Aleksanaa Aleksanaa force-pushed the ci-nixf-tidy branch 2 times, most recently from b79bf7c to 365b08c Compare July 27, 2024 07:31
@Aleksanaa Aleksanaa changed the title workflow/check-nixf-tidy.yml: init workflows/check-nixf-tidy.yml: init Jul 27, 2024
@infinisil infinisil mentioned this pull request Jul 27, 2024
1 task
Copy link
Member

@infinisil infinisil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks ready to me, nice work! While this is a rather fresh PR, I don't think we need to wait for anything else, let's just try out and see how well it works 🚀

@infinisil infinisil merged commit c1d3cc5 into NixOS:master Jul 27, 2024
28 checks passed
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/we-added-a-linter-to-nixpkgs-checking-workflows/49722/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants