den icon indicating copy to clipboard operation
den copied to clipboard

`den._.forward`: a second forward with the same fromClass / intoClass / intoPath is silently dropped, even when its `fromAspect` differs

Open sini opened this issue 2 weeks ago • 0 comments

If two den._.forwards in the same scope share fromClass, intoClass and intoPath but name different source aspects with fromAspect, only the first one gets delivered. The second is dropped with no warning, with or without guard.

Repro, short version (the full nix-unit test is in the original write-up below): fwd cls src builds a den._.forward with fromClass = _: cls, intoClass = _: "nixos", intoPath = _: [ "forwardTarget" ] and fromAspect = _: den.aspects.${src}.

den.aspects.a.src.items = [ "a" ];
den.aspects.b.src.items = [ "b" ];
den.aspects.igloo.includes = [ (fwd "src" "a") (fwd "src" "b") ];

Expected igloo.forwardTarget.items to be [ "a" "b" ]. Actual: [ "a" ]. Give the second source a different class (srcB) and both arrive. It bites in a home-manager setup too: three forwards into one user's home.file."x" from den.default.includes, and whichever srcu forward comes second is lost.

Cause: a forward's adapterKey is built from fromClass, intoClass and the static intoPath only (nix/lib/forward.nix), and routes are deduped on adapterKey@scope with the first one winning (nix/lib/aspects/fx/edges/route.nix). Two forwards that differ only in their source look like duplicates. Dropping the dedup isn't the fix -- the guarded path then fails because the adapter declares den.fwd.<adapterKey> twice. The key probably needs the source aspect in it, while a forward reached twice should still collapse.

Still happens on main. No fix PR yet.

Original write-up

Summary

Suppose two den._.forwards are reached in the same scope and share fromClass, intoClass and intoPath, but name different source aspects via fromAspect. Only the first-declared one is delivered. The second is dropped with no warning or error. This happens with and without guard, so it affects both the direct forward path and the adapter path.

The same thing happens in a realistic home-manager setup. Three producers (the host and two users) forward into one user's home.file path from one den.default.includes list, and the user receives only two of the three. The second forward of the shared user class is lost, whichever user it belongs to.

Reproducer

Drop this file into templates/ci/modules/deadbugs/forward-same-class-distinct-sources.nix and run:

git add templates/ci/modules/deadbugs/forward-same-class-distinct-sources.nix
just test deadbugs.forward-same-class-distinct-sources

(just test is nix-unit --override-input den . --flake ./templates/ci#.tests.<name>. The file must be git-added, because the flake is git-filtered.)

# Two `den._.forward`s in one include list, same fromClass / intoClass / intoPath, DIFFERENT fromAspect.
# Each forward names its own source aspect, so each source's content should reach the target. The `control`
# cell differs only in the second source's class (`srcB` instead of `src`), and is expected to deliver both.
{ denTest, ... }:
let
  targetModule =
    { lib, ... }:
    {
      options.forwardTarget = lib.mkOption {
        type = lib.types.submodule {
          options.items = lib.mkOption {
            type = lib.types.listOf lib.types.str;
            default = [ ];
          };
        };
        default = { };
      };
    };

  cell =
    {
      guarded,
      classB ? "src",
    }:
    denTest (
      {
        den,
        lib,
        igloo,
        ...
      }:
      let
        fwd =
          cls: src:
          { class, aspect-chain }:
          den._.forward (
            {
              each = lib.singleton null;
              fromClass = _: cls;
              intoClass = _: "nixos";
              intoPath = _: [ "forwardTarget" ];
              fromAspect = _: den.aspects.${src};
            }
            // lib.optionalAttrs guarded { guard = _: true; }
          );
      in
      {
        den.hosts.x86_64-linux.igloo = { };

        den.aspects.a.src.items = [ "a" ];
        den.aspects.b.${classB}.items = [ "b" ];

        den.aspects.igloo = {
          nixos.imports = [ targetModule ];
          includes = [
            (fwd "src" "a")
            (fwd classB "b")
          ];
        };

        expr = lib.sort lib.lessThan igloo.forwardTarget.items;
        expected = [
          "a"
          "b"
        ];
      }
    );
in
{
  flake.tests.deadbugs.forward-same-class-distinct-sources = {
    test-unguarded = cell { guarded = false; };
    test-guarded = cell { guarded = true; };
    test-control-unguarded-distinct-class = cell {
      guarded = false;
      classB = "srcB";
    };
    test-control-guarded-distinct-class = cell {
      guarded = true;
      classB = "srcB";
    };
  };
}

Expected

All four tests pass. Each forward names its own source aspect, so igloo.forwardTarget.items should be [ "a" "b" ] in every cell.

Actual

✅ test-control-guarded-distinct-class
✅ test-control-unguarded-distinct-class
❌ test-guarded        (diff: expected [ "a" "b" ], got [ "a" ])
❌ test-unguarded      (diff: expected [ "a" "b" ], got [ "a" ])
😢 2/4 successful

The two control cells differ only in giving the second source a different class (srcB), and they deliver both items. A plain forward does reach the target. What is lost is the second forward that shares the first one's class and path. Nothing is printed when it is dropped.

The same drop in a home-manager setup

This is a host igloo with users tux, pingu and aaron. The host's own home-manager config sets home.file."x".text = "R" for tux. Three forwards in den.default.includes target tux's home.file."x":

  • H: from class srci of aspect igloo;
  • P: from class srcu of aspect pingu;
  • A: from class srcu of aspect aaron.
include order with guard = { options, ... }: options ? home; without guard
H, P, A R\nH\nP (A missing) H\nP\nR (A missing)
A, P, H R\nA\nH (P missing) A\nH\nR (P missing)

Expected: all of H, P and A present in both orders. The srcu forward declared first survives, and the second is dropped, whichever user it belongs to.

Versions

  • den 2527e4d234cc4b0a85cdf8de1d60a28a00c44d5e (main as of 2026-11-18), using templates/ci with its own lock (nixpkgs nixos-26.05pre1004030.64c08a7ca051).
  • I got identical results on three evaluators: Nix 2.35.6, Determinate Nix 3.24.2 (Nix 2.35.3), and Lix 2.96.0. The test run above used nix-unit 2.35.1.

Suspected cause

These are the lines I read at 2527e4d2:

  1. Key. nix/lib/forward.nix:65-71 builds a forward's adapterKey from fromClass, intoClass and the static intoPath only. The source aspect (fromAspect) is not part of it. forwardItem builds this key for every forward, guarded or not.
  2. The key travels with the route. nix/lib/aspects/fx/handlers/compile-forward.nix:36-41 registers a forward that is not tier-1 as spec // { sourceScopeId; __complexForward = true; }, so the route carries adapterKey.
  3. Dedup by key. nix/lib/aspects/fx/edges/route.nix:511-514 ("same adapterKey@scope first-occurrence wins") marks every later route with the same "${adapterKey}@${sourceScopeId}" as suppressed. keptRoutes (route.nix:528-534) then filters it out.

So two forwards that differ only in their source aspect get the same key, and the second is discarded as a duplicate.

To check this, I set isDupKey = false at route.nix:513 in a local clone:

  • The unguarded cells then deliver everything: [ "a" "b" ], and H\nP\nA\nR / A\nP\nH\nR in the home-manager case.
  • The guarded cells instead fail with The option 'den.fwd."src/nixos/forwardTarget"' ... is already declared. The adapter declares its intermediate option under the same key (nix/lib/aspects/fx/handlers/forward.nix:69, options.den.fwd.${adapterKey}).
  • In one ordering of the home-manager case, that error also appeared for a key that has only one declared forward. So the dedup also collapses a single forward that is reached more than once.

Simply removing the dedup is therefore not a fix. I suspect a forward's identity, both for the dedup and for the adapter's den.fwd.<key> option, needs to include its source aspect, so that two forwards with the same class and path but different sources stay distinct. A forward reached twice should still collapse. The edge-trace id (forwardId, route.nix:884-887) is built from the same key, so I suspect the trace shows the two forwards as one as well. I did not check this.

sini avatar Nov 18 '26 19:11 sini