@@ -1356,6 +1356,31 @@ std::optional<Refusal> stage_member(PackRun& run, GroupJob& g, MemberJob& m) {
13561356 return std::nullopt ;
13571357}
13581358
1359+ // Copy a provider's distributable `product` into `dir` under its own name, with
1360+ // those of `beside` (outputs the product consumed) below the product's own
1361+ // directory, at their places relative to it. Each destination is replaced, and
1362+ // nothing else in `dir` is touched. Returns why it failed, or nothing.
1363+ std::string copy_distributable (const std::filesystem::path& product,
1364+ const std::vector<std::filesystem::path>& beside,
1365+ const std::filesystem::path& dir) {
1366+ namespace fs = std::filesystem;
1367+ const auto base = product.parent_path ().lexically_normal ();
1368+ std::vector<std::pair<fs::path, fs::path>> copies{{product, dir / product.filename ()}};
1369+ for (auto const & b : beside) {
1370+ const auto rel = b.lexically_normal ().lexically_relative (base);
1371+ if (rel.empty () || *rel.begin () == " .." ) continue ;
1372+ copies.emplace_back (b, dir / rel);
1373+ }
1374+ std::error_code ec;
1375+ for (auto const & [from, to] : copies) {
1376+ fs::remove_all (to, ec);
1377+ if (to.has_parent_path ()) fs::create_directories (to.parent_path (), ec);
1378+ fs::copy (from, to, fs::copy_options::recursive, ec);
1379+ if (ec) return std::format (" cannot write {}: {}" , to.string (), ec.message ());
1380+ }
1381+ return {};
1382+ }
1383+
13591384// ─── The dispatch pass ───────────────────────────────────────────────
13601385//
13611386// A `role = "artifact"` action is a ninja edge, and the staged tree is
@@ -1637,18 +1662,20 @@ std::optional<Refusal> dispatch_group(PackRun& run, GroupJob& g, mcpp::build::Ba
16371662 reported.push_back (std::move (abs));
16381663 }
16391664 if (!produced) continue ;
1640- // `-o <directory>/`: the distributable below it, under its own name.
1665+ // `-o <directory>/`: the distributable below it, under its own name,
1666+ // with the outputs it consumed that lie in its own directory, at their
1667+ // places relative to it. A page reads its script and module from beside
1668+ // it (dist-web's index.html, web-consumer.js, web-consumer.wasm); copied
1669+ // alone it loads nothing (2026.11.23.1 wrote dist/pack/index.html only).
16411670 if (!run.opts .outputDir .empty ())
16421671 for (auto & r : reported) {
16431672 const auto into = std::filesystem::absolute (run.opts .outputDir / r.filename (), ec).lexically_normal ();
16441673 // A provider may have written it there already (the okb is
16451674 // written beside the staged tree, in target/dist/): removing
16461675 // the destination would remove the product.
16471676 if (into == r.lexically_normal ()) continue ;
1648- std::filesystem::remove_all (into, ec);
1649- std::filesystem::copy (r, into, std::filesystem::copy_options::recursive, ec);
1650- if (ec) {
1651- mcpp::ui::error (about (run, m, std::format (" cannot write {}: {}" , into.string (), ec.message ())));
1677+ if (auto why = copy_distributable (r, intermediate, into.parent_path ()); !why.empty ()) {
1678+ mcpp::ui::error (about (run, m, why));
16521679 m.result .rc = 1 ;
16531680 break ;
16541681 }
0 commit comments