Skip to content

Commit

Permalink
Use a much more pure env for LSP tests (only HOME and PATH)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasjm committed Nov 22, 2023
1 parent 63aecc3 commit 6af215a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 6 additions & 0 deletions tests/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
packages = rec {
inherit tests;
inherit (pkgs) cabal2nix;

# Print a trivial PATH that we can use to run kernel and LSP tests, to ensure
# they aren't depending on anything on the test machine's PATH.
print-basic-path = pkgs.writeShellScriptBin "codedown-artifact-sizes.sh" ''
echo ${pkgs.lib.makeBinPath (with pkgs; [busybox bash])}
'';
};

defaultPackage = packages.tests;
Expand Down
11 changes: 7 additions & 4 deletions tests/src/TestLib/LSP.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import TestLib.Aeson
import TestLib.Types
import TestLib.Util
import UnliftIO.Directory
import UnliftIO.Environment (getEnvironment)
import UnliftIO.Exception
import UnliftIO.IO
import UnliftIO.Process


Expand Down Expand Up @@ -151,6 +151,9 @@ withLspSession' handleFn name filename codeToTest extraFiles session = do
info [i|LSP command: #{lspCommand}|]

homeDir <- liftIO $ createTempDirectory currentFolder (T.unpack (name <> "_home"))
pathToUse <- withFile "/dev/null" WriteMode $ \devNullHandle ->
(T.unpack . T.strip . T.pack) <$> readCreateProcess ((proc "nix" ["run", ".#print-basic-path"]) { std_err = UseHandle devNullHandle }) ""

dataDir <- liftIO $ createTempDirectory currentFolder (T.unpack name)

forM_ extraFiles $ \(path, bytes) -> do
Expand All @@ -172,11 +175,11 @@ withLspSession' handleFn name filename codeToTest extraFiles session = do
, logMessages = True
}

env <- getEnvironment
-- env <- getEnvironment
-- let cleanEnv = [(k, v) | (k, v) <- env, k /= "PATH", k /= "HOME", k /= "GHC_PACKAGE_PATH"]
let cleanEnv = [(k, v) | (k, v) <- env, k /= "GHC_PACKAGE_PATH"]

let configEnv = maybe mempty (fmap (bimap T.unpack T.unpack) . M.toList) (lspConfigEnv config)
let finalEnv = ("HOME", homeDir) : (configEnv <> cleanEnv)
let finalEnv = ("HOME", homeDir) : ("PATH", pathToUse) : configEnv
info [i|Language server environment: #{finalEnv}|]
let modifyCp cp = cp { env = Just finalEnv
, cwd = Just homeDir }
Expand Down

0 comments on commit 6af215a

Please sign in to comment.