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

Use stan config files for stan plugin (#3904) #3914

Merged
merged 6 commits into from
Jan 11, 2024
Merged

Conversation

0rphee
Copy link
Collaborator

@0rphee 0rphee commented Dec 18, 2023

Solves #3904

I didn't have a clear idea of how it would be best to get the .cabal file for the current file, but the way I did it seems to work fine, it is not critical for stan anyways. Please tell me if there's a better way, and whether the new export of the function from ghcide/session-loader/Development/IDE/Session/Implicit.hs is ok.

I also added a test case, in my machine it takes around 5-6s.

I'm not aware if it is possible to load the config just one time for each file, but it should be more appropriate than loading it with every change to the file.

Copy link
Collaborator

@michaelpj michaelpj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is probably fine, but I do think we should try and get upstream to provide us with a more packaged solution for doing this. Compare with e.g. fourmolu where we just have loadConfig :: FilePath -> Config that gives you the appropriate config given the working directory.

Could you make an issue upstream? or maybe even consider contributing the function?

let analysis = runAnalysis Map.empty enabledInspections [] [hie]
let currentHSfromHIEAbs = hie_hs_file hie
currentHSfromHIERel <- liftIO $ makeRelativeToCurrentDirectory currentHSfromHIEAbs
-- This codes follows what 'runStan' does, from the module 'Stan'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you ask upstream to expose this logic from the library so you can reuse it?

let enabledInspections = HM.fromList [(LSP.fromNormalizedFilePath file, inspectionsIds)]
-- This should use Cabal config for extensions and Stan config for inspection preferences is the future
let analysis = runAnalysis Map.empty enabledInspections [] [hie]
let currentHSfromHIEAbs = hie_hs_file hie
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you already have the file from the rule argument, I think?

HM.fromList [(LSP.fromNormalizedFilePath file, inspectionsIds)],
[])
ResultL warnings stanConfig -> do
-- I'm not sure this is the best way to obtain the .cabal
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, ideally stan would expose a function for working out everything it needs given a current directory. We've increasingly found that it's risky to deviate from what a tool does when run at the CLI. For example, by doing this maybe we do a better job of finding the cabal file than the stan code would, so the user will see different results in HLS and the stan CLI. Better to get stan to give us a function that just sets everything up given a working directory, and then we can just call that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could define the function here that we'd like stan to provide, that gives them something to look at!

-- This codes follows what 'runStan' does, from the module 'Stan'

-- There aren't any cli args. isLoud=False=Silent output
let isLoud = False -- Should this be enabled when debugging? Enables default stan cli output
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if it's enabled? Does it write to stderr or what? we don't want that!

-- I'm not sure this is the best way to obtain the .cabal
-- for this file but it'll have to do. Anyways, if it is not
-- found it's not a big issue. That was the default previously.
maybeCabalFileDir <- let maybeCabalFileDir = findFileUpwardsF
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I don't know if you looked around but I bet we do this somewhere else in HLS also...

Copy link
Collaborator

@fendor fendor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpicks, looks otherwise reasonable, but I agree with michael that we should upstream the config loading.

plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs Outdated Show resolved Hide resolved
plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs Outdated Show resolved Hide resolved
plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs Outdated Show resolved Hide resolved
@0rphee
Copy link
Collaborator Author

0rphee commented Dec 21, 2023

Thanks for the comments. I'll work on it and see what changes can be made in stan to make this work better.

@0rphee
Copy link
Collaborator Author

0rphee commented Dec 22, 2023

I've made a PR to stan exposing a bit of the config code, so there is more shared code. However, the analysis section needs to be kept different without some bigger changes to how stan applies checks (I tried to unify them without success). The difference is not that big anyway...

I've updated the logging constructors. As for the legibility of their results, it's not the best, but at least for me, they have been useful while debugging this, this is how they look:

2023-12-22T00:13:46.798 helix_lsp::transport [ERROR] hacked-hls err <- "2023-12-22T06:13:46.798310Z | Debug | stan: Config result: Result (fromList [\"\",\"configChecks is set through the source: TOML\",\"No TOML value is specified for key: remove\",\"\",\"configRemoved is set through the source: TOML\",\"No TOML value is specified for key: ignore\",\"\",\"configIgnored is set through the source: TOML\"]) (ConfigP {configChecks = [Check {checkType = Exclude, checkFilter = CheckInspection (Id {unId = \"STAN-0206\"}), checkScope = ScopeAll}], configRemoved = [], configIgnored = []})\n"
2023-12-22T00:13:46.798 helix_lsp::transport [ERROR] hacked-hls err <- "2023-12-22T06:13:46.798414Z | Debug | stan: Env vars: Fiasco (fromList [(E,\"No STAN_USE_DEFAULT_CONFIG Env Variable is set\")])\n"
2023-12-22T00:13:46.798 helix_lsp::transport [ERROR] hacked-hls err <- "2023-12-22T06:13:46.798875Z | Debug | Finished: InitialLoad Took: 0.51s\n"
2023-12-22T00:13:46.874 helix_lsp::transport [ERROR] hacked-hls err <- "2023-12-22T06:13:46.874564Z | Debug | stan: Map of checks per file: fromList [(\"/Users/roger/Documents/Dev/haskell-proj/haskell-language-server/plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs\",fromList [Id {unId = \"STAN-0214\"},Id {unId = \"STAN-0005\"},Id {unId = \"STAN-0215\"},Id {unId = \"STAN-0004\"},Id {unId = \"STAN-0007\"},Id {unId = \"STAN-0006\"},Id {unId = \"STAN-0210\"},Id {unId = \"STAN-0001\"},Id {unId = \"STAN-0211\"},Id {unId = \"STAN-0212\"},Id {unId = \"STAN-0003\"},Id {unId = \"STAN-0213\"},Id {unId = \"STAN-0002\"},Id {unId = \"STAN-0302\"},Id {unId = \"STAN-0009\"},Id {unId = \"STAN-0008\"},Id {unId = \"STAN-0301\"},Id {unId = \"STAN-0012\"},Id {unId = \"STAN-0207\"},Id {unId = \"STAN-0013\"},Id {unId = \"STAN-0010\"},Id {unId = \"STAN-0205\"},Id {unId = \"STAN-0011\"},Id {unId = \"STAN-0204\"},Id {unId = \"STAN-0016\"},Id {unId = \"STAN-0203\"},Id {unId = \"STAN-0017\"},Id {unId = \"STAN-0202\"},Id {unId = \"STAN-0014\"},Id {unId = \"STAN-0201\"},Id {unId = \"STAN-0015\"},Id {unId = \"STAN-0104\"},Id {unId = \"STAN-0105\"},Id {unId = \"STAN-0018\"},Id {unId = \"STAN-0106\"},Id {unId = \"STAN-0019\"},Id {unId = \"STAN-0101\"},Id {unId = \"STAN-0021\"},Id {unId = \"STAN-0102\"},Id {unId = \"STAN-0209\"},Id {unId = \"STAN-0020\"},Id {unId = \"STAN-0103\"},Id {unId = \"STAN-0208\"}])]\n"

@fendor
Copy link
Collaborator

fendor commented Dec 22, 2023

While they are debug logs, they are currently difficult to read and a bit overly verbose. Having legible logs is important for debugging HLS.

Rather than logging the show instance, log the information we care about. For example, in LogDebugStanConfigResult, log the location we've found for the configuration.
Logging the available checks may also be slight overkill.

@0rphee
Copy link
Collaborator Author

0rphee commented Dec 22, 2023

With some changes, how does this look like? Should I still disable the logging of the checks (even with a .stan.toml file, stan could ignore that via the env var)?

2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "2023-12-22T21:31:16.276992Z | Debug | stan: Config result using: [/Users/roger/.stan.toml]\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "Result:\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  ∩ Exclude\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "      ID: STAN-0213\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "      All files\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  ∩ Exclude\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "      ID: STAN-0206\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "      All files\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  ∩ Exclude\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "      ID: STAN-0103\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "      All files\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "With the following warnings:\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] 'hls-stan-plugin' doesn't receive CLI options for: checks\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] configChecks is set through the source: TOML\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] No TOML value is specified for key: remove\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] 'hls-stan-plugin' doesn't receive CLI options for: remove\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] configRemoved is set through the source: TOML\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] No TOML value is specified for key: ignore\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] 'hls-stan-plugin' doesn't receive CLI options for: ignore\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] configIgnored is set through the source: TOML\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "2023-12-22T21:31:16.277168Z | Debug | stan: EnvVars Fiasco:\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Error  ] No STAN_USE_DEFAULT_CONFIG Env Variable is set\n"
2023-12-22T15:31:16.277 helix_lsp::transport [ERROR] hacked-hls err <- "\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- "2023-12-22T21:31:16.500514Z | Debug | stan: Checks per file: Inspections set for /Users/roger/Documents/Dev/haskell-proj/haskell-language-server/plugins/hls-stan-plugin/src/Ide/Plugin/Stan.hs\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- "[ Id = STAN-0214\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0005\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0215\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0004\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0007\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0006\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0210\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0001\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0211\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0212\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0003\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0002\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0302\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0009\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0008\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0301\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0012\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0207\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0013\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0010\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0205\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0011\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0204\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0016\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0203\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0017\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0202\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0014\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0201\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0015\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0104\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0105\n"
2023-12-22T15:31:16.500 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0018\n"
2023-12-22T15:31:16.501 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0106\n"
2023-12-22T15:31:16.501 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0019\n"
2023-12-22T15:31:16.501 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0101\n"
2023-12-22T15:31:16.501 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0021\n"
2023-12-22T15:31:16.501 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0102\n"
2023-12-22T15:31:16.501 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0209\n"
2023-12-22T15:31:16.501 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0020\n"
2023-12-22T15:31:16.501 helix_lsp::transport [ERROR] hacked-hls err <- ", Id = STAN-0208 ]\n"

@fendor
Copy link
Collaborator

fendor commented Jan 4, 2024

@0rphee Much better! I still think logging the stan hints is overkill, as this will be spammed on every file save, right?
Maybe that's just my issue as I always run HLS with --debug logging level.

@fendor fendor added the status: needs review This PR is ready for review label Jan 4, 2024
@michaelpj
Copy link
Collaborator

I think the point of DEBUG is to log information that's useful for debugging, which might be a lot. If this is useful for debugging the stan plugin, then I don't see why it shouldn't get logged...

@0rphee
Copy link
Collaborator Author

0rphee commented Jan 8, 2024

I think this is fine now :)
The pretty printing of the debugging info is now done by removing terminal escape sequences from the ouput of the pretty printing functions, for now at least, because changes won't be made to the library at the moment (kowainik/trial#73).

Regarding showing each inspection name, I now agree with @fendor, since it was useful for me while doing this (there were mismatches between the loaded config, and what was actually applied, and that was the only way to notice), but now that same information is (correctly) provided by the first section.

This is how now all the debug ouput looks like:

2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "2024-01-08T02:34:01.617242Z | Debug | stan: Config result using: [/home/orphee/.stan.toml]\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "Result:\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  ∩ Exclude\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "      ID: STAN-0206\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "      All files\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "With the following warnings:\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] 'hls-stan-plugin' doesn't receive CLI options for: checks\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] configChecks is set through the source: TOML\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] No TOML value is specified for key: remove\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] 'hls-stan-plugin' doesn't receive CLI options for: remove\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] configRemoved is set through the source: TOML\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] No TOML value is specified for key: ignore\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] 'hls-stan-plugin' doesn't receive CLI options for: ignore\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Warning] configIgnored is set through the source: TOML\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "2024-01-08T02:34:01.617494Z | Debug | stan: EnvVars Fiasco:\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "  * [Error  ] No STAN_USE_DEFAULT_CONFIG Env Variable is set\n"
2024-01-07T20:34:01.617 helix_lsp::transport [ERROR] hacked-hls err <- "\n"
```<

Copy link
Collaborator

@fendor fendor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@michaelpj michaelpj merged commit 034b33e into haskell:master Jan 11, 2024
40 checks passed
soulomoon added a commit to soulomoon/haskell-language-server that referenced this pull request Jan 12, 2024
commit 2fe2d70
Merge: 034b33e bea1fed
Author: fendor <fendor@users.noreply.github.com>
Date:   Thu Jan 11 16:05:34 2024 +0100

    Merge pull request haskell#3941 from fendor/enhance/cabal-no-diags-if-disabled

    Don't produce diagnostics if plugin is turned off

commit bea1fed
Merge: e9aab3c 034b33e
Author: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Date:   Thu Jan 11 13:54:12 2024 +0000

    Merge branch 'master' into enhance/cabal-no-diags-if-disabled

commit e9aab3c
Author: Fendor <power.walross@gmail.com>
Date:   Wed Jan 10 17:18:39 2024 +0100

    Don't produce diagnostics if plugin is turned off

commit 034b33e
Author: 0rphee <0rph3e@proton.me>
Date:   Thu Jan 11 02:53:11 2024 -0600

    Use stan config files for stan plugin (haskell#3904) (haskell#3914)

    * Bump stan

    Needed in order to get the functions for getting the config, etc.

    * Use stan config files for stan plugin (haskell#3904)

    * Add test case for .stan.toml configuration

    * Fix windows tests

    See kowainik/stan#531

    ---------

    Co-authored-by: Michael Peyton Jones <me@michaelpj.com>
@0rphee 0rphee linked an issue Jan 28, 2024 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs review This PR is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hls-stan-plugin respect .stan.toml config file
3 participants