Skip to content

Commit 6b2cee9

Browse files
authored
fix: avoid unnecessary creation of .ansible folders (#4894)
1 parent 52e7d36 commit 6b2cee9

File tree

12 files changed

+33
-10
lines changed

12 files changed

+33
-10
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,7 @@ test/local-content
7979
test/schemas/node_modules
8080
node_modules
8181
.direnv
82-
.ansible
82+
83+
# ignore it only at repository root as we want to discover if these are
84+
# created accidentally in other folders.
85+
/.ansible

docs/configuring.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ in your current working directory.
2626
while the config may be found in a parent directory, the linter still
2727
expects to be executed from the root of the project being linted.**
2828

29+
Symlinked config files are followed and resolved and this will affect
30+
the how project path will be determined if not mentioned explicitly.
31+
2932
## Specifying configuration files
3033

3134
Use the `-c <filename>` CLI flag with command line invocations of Ansible-lint,

examples/playbooks/.ansible-lint-only-builtins-allow

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# yaml-language-server: $schema=https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/ansible-lint-config.json
22
# Mock modules or roles in order to pass ansible-playbook --syntax-check
3+
project_dir: ../..
34
mock_modules:
45
- fake_namespace.fake_collection.fake_module
6+
- zuul_return
57

68
only_builtins_allow_collections:
79
- fake_namespace.fake_collection

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ dev = [
9696
"tox-uv>=1.25",
9797
"types-jsonschema>=4.25.1.20250822",
9898
"types-pyyaml>=6.0.12.20250822",
99+
"uv>=0.8",
99100
]
100101
docs = ["markdown-exec[ansi]>=1.11.0", "mkdocs-ansible>=25.5.0", "six>=1.16.0"]
101102
lint = [

src/ansiblelint/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def initialize_options(arguments: list[str] | None = None) -> BaseFileLock | Non
144144
):
145145
options.cache_dir = get_cache_dir(pathlib.Path(options.project_dir))
146146

147+
options.project_dir = Path(options.project_dir).resolve().as_posix()
148+
147149
# add a lock file so we do not have two instances running inside at the same time
148150
if options.cache_dir:
149151
options.cache_dir.mkdir(parents=True, exist_ok=True)

src/ansiblelint/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(self, options: Options):
4848

4949
# Without require_module, our _set_collections_basedir may fail
5050
self.runtime = Runtime(
51+
project_dir=Path(options.project_dir),
5152
isolated=True,
5253
require_module=True,
5354
verbosity=options.verbosity,

src/ansiblelint/cli.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def load_config(
8080
if not config_path or not os.path.exists(config_path):
8181
# a missing default config file should not trigger an error
8282
return {}, None
83+
# resolve symlinks in config path as we will only use the final location
84+
config_path = Path(config_path).resolve().as_posix()
8385

8486
config_lintable = Lintable(
8587
config_path,
@@ -99,6 +101,15 @@ def load_config(
99101
config_dir = os.path.dirname(config_path)
100102
expand_to_normalized_paths(config, config_dir)
101103

104+
if (
105+
"project_dir" in config
106+
and config["project_dir"]
107+
and config["project_dir"][0] not in ("/", "~")
108+
):
109+
config["project_dir"] = (
110+
(Path(config_path).parent / config["project_dir"]).resolve().as_posix()
111+
)
112+
102113
return config, config_path
103114

104115

src/ansiblelint/rules/only_builtins.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ def test_only_builtins_allow() -> None:
9090
result = run_ansible_lint(
9191
f"--config-file={conf_path}",
9292
"--strict",
93+
"--offline",
94+
"--nocolor",
9395
"--warn-list=",
9496
"--enable-list",
9597
"only-builtins",

src/ansiblelint/schemas/__store__.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json"
2525
},
2626
"meta": {
27-
"etag": "d2f353a0077365ff075b14876b619ef65e7268129fd12b4f1b571549f2f39f14",
27+
"etag": "fa83d61b00b254d54a7c9a69e4fb8b722f356adec906da7beb733cadda1342bc",
2828
"url": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/meta.json"
2929
},
3030
"meta-runtime": {

test/test_main.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,24 +183,23 @@ def test_list_tags() -> None:
183183
assert isinstance(item, str)
184184

185185

186-
def test_ro_venv() -> None:
186+
def test_ro_venv(tmp_path: Path) -> None:
187187
"""Tests behavior when the virtual environment is read-only."""
188188
tox_work_dir = os.environ.get("TOX_WORK_DIR", ".tox")
189189
venv_path = f"{tox_work_dir}/ro"
190-
prerelease_flag = "" if sys.version_info < (3, 14) else "--pre "
191190
commands = [
192191
f"mkdir -p {venv_path}",
193192
f"chmod -R a+w {venv_path}",
194193
f"rm -rf {venv_path}",
195-
f"python -m venv --symlinks {venv_path}",
196-
f"{venv_path}/bin/python -m pip install {prerelease_flag}-q -e .",
194+
f"uv venv --seed --no-project {venv_path}",
195+
f"VIRTUAL_ENV={venv_path} uv pip install -q -e .",
197196
f"chmod -R a-w {venv_path}",
198197
# running with a ro venv and default cwd
199198
f"{venv_path}/bin/ansible-lint --version",
200199
# running from a read-only cwd:
201200
f"cd / && {abspath(venv_path)}/bin/ansible-lint --version", # noqa: PTH100
202201
# running with a ro venv and a custom project path in forced non-online mode, so it will need to install requirements
203-
f"{venv_path}/bin/ansible-lint -vv --no-offline --project-dir ./examples/reqs_v2/ ./examples/reqs_v2/",
202+
f"{venv_path}/bin/ansible-lint -vv --nocolor --no-offline --project-dir {tmp_path.as_posix()} ./examples/reqs_v2/",
204203
]
205204
for cmd in commands:
206205
result = subprocess.run(

0 commit comments

Comments
 (0)