Repository: MarketSquare/robotframework-robocop (python)

Issue:
[Bug] --ext-rules path in argument file should be relative to the argument file location
### What happened?

Running robocop with `--ext-rules` parameter takes a path relative to the location where the robocop is launched. When using the argument file with `-A` parameter then `--ext-rules` should be relative to the argument file. 

### What command/code did you try to run?

robocop tests/ -A tests/roboargs.txt

Argument file has following line:
--ext-rules libraries/robocop/

External rules are located to /tests/libraries/robocop


### What is the full error message?

Error: Fatal error: Failed to load external rules from file "/libraries/robocop/". Verify if the file exists

### What did you expect to happen instead?

Get ext-rules from the relative location to the argument file location.

For example in argument file:
`--ext-rules libraries/robocop/` could point relative to the argument file and `--ext-rules ./libraries/robocop/` could point relative to location robocop is launched.

### Operating System

Linux

### Robocop version

5.0.4

Pull request:
Fix relative paths not resolved in argument files
Fixes #1071

Patch:
diff --git a/docs/releasenotes/fixes.5.rst b/docs/releasenotes/fixes.5.rst
new file mode 100644
index 0000000..2eddede
--- /dev/null
+++ b/docs/releasenotes/fixes.5.rst
@@ -0,0 +1,13 @@
+Relative paths in argument files (#1071)
+-----------------------------------------
+
+Relative paths in argument files will be now resolved correctly. For example, using following command::
+
+    robocop -A tests/args.txt
+
+And tests/args.txt file::
+
+    --ext-rules rules/robocop_rules.py
+
+``--ext-rules rules/robocop_rules.py`` will be resolved to ``--ext-rules tests/rules/robocop_rules.py``. This
+behaviour already worked for toml configuration files and wasn't working correctly for argument files.
diff --git a/robocop/config.py b/robocop/config.py
index 2ea420e..8d2f92a 100644
--- a/robocop/config.py
+++ b/robocop/config.py
@@ -125,7 +125,7 @@ class ArgumentFileParser:
             else:
                 loaded_config_dir = None
             file_args = self.load_argument_file(argfile, config_dir)
-            file_args = self.resolve_arguments_paths(file_args, config_dir)
+            file_args = self.resolve_arguments_paths(file_args, loaded_config_dir)
             parsed_args += self.expand_argument_files(file_args, loaded_config_dir)
         return parsed_args
 


Test patch:
diff --git a/tests/test_data/relative_path_in_argfile/tests/args.txt b/tests/test_data/relative_path_in_argfile/tests/args.txt
new file mode 100644
index 0000000..aa9c310
--- /dev/null
+++ b/tests/test_data/relative_path_in_argfile/tests/args.txt
@@ -0,0 +1,1 @@
+--ext-rules libraries/test.py
\ No newline at end of file
diff --git a/tests/test_data/relative_path_in_argfile/tests/libraries/test.py b/tests/test_data/relative_path_in_argfile/tests/libraries/test.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/utest/test_configuration_file.py b/tests/utest/test_configuration_file.py
index d71776b..954b66c 100644
--- a/tests/utest/test_configuration_file.py
+++ b/tests/utest/test_configuration_file.py
@@ -229,7 +229,7 @@ class TestConfigurationFile:
     def test_load_config_with_relative_paths_pyproject(self, path_to_test_data, config_source):
         """
         pyproject.toml resolves relative path to config directory.
-        For example if root/pyproject.toml contains test.py, it will become root/test.py
+        For example if root/pyproject.toml contains test.py, it will become root/test.py .
         """
         src = path_to_test_data / "relative_path_in_config_pyproject"
         work_dir = src / "nested"
@@ -256,6 +256,17 @@ class TestConfigurationFile:
             ext_rule_path = config.ext_rules.pop()
             assert Path(ext_rule_path).absolute() == work_dir / "test.py"
 
+    def test_load_config_with_relative_paths_argfile(self, path_to_test_data):
+        """
+        Argument files resolves relative paths to config directory.
+        """
+        src = path_to_test_data / "relative_path_in_argfile"
+        work_dir = src
+        with working_directory(work_dir), patch.object(sys, "argv", ["robocop", "-A", "tests/args.txt"]):
+            config = Config(from_cli=True)
+            ext_rule_path = config.ext_rules.pop()
+            assert Path(ext_rule_path).absolute() == work_dir / "tests/libraries/test.py"
+
     def test_override_default_config(self, path_to_test_data):
         """
         Default config should not be loaded if "--argumentfile" option is used.