fix(changelog): restore cwd-relative resolution for --file-name CLI arg - #1987
fix(changelog): restore cwd-relative resolution for --file-name CLI arg#1987bearomorphism wants to merge 2 commits into
Conversation
Closes commitizen-tools#1331 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
When --file-name is passed on the command line, the path should be interpreted relative to the current working directory, as is standard for CLI tools. When changelog_file is read from the configuration file it should be anchored to the config file's directory so the result is stable regardless of where cz is invoked. PR commitizen-tools#1391 / v4.6.3 inadvertently applied the config-dir anchor to BOTH sources, breaking setups where users specify a cwd-relative path via --file-name (e.g. from a Sphinx docs subdirectory). Changes: - commitizen/commands/changelog.py: distinguish args-provided file_name (cwd-relative) from config-provided changelog_file (config-dir-relative) - commitizen/commands/bump.py: only forward file_name to Changelog when it was explicitly supplied via CLI; otherwise let Changelog perform its own config lookup so the anchor logic is applied correctly - tests/test_changelog.py: replace the PR-commitizen-tools#1391 test with a parametrised regression test covering all four combinations - docs/commands/changelog.md: document the two resolution strategies Fixes commitizen-tools#1411 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
There was a problem hiding this comment.
Pull request overview
This PR restores pre-v4.6.3 behavior for cz changelog --file-name by making CLI-provided paths resolve relative to the current working directory, while preserving config-provided changelog_file resolution relative to the config file directory. It also introduces a new cz dump-config command with docs/tests.
Changes:
- Fix changelog output path resolution by distinguishing CLI
--file-namevs configchangelog_filesources. - Adjust
cz bump --changelogto only forwardfile_nametoChangelogwhen explicitly provided via CLI. - Add a new
cz dump-configcommand, along with documentation and tests.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
commitizen/commands/changelog.py |
Implements source-aware path resolution for changelog output filename. |
commitizen/commands/bump.py |
Ensures Bump only forwards CLI --file-name, letting Changelog resolve config-relative paths. |
tests/test_changelog.py |
Updates/extends tests to validate the new filename resolution behavior. |
docs/commands/changelog.md |
Documents the CLI-vs-config relative path behavior. |
commitizen/commands/dump_config.py |
Adds the DumpConfig command implementation. |
commitizen/commands/__init__.py |
Exposes DumpConfig in the commands package. |
commitizen/cli.py |
Registers the new dump-config subcommand and help text. |
tests/commands/test_dump_config_command.py |
Adds tests for dump-config output formats. |
tests/commands/test_common_command.py |
Includes dump-config in the common --help regression tests. |
tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_10_dump_config_.txt |
Adds help output snapshot for dump-config (Py 3.10). |
tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_11_dump_config_.txt |
Adds help output snapshot for dump-config (Py 3.11). |
tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_12_dump_config_.txt |
Adds help output snapshot for dump-config (Py 3.12). |
tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_13_dump_config_.txt |
Adds help output snapshot for dump-config (Py 3.13). |
tests/commands/test_common_command/test_command_shows_description_when_use_help_option_py_3_14_dump_config_.txt |
Adds help output snapshot for dump-config (Py 3.14). |
docs/commands/dump_config.md |
Adds documentation for the new dump-config command. |
mkdocs.yml |
Adds the new dump-config docs page to navigation and plugin config. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| {"tool": {"commitizen": settings}}, | ||
| default_flow_style=False, | ||
| allow_unicode=True, | ||
| ) | ||
| ) | ||
| else: | ||
| out.write( | ||
| json.dumps({"tool": {"commitizen": settings}}, indent=2, default=str) |
| { | ||
| "name": "dump-config", | ||
| "description": "Output the current commitizen configuration", | ||
| "help": "Output the current commitizen configuration.", | ||
| "func": commands.DumpConfig, | ||
| "arguments": [ | ||
| { | ||
| "name": ["--format", "-f"], | ||
| "choices": ["toml", "yaml", "json"], | ||
| "default": "toml", | ||
| "help": "Output format (default: toml).", | ||
| } | ||
| ], | ||
| }, |
| assert "tool" in data | ||
| assert "commitizen" in data["tool"] | ||
| assert data["tool"]["commitizen"]["name"] == "cz_conventional_commits" | ||
|
|
||
|
|
||
| def test_dump_config_json(config, capsys): | ||
| DumpConfig(config, {"format": "json"})() | ||
| out, _ = capsys.readouterr() | ||
| data = json.loads(out) | ||
| assert "tool" in data | ||
| assert "commitizen" in data["tool"] | ||
| assert data["tool"]["commitizen"]["name"] == "cz_conventional_commits" |
| # dump-config | ||
|
|
||
| Output the current commitizen configuration (defaults merged with any project overrides) so you can paste it directly into a configuration file as a starting point. | ||
|
|
||
| ## Usage | ||
|
|
||
| ``` | ||
| cz dump-config [--format {toml,yaml,json}] | ||
| ``` | ||
|
|
||
| ## Options | ||
|
|
||
| | Option | Description | | ||
| |---|---| | ||
| | `--format`, `-f` | Output format: `toml` (default), `yaml`, or `json` | | ||
|
|
Manny7717
left a comment
There was a problem hiding this comment.
Verified: the changelog fix is correct; one scope concern before merge
The fix (a7da2ca) — verified locally, regression-proven:
Changelog.__init__now distinguishes CLI-provided--file-name(kept as-is, cwd-relative) from config-providedchangelog_file(anchored to the config file's directory);Bumpforwardsfile_nameto Changelog only when it came from the CLI. Matches the pre-4.6.3 contract from #1411 and keeps the #1391 config-dir fix for the config path.- New parametrized
test_changelog_file_name_resolution: 4/4 pass on head; 2/4 FAIL on master (the two CLI-arg cases get config-dir-anchored, e.g.changelog/CHANGES.rst→/my/project/changelog/CHANGES.rst) — clean regression proof. - End-to-end repro of the #1411 scenario: from a
docs/subdir,cz changelog --file-name=changelog/new.mdwrites to<cwd>/changelog/new.md; config-onlychangelog_filewrites to<root>/CHANGELOG.md. Both correct. tests/test_changelog.py+tests/commands/test_bump_command.py: 199 passed. ruff check/format clean, mypy clean on the 3 touched source files.
Scope concern (3f891ed): the PR bundles an unrelated feature — feat: cz dump-config (12 files, +219: new command + docs + 5 tests, all verified passing 5/5) — into a bugfix PR whose title and body only describe the changelog fix. Per the repo's contribution rules the commit type controls release impact: squash-merging this as fix(changelog): would ship a new user-facing feature in a patch release, and splitting it would let the fix land fast while the feature gets its own review/release cycle. Recommend splitting the dump-config commit into its own PR and keeping a7da2ca alone here.
Fix itself: no blockers. Happy to re-approve once scoped.
Description
Why
Fixes #1411 — relative paths work differently with newer versions.
The user reported that after upgrading to v4.6.3, the following command stopped working as expected:
# run from a Sphinx docs subdirectory cz -n cz_customize changelog --file-name=changelog/fileBefore v4.6.3: the file was created at
<cwd>/changelog/file(relative to the directory whereczwas invoked — standard CLI behaviour).After v4.6.3: the file is created at
<project_root>/changelog/file(relative to the config file's directory), ignoringcwd.What changed
--file-nameCLI argchangelog_fileconfig settingPR #1391 (v4.6.3) fixed a real bug —
cz bump --changelogfrom a subdirectory was writing the changelog relative to the subdirectory instead of the project root. However, it applied the config-dir anchor to both sources: the config-filechangelog_filesetting AND the CLI--file-nameargument. This over-correction broke existing setups that relied on--file-namebeing cwd-relative.How it works
commitizen/commands/changelog.py: The__init__method now distinguishes between the two sources:arguments.get("file_name")(CLI arg) → used as-is, relative to cwdconfig.settings.get("changelog_file")(config setting) → resolved relative toconfig.path.parentcommitizen/commands/bump.py: WhenBumpinvokesChangelog, it previously always forwardedfile_name(even when the value came from config). It now only forwardsfile_nameif it was explicitly provided via CLI (--file-name). Without a forwardedfile_name,Changelogfalls through to its own config lookup and applies the correct config-dir anchor.Backward compatibility
--file-namebeing resolved relative to cwd: behaviour restored to pre-v4.6.3.changelog_filein config being resolved relative to the config dir: no change, the PR fix: cz bump subdir #1391 behaviour is preserved.cz bump --changelogwithout--file-namefrom a subdirectory: no change, the changelog still goes to the project root (the PR fix: cz bump subdir #1391 fix is preserved).Checklist
Was generative AI tooling used to co-author this PR?
Code Changes
uv run poe alllocally to ensure this change passes linter check and testsExpected Behavior
cz changelog --file-name=changelog/CHANGES.mdrun from any working directory should write the changelog to<cwd>/changelog/CHANGES.md.changelog_file = "CHANGELOG.md"inpyproject.tomlshould write the changelog to<project_root>/CHANGELOG.md, regardless of the working directory.Steps to Test This Pull Request
Additional Context