Skip to content

fix(ext): give the right fix in enable_services build errors - #444

Open
nicksinas wants to merge 4 commits into
mainfrom
fix/enable-services-rootfs-unit-advice
Open

nicksinas wants to merge 4 commits into
mainfrom
fix/enable-services-rootfs-unit-advice

Conversation

@nicksinas

Copy link
Copy Markdown
Contributor

Problem

enable_services only enables units that the extension ships. A unit from the rootfs, such as systemd-networkd.service, fails the build with this advice:

Error: enable_services lists systemd-networkd.service, but no such unit exists in the extension sysroot (...) under /etc/systemd/system or /usr/lib/systemd/system. Units shipped via overlay belong in overlay/usr/lib/systemd/system/.

That advice is wrong for a unit that the rootfs already ships. The fix is a .wants link in the overlay, and the error does not mention it. The schema text adds to the confusion: "Services must be available in the extension or base system". avocadoctl does not enable these units on the device either. It reads AVOCADO_ENABLE_SERVICES only for HITL drop-ins.

A unit with no install target gets an error that says "its [Install] section has no WantedBy=", even when it has no [Install] section. The error also does not mention that the build already reads [Install] from a drop-in.

Change

The changes are in the enable-services shell that create_confext_build_script generates (src/commands/ext/build.rs), the schema text and the CHANGELOG. The build still fails in every case. Only the error text changes.

  • A missing unit that is not a template is looked up in the rootfs sysroot ($AVOCADO_PREFIX/rootfs, under /etc/systemd/system and /usr/lib/systemd/system). If the unit is there, the error says it is a rootfs unit and prints the overlay link for each WantedBy= target. If the rootfs unit has no WantedBy=, the error says so and uses multi-user.target as an example. If the unit is in neither place, the error says to install the package or ship the unit in the overlay.
  • A bare template (foo@.service) skips the lookup and keeps the plain error, because a bare template cannot go in a .wants directory.
  • The no-target error gives the drop-in path overlay/usr/lib/systemd/system/<unit>.d/install.conf, with [Install] and WantedBy=multi-user.target.
  • The enable_services schema text now says what the build can enable, and how to enable a rootfs unit.

New output for the rootfs case:

Error: enable_services lists systemd-networkd.service, but systemd-networkd.service is a rootfs unit (/usr/lib/systemd/system/systemd-networkd.service), not part of the extension sysroot (...). This build only enables units the extension ships. Drop systemd-networkd.service from enable_services and ship this link in the overlay instead:
  overlay/etc/systemd/system/multi-user.target.wants/systemd-networkd.service -> /usr/lib/systemd/system/systemd-networkd.service

The rootfs lookup only reads files. It does not write to the sysroot or the overlay file list.

Testing

  • 3 new tests run the generated script against a temp sysroot: a rootfs unit with WantedBy=, a rootfs unit with no [Install], and an extension unit with no install target. All 3 fail before the change. All 13 enable-services and template tests pass after it.
  • On a qemux86-64 project with enable_services: [systemd-networkd.service], the built binary prints the error above with the real rootfs WantedBy= target. With the link in the overlay, as the error says, the build passes.
  • cargo fmt --all -- --check and RUST_MIN_STACK=8388608 cargo test pass.
  • cargo clippy --all-targets --all-features -- -D warnings on macOS gives 5 dead_code errors in src/utils/host_mounts.rs. They are on main too. This change adds no warnings.

The docs repo has its own copy of the schema (docs/src/schemas/avocado-config.json) with the old text. It needs a separate update.

A rootfs unit such as systemd-networkd.service in enable_services failed
with advice to put the unit in overlay/usr/lib/systemd/system/, which is
wrong for a unit the rootfs already ships. A unit with no install target
got an error that claimed an [Install] section, even when it had none, and
did not mention the drop-in that the build already reads.

- Look up a missing non-template unit in the rootfs sysroot and, when it
  is there, print the overlay .wants link for each WantedBy= target of the
  rootfs unit, or multi-user.target as an example when it has none
- Keep the build failure for a rootfs unit
- Give the drop-in path that can add WantedBy= in the no-target error
- Say in the enable_services schema text what the build can enable
- Add shell-run tests for both rootfs cases and the no-target case

Refs: (bug)avocado-cli-S3-enable-services-fails-for-rootfs-units-and-units-without-install.md
Copilot AI balanced review requested due to automatic review settings November 28, 2026 19:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unexpanded systemd specifiers produce unusable enablement paths, while rootfs symlinks resolved against the build container can trigger false missing-unit errors.

2 open issues
What changed in this PR

This PR improves enable_services build errors by distinguishing rootfs units from extension units and explaining the appropriate overlay workaround. Invalid declarations still fail the build.

Changes:

  • Adds rootfs lookup to suggest overlay links using the unit’s WantedBy= targets.
  • Clarifies how drop-ins can supply missing install targets and adds three regression tests.
  • Updates the configuration schema and changelog to explain the supported behavior.
File Description
src/​commands/​ext/​build.rs Improves generated build diagnostics and adds regression tests.
schemas/​avocado-config.json Clarifies extension-unit requirements and rootfs workarounds.
CHANGELOG.md Records the diagnostic and schema improvements.

Review effort: Balanced

Comment thread src/commands/ext/build.rs Resolved
Comment thread src/commands/ext/build.rs Resolved
The rootfs lookup for an enable_services unit followed an absolute
symlink inside the build container, not inside the rootfs, so it could
miss a rootfs unit and give the wrong advice. A WantedBy= with a
specifier such as %p.target was printed as a literal .wants path, which
systemd does not read.

- Follow unit symlinks inside the rootfs, and read WantedBy= from the
  file the link resolves to
- Expand %n, %N, %p and %% in the printed link, and tell the user to
  expand any other specifier by hand
- Add tests for an absolute symlink and for specifier targets

Refs: #444
The extension sysroot lookup tested the unit path with [ -f ], which
follows an absolute symlink in the build container, not in the sysroot.
An extension that ships its unit as an alias link to /usr/lib/... failed
the build with "no such unit".

- Add _avocado_resolve_in to follow links inside a given root, at most 8
- Use it for the extension sysroot lookup and the rootfs advice lookup
- Add a test for an extension unit behind an absolute symlink

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Unresolved unit-resolution and install-target parsing issues can misreport valid units as missing or recommend incorrect enablement links by ignoring intermediate symlinks, drop-ins, and empty-value resets.

No open issues 🎉

3 previously missed issues

In code that hasn't changed since last review

Medium severity Intermediate symlinks resolve outside the supplied root

src/​commands/​ext/​build.rs:1528

This follows symlinks on the unit file, but not in intermediate directories: with alias.service -> /lib/systemd/system/vendor.service and /lib -> /usr/lib inside the sysroot, the directory link still resolves against the build container. Both the extension and rootfs lookups can therefore report a valid unit as missing. Resolve path components within the supplied root, including intermediate directory symlinks.

Medium severity Empty WantedBy assignment fails to clear previous targets

src/​commands/​ext/​build.rs:1674

An empty WantedBy= resets earlier targets, but this extraction retains them: WantedBy=multi-user.target, followed by WantedBy=, then WantedBy=rescue.target, recommends links into both targets. Following that advice enables the rootfs unit in a target it explicitly removed. Parse assignments in order and clear the accumulated targets on an empty value.

Medium severity Rootfs lookup ignores install drop-ins

src/​commands/​ext/​build.rs:1674

The rootfs lookup reads only the main unit file, ignoring its install drop-ins. For example, a unit without [Install] whose foo.service.d/install.conf declares WantedBy=graphical.target is reported as having no target and gets a multi-user.target example instead; drop-ins that change existing targets also leave incorrect advice. Read the effective rootfs drop-ins, respecting directory precedence, assignment order, and empty-value resets, before recommending links.

2 resolved since last review

Review effort: Balanced

Three gaps were left in the enable_services unit lookup. The resolver
followed links on the last path part only, so an absolute directory link
in the middle of a unit path still resolved in the build container. An
empty WantedBy= or RequiredBy= did not drop the targets set before it, as
it does in systemd. The rootfs advice read only the unit file, so a
target from a rootfs drop-in was missed and the advice was wrong.

- Resolve each path part inside the root, with at most 8 links overall
- Parse [Install] keys in file order with awk, and clear the targets on
  an empty assignment
- Apply the unit, then its drop-ins sorted by basename, as systemd does,
  after it picks one drop-in per basename by directory precedence
- Read the rootfs drop-ins for the advice with the same rules
- Add tests for a middle directory link, for resets across drop-ins, and
  for rootfs drop-ins

Refs: #444
@nicksinas

Copy link
Copy Markdown
Contributor Author

Reply to the Copilot review of 9e147df:

  • Directory symlink in the middle of the path: agreed. The resolver now follows links for each part of the path inside the root, with at most 8 links in total. It uses POSIX sh only. A test covers lib -> /usr/lib in the middle of a unit path. Fixed in bc71c1c.
  • Empty WantedBy= or RequiredBy= does not reset: agreed. [Install] keys are now read in file order, and an empty value clears the earlier targets. As in systemd, the unit comes first, then its drop-ins sorted by basename. This applies to the extension path and the rootfs advice. Test added. Fixed in bc71c1c.
  • Rootfs advice ignores drop-ins: agreed. The rootfs advice now reads the unit's rootfs drop-ins with the same precedence, same-basename override and reset rules as the extension path. Test added. Fixed in bc71c1c.

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