0

Nox runs commands via nox.Session.run. For example, this is a typical nox command:

import nox

@nox.session(venv_backend="none")
def black(session: nox.Session):
    session.run("black", "--check", ".")

Some tools are not amenable to running as a simple command, for example, clang-format. It is typically run as a find on C files and a pipe | into clang-format, like this:

find . -name "*.c" -print0 | xargs -0 clang-format --style=file --dry-run

How do I run this in Nox?

Plopping that in as one string does not work obviously:

@nox.session(venv_backend="none")
def clang_format(session: nox.Session):
    session.run('find . -name "*.c" -print0 | xargs -0 clang-format --style=file --dry-run')

Splitting it up into arguments does not work either obviously:

@nox.session(venv_backend="none")
def clang_format(session: nox.Session):
    session.run('find', '.', '-name', '"*.c"', '-print0', '|', 'xargs', '-0', 'clang-format', '--style=file', '--dry-run')

I understand why this does't work—the pipe is handled by the shell, not the OS. Is there a "shell mode" for Nox that is not documented? Or some other way to pipe commands in Nox?

drhagen
  • 8,331
  • 8
  • 53
  • 82

2 Answers2

0

I did not find a general solution, but for clang-format, this is solvable because find has the -exec capability.

import nox

@nox.session(venv_backend="none")
def clang_format(session: nox.Session):
    # Need to use + instead of ; because otherwise find will not error on clang-format errors
    session.run(
        "find",
        ".",
        "-name",
        "*.[ch]",
        "-exec",
        "clang-format",
        "--style=file",
        "--dry-run",
        "--Werror",
        "{}",
        "+",
    )

Note that the + on the end is important because otherwise find will not return a non-zero error code if ; is used.

drhagen
  • 8,331
  • 8
  • 53
  • 82
0

I would try to fork a shell to run the pipe command:

@nox.session(venv_backend="none")
def clang_format(session: nox.Session):
    session.run('bash','-c','find . -name "*.c" -print0 | xargs -0 clang-format --style=file --dry-run')

HTH

dod
  • 294
  • 1
  • 5