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

PPX being enabled with an environment variable is not handled by lsp #1394

Closed
mattiasdrp opened this issue Oct 29, 2024 · 2 comments
Closed

Comments

@mattiasdrp
Copy link

mattiasdrp commented Oct 29, 2024

Let's say I have a PPX that will transform [@myppx "name"] into Format.eprintf "%s" name

And this PPX is only active if the PROFILER environment variable is set to any value like this:

olet make_ppx_profiler () =
  match Sys.getenv_opt "PROFILER" with Some "" | None -> false | _ -> true

(** [mapper] inherits from Ast_traverse.map because we want to take a parsetree
    and return a new parsetree that have been rewritten or not by this ppx *)
let mapper =
  if make_ppx_profiler () then
    object
      inherit Ppxlib.Ast_traverse.map as super

      method! expression e =
        let detected_rewriters =
          (* The list of attributes is reverted to make sure that we preprocess
             them from left to right *)
          Rewriter.extract_rewriters (List.rev e.pexp_attributes)
        in
        (* Remove the handled attributes that have been transformed in rewriters *)
        Expression.remove_attributes e
        (* Transform the expression with the help of the list of rewriters *)
        |> Expression.rewrite detected_rewriters
        (* Gives the hand back to Ast_travers.map to keep iterating *)
        |> super#expression
    end
  else
    object
      inherit Ppxlib.Ast_traverse.map
    end

let () =
  Ppxlib.Driver.register_transformation "ppx_profiler" ~impl:mapper#structure

The issue now is that in my editor, if I start editing code and add improper values, no errors will be displayed and I'll only find them when compiling with PROFILER=true dune build

I found two solutions for this:

  • PROFILER=true dune build -w
  • export PROFILER=true && emacs main.ml

I wondered if there was a solution to ask the ocaml-lsp-server to compile with some environment variables.

@voodoos
Copy link
Collaborator

voodoos commented Oct 30, 2024

I don't think there is such an option, but starting your editor (which itself will start the lsp server) with the correct environment variable like you do in your second solution looks like a reasonable thing to do.

@mattiasdrp
Copy link
Author

Yep, that's what I feared. At least my solution is easy to use (I can even set the environment variable directly in my editor and restart the lsp server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants