Skip to content

lib/shell: add idempotent search-variable merge primitives - #9859

Open
khaneliman wants to merge 1 commit into
session-vars-1-search-varsfrom
session-vars-2-merge-primitives
Open

khaneliman wants to merge 1 commit into
session-vars-1-search-varsfrom
session-vars-2-merge-primitives

Conversation

@khaneliman

@khaneliman khaneliman commented Aug 28, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Adds the POSIX __hm_merge helper and one public
mergeSearchVariables { prepend, append } generator. The generator emits the
complete sourceable block: helper definition, merge calls, process marker, and
cleanup. Keeping those stages private prevents callers from omitting or
misordering them. Nothing consumes the generator yet; prependToVar is
untouched, so this PR changes no generated output.

Adding an already-present entry never duplicates it. On the first merge in a
process tree, detected through __HM_SESS_VARS_MERGED, configured entries take
configured precedence and may be repositioned. On later merges, existing
entries keep their position, so re-sourcing cannot reorder a path that
nix develop, direnv, or a wrapper put in front. Removal is join-based rather
than token-splitting so entries containing the separator stay idempotent.

The in-tree test suite (tests/lib/shell/search-variable-merge.nix) exercises
the complete generator across Bash, dash, and Zsh. It covers expansion,
prepend and append ordering, marker export, cleanup, and repeated sourcing.

Part of splitting #9735 into independently reviewable changes; the stack as a
whole supersedes that PR.

Checklist

  • Change is backwards compatible.
  • Code formatted with nix fmt.
  • Code tested through nix build .#test-all.
  • Test cases updated/added.
  • Commit messages follow the {component}: {description} format.

@khaneliman khaneliman changed the title session vars 2 merge primitives lib/shell: add idempotent search-variable merge primitives Aug 28, 2026
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch from 954e843 to 38c71ac Compare August 28, 2026 06:17
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch from 38c71ac to 5772763 Compare August 28, 2026 19:53
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch from 5772763 to d5eea22 Compare August 28, 2026 20:48
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch 2 times, most recently from 6807577 to 618888c Compare August 28, 2026 21:04
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch 3 times, most recently from fe8ec2c to 21d95a0 Compare August 29, 2026 04:47
@khaneliman
khaneliman marked this pull request as ready for review August 29, 2026 04:49
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch from 21d95a0 to 9a3c024 Compare September 9, 2026 22:13
@khaneliman
khaneliman removed this pull request from stack #9866 September 11, 2026 03:12
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch from 9a3c024 to 6b88e44 Compare September 11, 2026 03:12
@khaneliman
khaneliman added this pull request to stack #9926 September 11, 2026 03:12
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch from 6b88e44 to b373a11 Compare September 30, 2026 18:58
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch from b373a11 to 70f3963 Compare October 1, 2026 02:11
Adds a POSIX `__hm_merge` helper and one `mergeSearchVariables`
generator. The generator owns helper setup, prepend and append calls,
marker export, and cleanup so callers cannot misorder those stages.
Nothing consumes it yet; `prependToVar` is untouched.

Adding an entry that is already present never duplicates it. Whether an
already-present entry may be moved depends on where the merge runs:

- First merge in a process tree, detected by
  `__HM_SESS_VARS_MERGED` being unset: configured entries take their
  configured precedence and existing occurrences are repositioned.
- Any later merge: existing entries keep their position, so re-sourcing
  cannot reorder a path a development shell, direnv, or a wrapper put in
  front.

The complete generator exports the marker after the last merge, then
removes its helper and scratch variables. The marker is deliberately
separate from `__HM_SESS_VARS_SOURCED`, whose position relative to the
merges is not fixed.

Entries expand in a double-quoted shell context. Raw double quotes
remain shell syntax so quoted command substitutions keep working; `\"`
produces a literal quote.

The helper lives in `session-search-merge.sh` rather than a Nix string.
It contains no interpolation, so keeping it as shell source avoids three
layers of escaping and lets shellcheck read it.

Removal is join-based rather than token-splitting. Splitting on the
separator is not idempotent for entries that contain the separator:
merging `a:b` into `x:a:b:y` would grow on every source.

Verified in bash, dash, and zsh across substring entries and globs.
Coverage also includes separator-bearing values, marker transitions,
prepend and append ordering, cleanup, and repeated sourcing.
@khaneliman
khaneliman force-pushed the session-vars-2-merge-primitives branch from 70f3963 to 4a200a2 Compare November 18, 2026 01:13

This branch has not been deployed

No deployments
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.

1 participant