# Build option inheritance

**URL:** <https://ziggit.dev/t/build-option-inheritance/13786>\
**Category:** Help\
**Tags:** build-system\
**Created:** [January 3, 2026, 5:01pm UTC](https://ziggit.dev/t/build-option-inheritance/13786 "2026-01-03T17:01:50Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![LucasSantos91](https://ziggit.dev/user_avatar/ziggit.dev/lucassantos91/32/440_2.png) [@LucasSantos91](https://ziggit.dev/u/LucasSantos91)\
**Post date:** [January 4, 2026, 4:18am UTC](https://ziggit.dev/t/build-option-inheritance/13786/3 "2026-01-04T04:18:45Z")

</div>

Modules can be initiated with `null` target and optimization, in which case these will be inherited.  
However, in order for you to compile a module, at least the target needs to be explicitly specified, and the optimization mode will default to debug if not specified.  
Also note that `standardOptimizeOption` and its target counterpart do not return optionals. If you don’t explicitly specify these options in your build command, zig will give you a default value, which is not null.  
Put it all together, and you get this footgun:

```zig
const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOption(.{});
const exported_module = b.addModule("foo", .{
  .root_source_file = b.path("src/root.zig"),

  // Here you have given the module a non-null
  // optimize and target, so they will not be inherited.
  // If you don't specify them when importing your module
  // you'll end up with .Debug and native target
  .optimize = optimize,
  .target = target.
});

```

The proper way of doing it is:

```zig
// This is suitable for importing, but unsuitable for compilation.
const exported_module = b.addModule("foo", .{
  .root_source_file = b.path("src/root.zig"),
});

const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOption(.{});

// This is suitable for compilation, but unsuitable for importing.
const compiled_module = b.createModule(
  .root_source_file = b.path("src/root.zig"),
  .optimize = optimize,
  .target = target
);
// Only `compiled_module` can be used for compilation.
const exe = b.addExecutable(.{
  .name = "foo",
  .root_module = compiled_module,
});

// Tests are compiled as well
const test_exe = b.addTest(.{
  .name = "foo",
  .root_module = compiled_module,
});

```

---

_[View the full topic](https://ziggit.dev/t/build-option-inheritance/13786)._
