Ruff fix 'formatter' no longer works since 0.1.0
This concerns using ruff check --fix to fix code (#296) instead of the new ruff format command.
Since ruff 0.1.0, the functionality no longer works. This is because since https://github.com/astral-sh/ruff/pull/7838, ruff emits to stderr, which jupyterlab_code_formatter interprets as an error.
A potential fix is to add the -e and -q flags to the ruff call, which sets the exit code to 0 and suppresses stderr respectively.
class RuffFixFormatter(CommandLineFormatter):
@property
def label(self) -> str:
return f"Apply ruff Formatter"
def __init__(self):
try:
from ruff.__main__ import find_ruff_bin
ruff_command = find_ruff_bin()
except (ImportError, FileNotFoundError):
ruff_command = "ruff"
self.command = [ruff_command, "check", "-eq", "--fix-only", "-"]
I edited my installation of jupyterlab_code_formatter to the above and it works, but I haven't had a chance to test more systematically.
@felix-cw You might be able to shed some light with an issue I'm having 🙏.
I'm using this workaround: https://gist.github.com/jbwhit/eecdd1cac2756df85ad165f437445b0b
I'm having some issues with the fact that formatting through jyputerlab_code_formatter doesn't format the same way as if running ruff format from the command line... Mildly infuriating to say the least, and makes it really difficult to use in CI. Any ideas why? It should use the same binary and version, it's just one goes through jyputerlab_code_formatter and the other doesn't as far as I can tell. I'm also using jupytext, so maybe that's got something to do with it 🤔.
I'm afraid not. I know that this extension formats cell by cell, wheras using ruff directly can potentially see the whole notebook file during formatting. Whether or not this makes a difference I don't know.
Another idea could be an environment thing? I tend to have jupyter lab and extensions installed in a "global" conda environment as a host and use kernels from per-project environments. I think that this extension uses the ruff in the jupyter lab environment own environment which could lead to mismatches if you have a similar setup. I don't recall having this problem though, maybe because any mismatches got caught by pre-commit before CI.
I've never used jupytext so not sure what the impact could be.
It's probably as you say, that this extension formats cell by cell. The diff I see most often is that ruff likes to remove empty cells, which this extension does not. Thanks for taking the time to respond!
Closing as fixed by https://github.com/jupyterlab-contrib/jupyterlab_code_formatter/pull/333