MarketSquare / MarketSquare/robotframework-robocop
ReplaceWithVAR silently corrupts inline comments on Create Dictionary / Create List
- Dominant language
- Python
- Stars
- 269
- Forks
- 58
- Avg merge
- 4h 55m
- Merged PRs (30d)
- 45
Description
## Description
The `ReplaceWithVAR` transformer silently corrupts Robot Framework code when converting `Create Dictionary` or `Create List` calls that contain inline comments (`#`). The comment marker gets orphaned, and the tokens after `#` become standalone lines that Robot Framework interprets as keyword calls.
This causes **silent runtime failures** — the transformed code is syntactically valid but semantically broken.
## Versions
- **robocop**: 8.2.4 (also confirmed on 8.2.3)
- **Robot Framework**: 7.2
- **Python**: 3.13.7
- **OS**: Windows 11
## Reproduction
### Case 1: Create Dictionary with inline comment
**Input (`repro_dict.robot`):**
```robotframework
*** Test Cases ***
Example Dict
&{task_dict} Create Dictionary # Task/Subtask State=Task/Subtask Name Subtask Status=Subtask Name
Log &{task_dict}
```
**Command:**
```
python -m robocop format --select ReplaceWithVAR repro_dict.robot
```
**Actual output:**
```robotframework
*** Test Cases ***
Example Dict
#
Task/Subtask State=Task/Subtask Name
Subtask Status=Subtask Name
VAR &{task_dict} &{EMPTY}
Log &{task_dict}
```
**Runtime error (`robot --dryrun repro_dict.robot`):**
```
==============================================================================
Example Dict | FAIL |
Several failures occurred:
1) No keyword with name 'Task/Subtask State=Task/Subtask Name' found.
2) No keyword with name 'Subtask Status=Subtask Name' found.
==============================================================================
```
**Expected:** The `#` and everything after it should remain as a single comment line (or be dropped entirely), and `VAR &{task_dict} &{EMPTY}` should be the only executable line. The key=value pairs after `#` should never become standalone executable lines.
---
### Case 2: Create List with inline comment
**Input (`repro_list.robot`):**
```robotframework
*** Test Cases ***
Example List
@{items} Create List # Asset_REST (AssetRESTAPIData) Project_Site_Unit
Log @{items}
```
**Command:**
```
python -m robocop format --select ReplaceWithVAR repro_list.robot
```
**Actual output:**
```robotframework
*** Test Cases ***
Example List
#
Asset_REST (AssetRESTAPIData)
Project_Site_Unit
VAR @{items} @{EMPTY}
Log @{items}
```
**Runtime error (`robot --dryrun repro_list.robot`):**
```
==============================================================================
Example List | FAIL |
Several failures occurred:
1) No keyword with name 'Asset_REST (AssetRESTAPIData)' found.
2) No keyword with name 'Project_Site_Unit' found.
==============================================================================
```
## Impact
This is a **silent code corruption** bug. The transformer produces syntactically valid `.robot` files that fail at runtime with misleading keyword-not-found errors. In our case it broke 12 test files across a large test suite after running `robocop format`, and the root cause was not obvious from the error messages alone.
## Workaround
We manually re-merged the orphaned comment content back onto the `#` line after formatting. No robocop configuration option prevents this.
Contributor guide
Research direction
Start with the ReplaceWithVAR transformer and reproduce the issue using `python -m robocop format --select ReplaceWithVAR` on the provided Create Dictionary and Create List examples. Trace how inline comments are handled and add regression coverage for both cases; done means text after `#` stays comment content or is dropped, without becoming executable lines.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100