Repository: jazzband/pip-tools

Issue:
duplicate --find-links
I've managed to get 2 `--find-links` in my requirements.txt when I altered the single value in requirements.in . The first is the one before the change and the second is after.

#### Environment Versions

1. OS Type: MacOS 10.13
1. Python version: 3.7.5
1. pip version:  19.3.1
1. pip-tools version: 4.3.0

#### Steps to replicate

1. add `--find-links file:./local_src` in requirements.in
1. `pip-compile requirements.in`
1. change entry in 1 to `--find-links ./local_src`
1. `pip-compile requirements.in`

#### Expected result
(I would actually expect the result to remain a relative path, but that's #204)
```
--find-links /Users/me/myrepo/./local_src
```


#### Actual result

```
--find-links file:./local_src
--find-links /Users/me/myrepo/./local_src
```



Discussion:
@tisdall thanks for the issue!
FTR, should be fixed by passing deep copied finder here: https://github.com/jazzband/pip-tools/blob/67092445a1c47dc453b8cc9753bb9fbf125beaac/piptools/scripts/compile.py#L290



Resolving patch:
diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py
--- a/piptools/scripts/compile.py
+++ b/piptools/scripts/compile.py
@@ -287,11 +287,16 @@ def cli(
     # Proxy with a LocalRequirementsRepository if --upgrade is not specified
     # (= default invocation)
     if not upgrade and os.path.exists(output_file.name):
+        # Use a temporary repository to ensure outdated(removed) options from
+        # existing requirements.txt wouldn't get into the current repository.
+        tmp_repository = PyPIRepository(
+            pip_args, build_isolation=build_isolation, cache_dir=cache_dir
+        )
         ireqs = parse_requirements(
             output_file.name,
-            finder=repository.finder,
-            session=repository.session,
-            options=repository.options,
+            finder=tmp_repository.finder,
+            session=tmp_repository.session,
+            options=tmp_repository.options,
         )
 
         # Exclude packages from --upgrade-package/-P from the existing


Test patch:
diff --git a/tests/test_cli_compile.py b/tests/test_cli_compile.py
--- a/tests/test_cli_compile.py
+++ b/tests/test_cli_compile.py
@@ -960,3 +960,59 @@ def test_upgrade_packages_option_subdependency(
     assert (
         upgraded_package in stderr_lines
     ), "{} must be upgraded/downgraded to {}".format(current_package, upgraded_package)
+
+
+@pytest.mark.parametrize(
+    "input_opts, output_opts",
+    (
+        # Test that input options overwrite output options
+        pytest.param(
+            "--index-url https://index-url",
+            "--index-url https://another-index-url",
+            id="index url",
+        ),
+        pytest.param(
+            "--extra-index-url https://extra-index-url",
+            "--extra-index-url https://another-extra-index-url",
+            id="extra index url",
+        ),
+        pytest.param("--find-links dir", "--find-links another-dir", id="find links"),
+        pytest.param(
+            "--trusted-host hostname",
+            "--trusted-host another-hostname",
+            id="trusted host",
+        ),
+        pytest.param(
+            "--no-binary :package:", "--no-binary :another-package:", id="no binary"
+        ),
+        pytest.param(
+            "--only-binary :package:",
+            "--only-binary :another-package:",
+            id="only binary",
+        ),
+        # Test misc corner cases
+        pytest.param("", "--index-url https://index-url", id="empty input options"),
+        pytest.param(
+            "--index-url https://index-url",
+            (
+                "--index-url https://index-url\n"
+                "--extra-index-url https://another-extra-index-url"
+            ),
+            id="partially matched options",
+        ),
+    ),
+)
+def test_remove_outdated_options(runner, input_opts, output_opts):
+    """
+    Test that the options from the current requirements.txt wouldn't stay
+    after compile if they were removed from requirements.in file.
+    """
+    with open("requirements.in", "w") as req_in:
+        req_in.write(input_opts)
+    with open("requirements.txt", "w") as req_txt:
+        req_txt.write(output_opts)
+
+    out = runner.invoke(cli, ["--no-header"])
+
+    assert out.exit_code == 0, out
+    assert out.stderr.strip() == input_opts