haskell / haskell-language-server

Official haskell ide support via language server (LSP). Successor of ghcide & haskell-ide-engine.
Apache License 2.0
2.61k stars 351 forks source link

HLS loops forever on bogus cyclic import introduced by the duplicate module names #4265

Open konn opened 1 month ago

konn commented 1 month ago

Your environment

Which OS do you use? macOS Sonoma 14.5 (23F79)

Which version of GHC do you use and how did you install it? GHC 9.4.8 and 9.8.2, via GHCup ()

How is your project built (alternative: link to the project)? Cabal. See the repro repo: https://github.com/konn/hls-multihome-modname (NOTE: despite the repo name, Multi Home seems irrelevant here - sorry for the confusion)

Which LSP client (editor/plugin) do you use? VSCode + vscode-haskell

Which version of HLS do you use and how did you install it? 2.8.0.0 via GHCup

Have you configured HLS in any way (especially: a hie.yaml file)? No hie.yaml (see the repro above).

Steps to reproduce

  1. Create a package with two modules: src/Lib.hs and src/Lib/A.hs
  2. Give src/Lib/A.hs the same name as Lib: put module Lib where on top of src/Lib/A.hs.
  3. import Lib.A from Lib

Expected behaviour

HLS must suggest to rename the module name of Lib/A.hs to Lib.A.

Actual behaviour

HLS loops indefinitely.

Although the situation is ill-conditioned on the user side, but it can occur relatively easily when one wants to cut out some portion of the existing module to a separate (sub) module. So, it might be good for HLS to be able to handle this case.

Debug information

4-Haskell (hls-multihome-modname).log

soulomoon commented 1 month ago

we should create a test case for this. We do have code detecting cycles: https://github.com/soulomoon/haskell-language-server/blob/60839b023b7eea705a8827c0887c87114b12b940/ghcide/src/Development/IDE/Import/DependencyInformation.hs#L273

soulomoon commented 1 month ago

I've created a branch and push an attempt to add the test. https://github.com/haskell/haskell-language-server/tree/4265-hls-loops-forever-on-bogus-cyclic-import-introduced-by-the-duplicate-module-names. Then I test it with TASTY_PATTERN="ImportCycleTests" cabal test ghcide-tests But I am not sure if I set it up correctly.

============================ Sometime we can get the following reproduction. We do figure out the correct module name for them and import name, so we do not see cycles in ReportImportCycles. But then we have the wrong ModLocation for Lib/A.hs(Since we have ill defined module name in Lib/A.hs). Then we stuck in tcRnModule->hscTypecheckRename but I am not sure what went wrong after in ghc that caused the hang.

ThreadId 95 | 2024-05-29T08:06:02.999757Z | Debug | Known files updated:
  fromList [
(TargetModule (ModuleName "Lib.A"),fromList ["/private/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/extra-dir-10842865193/src/Lib/A.hs"]),
(TargetModule (ModuleName "Lib"),fromList ["/private/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/extra-dir-10842865193/src/Lib.hs"]),
(TargetFile NormalizedFilePath "/private/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/extra-dir-10842865193/src/Lib/A.hs",
fromList ["/private/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/extra-dir-10842865193/src/Lib/A.hs"])]
And the `rawImports`:
0 -> [L (RealSrcSpan SrcSpanOneLine "/private/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/extra-dir-10842865193/src/Lib.hs" 3 8 13 (Just (BufSpan {bufSpanStart = BufPos {bufPos = 28}, bufSpanEnd = BufPos {bufPos = 33}}))) (ModuleName "Lib.A")]
1 -> []

tcRnModule: ModLocation {ml_hs_file = Just "/private/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/extra-dir-13294337334/src/Lib/A.hs", ml_hi_file = "/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/.cache/ghcide/importCycle-0.1.0.0-inplace-2836ba19a3aec7ab18476d0269fb9c130232ebad/Lib.hi", ml_dyn_hi_file = "/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/.cache/ghcide/importCycle-0.1.0.0-inplace-2836ba19a3aec7ab18476d0269fb9c130232ebad/Lib.dyn_hi", ml_obj_file = "/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/.cache/ghcide/importCycle-0.1.0.0-inplace-2836ba19a3aec7ab18476d0269fb9c130232ebad/Lib.o", ml_dyn_obj_file = "/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/.cache/ghcide/importCycle-0.1.0.0-inplace-2836ba19a3aec7ab18476d0269fb9c130232ebad/Lib.dyn_o", ml_hie_file = "/var/folders/36/_743psv11gv2wrj9dclrpd500000gn/T/hls-test-root/.cache/ghcide/importCycle-0.1.0.0-inplace-2836ba19a3aec7ab18476d0269fb9c130232ebad/Lib.hie"}
fendor commented 2 days ago

@wz1000 Will take a look!