ruff: per-file-ignores have inconsistent behavior in symlinked directories

I am trying to ignore the S101 rule in tests. I have a base ruff.toml which is used in pyproject.toml.

Adding this to ruff.toml doesn’t work:

[per-file-ignores]
"**/test/**" = ["S101"] # <- still warnings in tests

However, adding the same in pyproject.toml works:

[tool.ruff]
    extend = "/path/to/ruff.toml"

[tool.ruff.per-file-ignores]
"**/test/**" = ["S101"] # <- this works as expected

I tried without wildcards as well and extend-per-file-ignores. Nothing added to ruff.toml is considered.

Ruff version: 0.0.284

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 1
  • Comments: 18 (15 by maintainers)

Most upvoted comments

Thank you for all those interesting insights. My actual setup is not using symlinks (I was just using tmp for the minimal example). Instead, ruff is running in a docker, with the ruff config located at /presets/ruff.toml and the project mounted in /app.

I couldn’t make it work with the **/test/** regex, but it works if I use /app/**/test/** in the ruff.toml. The problem is, a CI may mount the folders somewhere else in the docker container, so this is not good enough.

I tried calling ruff with ruff check /app, but still now luck.

UPDATE: it seems adding a leading / works -> /**/test/**.

@derlin Thanks for the workaround! Adding a leading slash does work ("/*/tests/*" seems to be sufficient)

@charliermarsh With respect to ripgrep, I believe that code was originally written with std::env::current_dir, which is a platform independent way of getting the CWD. And then an issue was filed pointing out cases where that fails, for example, when using ripgrep in a directory that had been removed. So in that context, querying for PWD is treated as a “fallback” to try and find the CWD even when current_dir fails. Potentially flipping this to ask for PWD first and falling back to std::env::current_dir might indeed be the better path. I don’t think I considered symlinks in this context when writing current_dir

@qdegraaf added a minimal example of the problem: this has to do with the relative path taken from the ruff.toml location instead of the root of the project.

Sorry this wasn’t very clear. The problem actually seems to be when ruff.toml is not at the root of the python project that is linted. Find below a reproducible example.

The structure is:

/tmp
├── ruff-example
│   ├── .venv   # <- ruff is installed in this venv
│   ├── pyproject.toml 
│   └── src
│       └── test
│           └── foo.py
└── ruff.toml 

The content of /tmp/ruff.toml:

select = ['S']
[per-file-ignores]
"**/test/**" = ["S101"]

The content of /tmp/ruff-example/pyproject.toml:

[tool.ruff]
    extend = "/tmp/ruff.toml"

Running from the root of /tmp works as expected:

/tmp/ruff-example/.venv/bin/ruff /tmp/ruff-example

Running it from the root of the source directory ruff-example fails:

cd /tmp/ruff-example
/tmp/ruff-example/.venv/bin/ruff .

src/test/foo.py:1:1: S101 Use of `assert` detected
Found 1 error.