den icon indicating copy to clipboard operation
den copied to clipboard

BUG: Providers that include parametric aspects are evaluated twice when imported

Open DylanRJohnston opened this issue 7 months ago • 7 comments

Summary

When a provider includes a parametric aspect, it behaves normally when the namespace is local, but is evaluated twice when imported.

Test

{ denTest, lib, ... }:
{
  flake.tests.deadbugs-issue-400 =
    let
      module =
        { inputs, ... }:
        {
          imports = [
            inputs.den.flakeModule
            (inputs.den.namespace "test" true)
          ];

          test.aspect =
            { host }:
            {
              nixos.test = [ "aspect-${host.name}" ];
            };

          test.provided._.provider =
            { host }:
            {
              nixos.test = [ "provider-${host.name}" ];
            };

          test.provided._.included = {
            includes = [
              (
                { host }:
                {
                  nixos.test = [ "included-${host.name}" ];
                }
              )
            ];
          };
        };

      external-module =
        inputs:
        (inputs.nixpkgs.lib.evalModules {
          specialArgs = { inherit inputs; };
          modules = [ module ];
        }).config.flake;

      test-module.nixos.options.test = lib.mkOption {
        type = lib.types.listOf lib.types.str;
        default = [ ];
      };
    in
    {
      test-internal-namespace = denTest (
        {
          test,
          igloo,
          ...
        }:
        {
          imports = [ module ];

          den.hosts.x86_64-linux.igloo.users.tux = { };
          den.aspects.igloo.includes = [ test-module ];

          den.ctx.host.includes = [
            test.aspect
            test.provided._.provider
            test.provided._.included
          ];

          expr = igloo.test;
          expected = [
            "included-igloo"
            "provider-igloo"
            "aspect-igloo"
          ];
        }
      );

      test-external-namespace = denTest (
        {
          inputs,
          test,
          igloo,
          ...
        }:
        {
          imports = [
            (inputs.den.namespace "test" [ (external-module inputs) ])
          ];

          den.hosts.x86_64-linux.igloo.users.tux = { };
          den.aspects.igloo.includes = [ test-module ];

          den.ctx.host.includes = [
            test.aspect
            test.provided._.provider
            test.provided._.included
          ];

          expr = igloo.test;
          expected = [
            "included-igloo"
            "provider-igloo"
            "aspect-igloo"
          ];
        }
      );
    };
}

test-external-namespace fails with an extra included-igloo

[
    "included-igloo"
    "included-igloo"
     "provider-igloo"
     "aspect-igloo"
]

DylanRJohnston avatar Apr 08 '26 07:04 DylanRJohnston

Interestingly using the namespace "raw" works

test-external-namespace-raw = denTest (
        {
          inputs,
          igloo,
          ...
        }:
        let
          test = (external-module inputs).denful.test;
        in
        {

          den.hosts.x86_64-linux.igloo.users.tux = { };
          den.aspects.igloo.includes = [ test-module ];

          den.ctx.host.includes = [
            test.aspect
            test.provided._.provider
            test.provided._.included
          ];

          expr = igloo.test;
          expected = [
            "included-igloo"
            "provider-igloo"
            "aspect-igloo"
          ];
        }
      );

DylanRJohnston avatar Apr 08 '26 07:04 DylanRJohnston

This is broken however

      test-external-namespace-raw-aliased = denTest (
        {
          inputs,
          igloo,
          den,
          ...
        }:
        {
          den.ful.test = (external-module inputs).denful.test;

          den.hosts.x86_64-linux.igloo.users.tux = { };
          den.aspects.igloo.includes = [ test-module ];

          den.ctx.host.includes = [
            den.ful.test.aspect
            den.ful.test.provided._.provider
            den.ful.test.provided._.included
          ];

          expr = igloo.test;
          expected = [
            "included-igloo"
            "provider-igloo"
            "aspect-igloo"
          ];
        }
      );

which makes think it has something to do with the den.ful aliasing?

DylanRJohnston avatar Apr 08 '26 07:04 DylanRJohnston

Cutting out the self parts for brevity, this is the provider with the include before aliasing

{
  _ = { };
  __functor = "lambda __functor @ /nix/store/g3phy1x0fbigpha8cg7iz8m0f71h3mq1-source/nix/lib/parametric.nix:67:19";
  description = "Aspect included";
  includes = [
    {
      _ = { };
      __functor = "lambda __functor @ /nix/store/g3phy1x0fbigpha8cg7iz8m0f71h3mq1-source/nix/lib/aspects/types.nix:37:27";
      description = "Aspect [definition 1-entry 1]";
      includes = [ ];
      meta = {
        adapter = null;
        file = "<unknown-file>";
        loc = [
          "den"
          "ful"
          "test"
          "provided"
          "provides"
          "included"
          "includes"
          "[definition 1-entry 1]"
        ];
        name = "den.ful.test.provided.provides.included.includes.[definition 1-entry 1]";
      };
      name = "[definition 1-entry 1]";
      provides = "repeated";
    }
  ];
  meta = {
    adapter = null;
    file = "<unknown-file>";
    loc = [
      "den"
      "ful"
      "test"
      "provided"
      "provides"
      "included"
    ];
    name = "den.ful.test.provided.provides.included";
  };
  name = "included";
  provides = "repeated";
}

after aliasing

{
  _ = { };
  __functor = "lambda __functor @ /nix/store/bhadm8aiirfnldbfcacknmi7i551ydr3-source/nix/lib/parametric.nix:67:19";
  description = "Aspect included";
  includes = [
    {
      _ = { };
      __functor = "lambda __functor @ /nix/store/bhadm8aiirfnldbfcacknmi7i551ydr3-source/nix/lib/aspects/types.nix:37:27";
      description = "Aspect [definition 1-entry 1]";
      includes = [ ];
      meta = {
        adapter = null;
        file = "<unknown-file>";
        loc = [
          "den"
          "ful"
          "test"
          "provided"
          "provides"
          "included"
          "includes"
          "[definition 1-entry 1]"
        ];
        name = "den.ful.test.provided.provides.included.includes.[definition 1-entry 1]";
        self = "lambda @ /nix/store/660ljssgj5ky0ph0z8pwc1jw8q39xb6j-source/lib/options.nix:430:7";
      };
      name = "[definition 1-entry 1]";
      provides = "repeated";
    }
    {
      _ = { };
      __functor = "lambda __functor @ /nix/store/bhadm8aiirfnldbfcacknmi7i551ydr3-source/nix/lib/aspects/types.nix:37:27";
      description = "Aspect [definition 1-entry 1]";
      includes = [ ];
      meta = {
        adapter = null;
        file = "<unknown-file>";
        loc = [
          "den"
          "ful"
          "test"
          "provided"
          "provides"
          "included"
          "includes"
          "[definition 2-entry 1]"
        ];
        name = "den.ful.test.provided.provides.included.includes.[definition 2-entry 1]";
        self = "lambda @ /nix/store/660ljssgj5ky0ph0z8pwc1jw8q39xb6j-source/lib/options.nix:430:7";
      };
      name = "[definition 1-entry 1]";
      provides = "repeated";
    }
  ];
  meta = {
    adapter = null;
    file = "<unknown-file>";
    loc = [
      "den"
      "ful"
      "test"
      "provided"
      "provides"
      "included"
    ];
    name = "den.ful.test.provided.provides.included";
    self = "lambda @ /nix/store/660ljssgj5ky0ph0z8pwc1jw8q39xb6j-source/lib/options.nix:430:7";
  };
  name = "included";
  provides = "repeated";
}

DylanRJohnston avatar Apr 08 '26 08:04 DylanRJohnston

Hey @DylanRJohnston, thanks for the detailed report and investigating!

Looks like something related with our latest changes, includes being duplicated makes me think that when re-assigning to den.ful. from the upstream flake we have some merging (dup) we don't want. I'll try to investigate tonight, and perhaps in the meantime @sini can also have some insight on what we are missing.

Thanks for providing tests we can just paste into Den repo :) much helpful, very awesome!

vic avatar Apr 08 '26 13:04 vic

I'll take a look in an hour or so. :)

sini avatar Apr 08 '26 14:04 sini

edit: seems it's actually simpler.

The root cause is the _ → provides alias in aspectSubmodule (lib.mkAliasOptionModule [ "_" ] [ "provides" ]). When an external namespace is evaluated via lib.evalModules, the resulting config contains both _ and provides as concrete attributes with identical content. On re-import via config.den.ful.${name} = denful in namespace.nix, the module system sees both paths as separate definitions for the same provides option. Since includes is listOf, the merge concatenates both — duplicating entries.

Fix: strip _ from external denful configs before assignment in namespace.nix. PR incoming with the reporter's test cases.

sini avatar Apr 08 '26 14:04 sini

omg! looks like I forgot about _ here: https://github.com/vic/den/commit/892a94faaad31b3da4613478d1db0e81605b14f1#diff-ea38352269870765d689ce712d865ce43c255473665627fa4b405cddd17f0798L12-L18, good to have tests now!

thanks @sini and @DylanRJohnston

vic avatar Apr 08 '26 15:04 vic