Skip to content

use ContextFromEnv() as the base context when args are passed - #466

Merged
jaypipes merged 1 commit into
jaypipes:mainfrom
jimmykarily:fix-context-from-args-env
May 14, 2026
Merged

jaypipes merged 1 commit into
jaypipes:mainfrom
jimmykarily:fix-context-from-args-env

Conversation

@jimmykarily

@jimmykarily jimmykarily commented May 14, 2026 •

Copy link
Copy Markdown
Contributor

Commit 4e7ac7a restored pre-0.23.0 behaviour for the zero-args call path, but the with-args path still starts from context.TODO() and silently drops environment variables like GHW_CHROOT, GHW_DISABLE_WARNINGS, and GHW_DISABLE_TOOLS.

In pre-0.23.0 ghw, option.Merge always called EnvOrDefaultChroot() (and its siblings) as defaults regardless of which options the caller passed, so e.g. block.New(WithDisableTools()) still picked up GHW_CHROOT. The 0.23.0 refactor lost that, breaking downstream callers that set the env var and expected it to compose with other options. A common pattern is test mocks that export GHW_CHROOT and rely on consumers (including indirect ones via libraries that call block.New with a couple of modifiers) to honour it.

Use ContextFromEnv() as the base inside ContextFromArgs so env-derived values survive unless overridden by an explicit Modifier, Option, or context.Context arg.

Includes regression tests covering each of: Modifier-only args, WithChroot override of env, old-style option.WithChroot override of env, and explicit context.Context replacement of the env-derived base.

NOTE: debugged and co-authored with Claude AI

Commit 4e7ac7a restored pre-0.23.0 behaviour for the zero-args call
path, but the with-args path still starts from context.TODO() and
silently drops environment variables like GHW_CHROOT,
GHW_DISABLE_WARNINGS, and GHW_DISABLE_TOOLS.

In pre-0.23.0 ghw, option.Merge always called EnvOrDefaultChroot() (and
its siblings) as defaults regardless of which options the caller passed,
so e.g. `block.New(WithDisableTools())` still picked up GHW_CHROOT. The
0.23.0 refactor lost that, breaking downstream callers that set the env
var and expected it to compose with other options. A common pattern is
test mocks that export GHW_CHROOT and rely on consumers (including
indirect ones via libraries that call block.New with a couple of
modifiers) to honour it.

Use ContextFromEnv() as the base inside ContextFromArgs so env-derived
values survive unless overridden by an explicit Modifier, Option, or
context.Context arg.

Includes regression tests covering each of: Modifier-only args,
WithChroot override of env, old-style option.WithChroot override of env,
and explicit context.Context replacement of the env-derived base.

Signed-off-by: Dimitris Karakasilis <dimitris@karakasilis.me>

@jaypipes jaypipes left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

@jimmykarily heyo! thank you very much for this submission (and the corresponding unit test). much appreciated! :)

@jaypipes
jaypipes merged commit d069362 into jaypipes:main May 14, 2026
15 checks passed
@github-project-automation github-project-automation Bot moved this from Under review 🔍 to Done ✅ in 🧙Issue tracking board May 14, 2026
dims added a commit to dims/rdma_topo that referenced this pull request May 14, 2026
PR jaypipes/ghw#466 ("use ContextFromEnv() as the base context when
args are passed") fixes a regression where pci.New(opts...) silently
dropped GHW_CHROOT and similar env vars. Bump ghw to the merge
commit and switch our own ctx construction to start from
ghw.ContextFromEnv() so the same env vars survive into the ctx we
hand to BuildTopology for VPD reads.

main.go now builds one ctx (ContextFromEnv -> optional --chroot ->
WithDisableTopology) and threads it through both pci.New and
BuildTopology, removing the parallel option.WithChroot path in
loadPCI. The pre-flight sysfs stat resolves the effective root with
the same precedence ghw uses internally: --chroot beats GHW_CHROOT
beats "/sys".

Signed-off-by: Davanum Srinivas <davanum@gmail.com>
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

Successfully merging this pull request may close these issues.

2 participants