Repository: readthedocs/readthedocs.org

Issue:
Flyout links incorrect on Mkdocs projects
GitHub links in the sidebar footer contain an extraneous path. For example, on http://tracks.readthedocs.org/en/latest/upgrading/ the "View" link points to https://github.com/TracksApp/tracks/blob/master/home/docs/checkouts/readthedocs.org/user_builds/tracks/checkouts/latest/doc/Upgrading.md.



Discussion:
#1480 was closed even though the root cause seems to be the same. Yet, the flyout links are still incorrect.

I was trying to fix this and believe I found the cause, I was able to get a working version for a MkDocs based project.

More info here: https://github.com/rtfd/readthedocs.org/commit/98b5d34316917e0bdb26404c6f3816108ce7f42e

@agjohnson any thoughts on my comment in https://github.com/rtfd/readthedocs.org/commit/98b5d34316917e0bdb26404c6f3816108ce7f42e

I have tested the change locally using the directions here: https://docs.readthedocs.org/en/latest/install.html#installing-read-the-docs and it works for a MkDocs and Sphinx based projects.

@ericholscher do you have any thoughts on this? If I have a fix that is tested, so I can create the PR. Want to get this fixed as we just went live with our ReadTheDocs integration.

@sahilTakiar Definitely send the PR over! 

The same here.

Steps to reproduce:
1. Go to [this page](http://docs.drupalvm.com/en/latest/).
2. Click _Read the Docs_ in the bottom, then View or Edit.

The links are like:

```
https://github.com/geerlingguy/drupal-vm/blob/master/home/docs/checkouts/readthedocs.org/user_builds/drupal-vm/checkouts/latest/docs/index.md
https://github.com/geerlingguy/drupal-vm/edit/master/home/docs/checkouts/readthedocs.org/user_builds/drupal-vm/checkouts/latest/docs/index.md
```

which aren't valid.

Same here: http://mathquill.readthedocs.io/en/latest/

#2066 was merged hours ago and thus this should now be closed I guess.
Thanks @marcelstoer. I just rebuilt and the issue persists so I don't believe this should be closed until the change is deployed.
Just deployed this, so it should hopefully be fixed. 
It appears to be. Thanks @sahilTakiar and @ericholscher!
It works for the OP's project at http://tracks.readthedocs.org but it doesn't work for any other project listed here in the comments: http://docs.drupalvm.com,  http://mathquill.readthedocs.io and http://nodemcu.readthedocs.io (mine) still have this odd ".../docs/checkouts/readthedocs.org/..." URLs. What's the difference between them? I just built mine again to be sure.
same for me. The view and edit URLs have the same '.../docs/checkouts/readthedocs.org/...' in them

I thought maybe it was related to having ```edit_uri``` set in ```mckdocs.yml```, but removing it and rebuilding seems to make no difference. @marcelstoer doesn't even have the set in their mkdcos.yml.

- https://psraw.readthedocs.io/en/latest/
- https://psmsgraph.readthedocs.io/en/latest/
- https://autodocumentsexample.readthedocs.io/en/latest/
`edit_uri` is a feature that [appeared in mkdocs 0.16](http://www.mkdocs.org/about/release-notes/#version-016-2016-11-04) and that would help in solving that I think. But it seems RTFD is still using mkdocs 0.15 according to my [build logs](https://readthedocs.org/projects/phpspreadsheet/builds/5380219/).

Is an upgrade to mkdocs 0.16 planed ? Or is it something that could be contributed by someone relatively easily ?

Resolving patch:
diff --git a/readthedocs/doc_builder/backends/mkdocs.py b/readthedocs/doc_builder/backends/mkdocs.py
--- a/readthedocs/doc_builder/backends/mkdocs.py
+++ b/readthedocs/doc_builder/backends/mkdocs.py
@@ -111,11 +111,16 @@ def append_conf(self, **__):
             '%scss/readthedocs-doc-embed.css' % static_url,
         ])
 
-        docs_path = os.path.join(self.root_path, docs_dir)
+        # The docs path is relative to the location
+        # of the mkdocs configuration file.
+        docs_path = os.path.join(
+            os.path.dirname(self.yaml_file),
+            docs_dir
+        )
 
         # RTD javascript writing
         rtd_data = self.generate_rtd_data(
-            docs_dir=docs_dir,
+            docs_dir=os.path.relpath(docs_path, self.root_path),
             mkdocs_config=user_config
         )
         with open(os.path.join(docs_path, 'readthedocs-data.js'), 'w') as f:


Test patch:
diff --git a/readthedocs/rtd_tests/tests/test_doc_builder.py b/readthedocs/rtd_tests/tests/test_doc_builder.py
--- a/readthedocs/rtd_tests/tests/test_doc_builder.py
+++ b/readthedocs/rtd_tests/tests/test_doc_builder.py
@@ -335,3 +335,36 @@ def test_dont_override_theme(self, checkout_path, run):
             config['theme_dir'],
             'not-readthedocs'
         )
+
+    @patch('readthedocs.doc_builder.backends.mkdocs.BaseMkdocs.generate_rtd_data')
+    @patch('readthedocs.doc_builder.base.BaseBuilder.run')
+    @patch('readthedocs.projects.models.Project.checkout_path')
+    def test_write_js_data_docs_dir(self, checkout_path, run, generate_rtd_data):
+        tmpdir = tempfile.mkdtemp()
+        os.mkdir(os.path.join(tmpdir, 'docs'))
+        yaml_file = os.path.join(tmpdir, 'mkdocs.yml')
+        yaml.safe_dump(
+            {
+                'site_name': 'mkdocs',
+                'docs_dir': 'docs',
+            },
+            open(yaml_file, 'w')
+        )
+        checkout_path.return_value = tmpdir
+        generate_rtd_data.return_value = ''
+
+        python_env = Virtualenv(
+            version=self.version,
+            build_env=self.build_env,
+            config=None,
+        )
+        self.searchbuilder = MkdocsHTML(
+            build_env=self.build_env,
+            python_env=python_env,
+        )
+        self.searchbuilder.append_conf()
+
+        generate_rtd_data.assert_called_with(
+            docs_dir='docs',
+            mkdocs_config=mock.ANY
+        )