如何在不下载GHC的情况下查询对应快照的GHC版本
I totally get where you're coming from—using stack query compiler wanted in CI is a non-starter because it triggers a full GHC download, which completely defeats the purpose of using the GHC version as a cache key. Here are a few reliable, lightweight ways to get the GHC version tied to your target snapshot without pulling down the compiler itself:
Method 1: Use stack resolver info (simplest approach)
This command queries Stack's resolver metadata directly, no GHC download required. It returns basic details about the resolver, including the exact GHC version.
For a specific hardcoded resolver:
stack resolver info lts-20.13
For the resolver defined in your project's stack.yaml:
Automatically pull the resolver from your config and extract the GHC version:
RESOLVER=$(stack resolver show) stack resolver info "$RESOLVER" | grep "GHC version:" | awk '{print $3}'
Method 2: Parse snapshot metadata with JSON
If you prefer structured data (ideal for scripting in CI), use stack ls snapshots with JSON output and a tool like jq to filter the result:
# For a hardcoded resolver stack ls snapshots --json | jq '.[] | select(.resolver == "lts-20.13") | .ghc_version' # For your project's resolver RESOLVER=$(stack resolver show) stack ls snapshots --json | jq --arg res "$RESOLVER" '.[] | select(.resolver == $res) | .ghc_version'
This pulls the full list of available snapshots (a tiny metadata payload) and extracts the GHC version for your target resolver—no heavy downloads involved.
Method 3: Optimize CI with resolver index caching
In CI environments, cache the Stack resolver index directory (~/.stack/index) to speed up these metadata queries even more. The index is small and rarely changes, so caching it avoids re-fetching resolver details on every build, making your cache key lookup nearly instant.
Why these work (and stack query compiler wanted doesn't)
The stack query compiler wanted command is designed to resolve and prepare the compiler for active use, which includes downloading it if it's missing from your system. The methods above only interact with Stack's resolver metadata—lightweight files that map snapshots to GHC versions, package sets, and other metadata—so they never trigger a GHC download.
内容的提问来源于stack exchange,提问作者John Ky

