Skip to content

packages: staged_install returns sentinel-less dirs as installed; rp2040 unit test writes into the real global cache #2043

Closed

Description

@zackees

Symptom

On a Linux dev box, prod-mode RP2040 Arduino-Pico builds (board = pico, board_build.core = earlephilhower) failed with:

build error: io error: No such file or directory (os error 2)

The prod cache entry ~/.fbuild/prod/cache/platforms/earlephilhower-arduino-pico/3390eb851c5aa882/4.5.3/ contained only:

4.5.3/variants/fbuild-test-rpipico/memmap_default.ld

It had no .install_complete sentinel, no rp2040-4.5.3/ tree, and no row in index.sqlite. fbuild still used it as the framework root and found 0 core sources. After the directory was quarantined, the same build re-downloaded 4.5.3 and succeeded. The new entry has .install_complete plus an index row.

The error string and environment (Linux, 2.5.50, RP2040) match #1977. That issue was closed as an Mbed-dispatch problem. A polluted cache entry like this one may have contributed there too.

Two bugs combine here (evidence at ea168085)

1. staged_install treats any existing directory as installed and ignores the sentinel

PackageBase::is_cached() (crates/fbuild-packages-fetch/src/lib.rs:393) is documented to require the .install_complete sentinel. Its docs say a directory without one "is an incomplete or partial install ... The caller should treat this as 'not installed' so the package is re-extracted". There is a test for that contract at lib.rs:933.

staged_install_impl does not follow that contract. It returns early whenever the path merely exists:

  • crates/fbuild-packages-fetch/src/lib.rs:479 if install_path.exists() { self.touch_disk_cache(); return Ok(install_path); }
  • lib.rs:487 the same check for target_path
  • lib.rs:502 / lib.rs:506 the same pair again after install_lock is acquired

So Rp2040Cores::ensure_installed (crates/fbuild-library/src/library/rp2040_core.rs:189) does this:
is_installed() returns false (no sentinel, no Arduino.h), then it calls staged_install, which sees the dir exists and returns it. The install is never repaired. Every framework that uses staged_install has the same flaw. The staging-dir plus rename commit (lib.rs:576) is atomic, but this short-circuit defeats it: any foreign directory at the final path counts as installed. Examples are a stray write, a CI cache restored without the sentinel, or a legacy partial tree.

Expected: the early returns should apply the same is_complete_install check as is_cached(). An incomplete directory should be moved aside or removed and then re-installed under the install lock. It should not be returned.

2. A unit test writes into the real global cache

test_generate_linker_script_substitutes_family_values (crates/fbuild-build-arm/src/rp2040/orchestrator.rs:1490) builds the framework with Rp2040Cores::new(tmp.path()) (:1494). That uses the tempdir only as the project dir. PackageBase::new then calls Cache::new, and Cache::new uses fbuild_paths::get_cache_root() unconditionally (crates/fbuild-packages-fetch/src/cache.rs:37). So get_variant_dir("fbuild-test-rpipico") resolves to ~/.fbuild/<mode>/cache/platforms/earlephilhower-arduino-pico/3390eb851c5aa882/4.5.3/variants/fbuild-test-rpipico/. The test then runs create_dir_all + fs::write of memmap_default.ld there (orchestrator.rs:1497-1505).

  • When 4.5.3 is not yet installed (fresh cache, prod mode because tests run without FBUILD_DEV_MODE), find_core_root falls back to the bare 4.5.3/ dir. The test then creates exactly the stray 4.5.3/variants/fbuild-test-rpipico/ tree seen above, which bug 1 then trusts forever.
  • When 4.5.3 is installed, it writes a junk variant into the real install under rp2040-4.5.3/variants/.

Tests inside fbuild-library already avoid this with the #[cfg(test)] Rp2040Cores::with_cache_root (rp2040_core.rs:59), for example rp2040_core.rs:336. That constructor is cfg(test)-private, so the cross-crate test in fbuild-build-arm can't use it.

Suggested direction (not implemented here)

  • Fix 1 is the real defect. Make the staged_install_impl existence checks sentinel-aware, and add a test: a pre-existing install dir without the sentinel must be re-installed, not returned.
  • Fix 2: give the fbuild-build-arm test an isolated cache root, either through a test-visible constructor or by overriding the cache root the same way other tests isolate it. Then audit the other *Cores::new(tmp.path()) test sites that write through framework path getters.

Local remediation done

The broken entry was quarantined (not deleted) and re-installed by a normal prod build. There was no index row to reset.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions