Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite Documentation/Source links on hover to use local hoogle/hackage if available on nix and otherwise #3475

Open
ParetoOptimalDev opened this issue Feb 2, 2023 · 4 comments
Labels
os: nixos type: enhancement New feature or request

Comments

@ParetoOptimalDev
Copy link

ParetoOptimalDev commented Feb 2, 2023

Is your enhancement request related to a problem? Please describe.

This request is similar to #208, but slightly different? At least the fact that this issue is focused on nix support makes it different, but the implementation of this could satisfy both issues I believe.

Even when I have a local instance of hoogle running at localhost:8888 the links I click for source and documentation go to http://haskell.org.

NOTE: I use nix's withHaddock feature for this

For instance I'm linked to:

https://hackage.haskell.org/package/apecs-0.9.4/docs/Apecs-Util.html#v:newEntity_

When I'd prefer to be linked to:

http://localhost:8888/file/nix/store/pmyb1ayz7vxlmim1bd12sjpss3s7pddy-apecs-0.9.4-doc/share/doc/apecs-0.9.4/html/Apecs.html#v:newEntity_

Describe the solution you'd like

For HLS to check to see if a hoogle instance is running and if it's fingerprint is a nix server, rewrite urls like:

- https://hackage.haskell.org/package/apecs-0.9.4/docs/Apecs-Util.html#v:newEntity_
+ http://localhost:8888/file/nix/store/pmyb1ayz7vxlmim1bd12sjpss3s7pddy-apecs-0.9.4-doc/share/doc/apecs-0.9.4/html/Apecs.html#v:newEntity_

And for source:

- https://hackage.haskell.org/package/apecs-0.9.4/docs/src/Apecs.Util.html#newEntity_
+ http://localhost:8888/file/nix/store/pmyb1ayz7vxlmim1bd12sjpss3s7pddy-apecs-0.9.4-doc/share/doc/apecs-0.9.4/html/src/Apecs.Util.html#newEntity_

Looking at the above, the common replacement within the urls is:

- /package/apecs-0.9.4/docs
+/file/nix/store/pmyb1ayz7vxlmim1bd12sjpss3s7pddy-apecs-0.9.4-doc/share/doc/apecs-0.9.4/html

You can get this variable in recent versions of Nix's haskellPackages from the environmental variable $NIX_GHC_DOCDIR.

NOTE: I believe the code to change is in Development.IDE.Spans.Documentation.getDocumentationsTryGhc

Describe alternatives you've considered

A workaround described here:

A possible workaround is to active documentation generation for all dependant packages adding in the cabal.project:

package * 
  documentation: true

To be frank though, I'd just never do that and probably forget it was ever an option.

@ParetoOptimalDev ParetoOptimalDev changed the title Rewrite hackage Documentation/Source links on hover to use local hoogle/hackage if available on nix and otherwise Rewrite Documentation/Source links on hover to use local hoogle/hackage if available on nix and otherwise Feb 2, 2023
@alexfmpe
Copy link

Would also like this due to having some dependencies that aren't present in hackage at all.
What if instead of HLS rewriting links it accepted a "haddocks root" flag, defaulting to hackage but allowing nix setups to override it with the same root used in the links rendered by nix-based local hoogle?
That way HLS doesn't need to be nix aware, it would just stop forcing hackage as a decision.

@fendor
Copy link
Collaborator

fendor commented May 25, 2024

I was thinking a little bit about this, and what would the haddock root look like? Given the example above, providing a root location is easy, but nix stores the documentation in /nix/store/<hash>-<pkgname>/share/doc. There is no common root that we could use, afaict.

@alexfmpe
Copy link

Hmm true, we'd need to be able to override on a per-package basis. Which makes some sense I guess, you could have most packages coming from hackage but a few hosted on an org-specific repository?

So the flag would need to be a filepath to a file with contents similar to

apecs-0.9.4 -> /file/nix/store/pmyb1ayz7vxlmim1bd12sjpss3s7pddy-apecs-0.9.4-doc/share/doc/apecs-0.9.4/html
example-0.1.1 -> /file/nix/store/<hash>-example-0.1.1-doc/share/doc/example-0.1.1/html

@alexfmpe
Copy link

alexfmpe commented May 27, 2024

After some more thought, I realised HLS probably should be getting the urls from haddock-interfaces/ haddock-html whenever possible, and give that priority over hackage.

$ ghc-pkg describe text

haddock-interfaces:
    /nix/store/phnrbf9zvf8nl6wrhbh3qwgn9d2q7rhl-ghc-9.4.8-doc/share/doc/ghc/html/libraries/text-2.0.2/text.haddock

haddock-html:
    /nix/store/phnrbf9zvf8nl6wrhbh3qwgn9d2q7rhl-ghc-9.4.8-doc/share/doc/ghc/html/libraries/text-2.0.2

For the specific case of nix-built dependencies, this seems to solve the entire problem as every dep ends up in the package database.

Following the above source directions by @ParetoOptimalDev, I ended up very surprised seeing HLS is already doing this:

lookupHtmls :: HscEnv -> Unit -> Maybe [FilePath]
lookupHtmls df ui =
-- use haddockInterfaces instead of haddockHTMLs: GHC treats haddockHTMLs as URL not path
-- and therefore doesn't expand $topdir on Windows
map takeDirectory . unitHaddockInterfaces <$> lookupUnit df ui

However, vscode-haskell hardcodes to hackage. On the other hand, haskell-mode seems to produce the right links - not sure whether it's getting them from HLS or something else. I only use emacs/vscode so don't know the status on extensions for other editors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
os: nixos type: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants