From bb370482967e54321ae853537e6ce43f60e31099 Mon Sep 17 00:00:00 2001 From: Drew Hess Date: Mon, 10 Oct 2022 12:07:17 +0100 Subject: [PATCH] fix: Add a project-local `.envrc` file. This fixes the issue described here: https://github.com/hackworthltd/primer-app/pull/547#issuecomment-1273060657 It does so by configuring `direnv` to write its cache for this project in `~/.cache/direnv/layouts`. This is a bit intrusive, but without this workaround, `vite` commands search the local `.direnv` cache for dependencies, and it adds about 10 seconds or more to every `vite` command. Note that the fix comes from here: https://github.com/direnv/direnv/wiki/Customizing-cache-location Before falling back to this workaround, I also tried the following Vite configuration tricks, but none of them worked: https://vitejs.dev/config/server-options.html#server-watch https://vitejs.dev/config/dep-optimization-options.html#optimizedeps-exclude There are multiple (somewhat) related issues currently filed on Vite which suggest that convincing Vite not to look everywhere in the project root directory is a known issue: https://github.com/vitejs/vite/pull/8778/files https://github.com/vitejs/vite/issues/7363 https://github.com/vitejs/vite/issues/8341 --- .envrc | 13 +++++++++++++ .gitignore | 1 - README.md | 8 +------- 3 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 00000000..b0c2de0b --- /dev/null +++ b/.envrc @@ -0,0 +1,13 @@ +# Prevent direnv from creating a local .direnv cache. Otherwise, 'vite' +# commands will be incredibly slow to start. + +: ${XDG_CACHE_HOME:=$HOME/.cache} +declare -A direnv_layout_dirs +direnv_layout_dir() { + echo "${direnv_layout_dirs[$PWD]:=$( + local path="${PWD//[^a-zA-Z0-9]/-}" + echo "$XDG_CACHE_HOME/direnv/layouts/${path:1}" + )}" +} + +use flake diff --git a/.gitignore b/.gitignore index 78babee3..a6c81274 100644 --- a/.gitignore +++ b/.gitignore @@ -13,7 +13,6 @@ result* # direnv. .direnv/ -.envrc # tsc tsconfig.tsbuildinfo diff --git a/README.md b/README.md index 256166dd..c67e144a 100644 --- a/README.md +++ b/README.md @@ -182,13 +182,7 @@ On the other hand, for production builds and CI, you can rest assured that TypeS We've tried to configure the project such that LSP and other tooling should "just work" in your editor, but you still might need to do some configuration on your end to hook it up. At the very least, the Nix development shell provides binaries for the TypeScript language server and `eslint` support, so you shouldn't need to download these or otherwise install them in your environment. (However, note to Emacs LSP users: it may prompt you whether it should automatically download a helper, and in my experience, this is usually necessary. I haven't gotten to the bottom of why this is required. -- @dhess) -Probably the easiest way to ensure your editor sees the right environment is to add [`direnv`](https://direnv.net) support to your shell & editor, and then use [`nix-direnv`](https://github.com/nix-community/nix-direnv) to ensure that your editor sees the tools that the Nix shell provides. If you've got `nix-direnv` in your environment, you can do this by adding a file to the top-level project directory named `.envrc`, which contains just this one line: - -``` -use flake -``` - -If you need a tool that's not provided by the project's Nix development shell and you think others would benefit from it, as well, please make a PR! +Probably the easiest way to ensure your editor sees the right environment is to add [`direnv`](https://direnv.net) support to your shell & editor, and then use [`nix-direnv`](https://github.com/nix-community/nix-direnv) to ensure that your editor sees the tools that the Nix shell provides. The project includes an `.envrc` file which will do the right thing once you run `direnv allow` in the project root directory. ### CI & Chromatic