proa-cli 0.1.2

CLI for scaffolding, inspecting, formatting, and validating Proa web projects
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright Proa Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

//! End-to-end acceptance coverage for relocation-resolved SharedState
//! consumers.
//!
//! The positive fixture exercises descriptor compilation, exact relocation
//! linking, canonical graph lowering, semantic owner planning, final Rust
//! compilation, and the resulting executable. The state type and consumer are
//! both imported through ordinary Rust aliases so no path-token spelling can
//! become protocol authority.

use std::collections::{BTreeMap, BTreeSet};
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;

use proa_cli::rsjs::build::DescriptorCargoRequest;
use proa_cli::rsjs::pipeline::{
    run_linked_cargo, DescriptorRootSelector, LinkedCargoError, LinkedCargoOutput,
    LinkedCargoRequest,
};
use proa_cli::rsjs::semantic_link::{
    SemanticLinkError, StandaloneContextRequirement, StandaloneRootDisposition,
};
use rsjs_ir::abi::{
    AbiVersion, ComponentSemanticKey, DescriptorBody, EffectKind, InvocationId, OriginKind,
    ProducerVersion, SemanticHash, SharedStateRefId,
};
use rsjs_ir::build::ArtifactRole;
use tempfile::TempDir;

static FIXTURE_BUILD_LOCK: Mutex<()> = Mutex::new(());

const CONSUMER_FIXTURE: &str = r#"
#![allow(dead_code, unexpected_cfgs, unused_imports)]

use proa_core::{
    Ctx, DataLoader, NoLoader, WebContext, WebRenderSync, WriteBuf, WriteError,
};
use rsjs::rsjs;

mod state_definition {
    #[derive(Clone, Debug, rsjs::SharedState)]
    #[rsjs(type_key = "acceptance.shared-state-consumer")]
    pub struct WorkbenchState {
        pub count: u32,
        pub status: String,
    }

    pub mod public {
        pub use super::WorkbenchState as ExportedWorkbenchState;
    }
}

mod participant {
    use super::*;
    use crate::state_definition::public::ExportedWorkbenchState as StateAlias;
    use proa_core::WebRenderSync as RenderSync;

    pub struct SharedCounter {}

    #[rsjs(component, client)]
    impl<L: DataLoader, B: WriteBuf> RenderSync<L, B> for SharedCounter {
        fn render(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
            let workbench = rsjs::shared::<StateAlias>();
            let count = workbench.count();
            let status = workbench.status();
            let increment =
                rsjs::event_handler(|_: rsjs::Event| count.update(|value| value + 1_u32));
            let reset = rsjs::event_handler(|_: rsjs::Event| {
                count.set(0_u32);
                status.set("reset".to_string());
            });

            proa_macros::html_sync! {
                <section data-shared-counter>
                    <output data-count>{count.get()}</output>
                    <span data-status>{status.get()}</span>
                    <button type="button" on_click={increment()}>
                        "Increment"
                    </button>
                    <button type="button" on_click={reset()}>
                        "Reset"
                    </button>
                </section>
            }
            .render(cx)
        }
    }

    pub mod public {
        pub use super::SharedCounter as ExportedCounter;
    }
}

mod page {
    use super::*;
    use crate::participant::public::ExportedCounter as ImportedCounter;
    use crate::state_definition::public::ExportedWorkbenchState as ProviderStateAlias;
    use proa_core::WebRenderSync as RenderSync;
    use std::sync::atomic::{AtomicUsize, Ordering};

    pub struct WorkbenchPage;

    static PROVIDER_SEED_EVALUATIONS: AtomicUsize = AtomicUsize::new(0);

    fn provider_status() -> String {
        PROVIDER_SEED_EVALUATIONS.fetch_add(1, Ordering::SeqCst);
        "ready".to_owned()
    }

    pub fn provider_seed_evaluations() -> usize {
        PROVIDER_SEED_EVALUATIONS.load(Ordering::SeqCst)
    }

    #[rsjs(component)]
    impl<L: DataLoader, B: WriteBuf> RenderSync<L, B> for WorkbenchPage {
        fn render(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
            proa_macros::html_sync! {
                <main data-workbench-page>
                    {rsjs::SharedStateProvider {
                        state: ProviderStateAlias {
                            count: 5_u32,
                            status: provider_status(),
                        },
                        children: ImportedCounter {},
                    }}
                </main>
            }
            .render(cx)
        }
    }
}

#[cfg(not(rsjs_final))]
fn main() {}

#[cfg(rsjs_final)]
fn main() {
    let (seed_table, out) = {
        let mut cx = Ctx::<NoLoader, Vec<u8>>::with_buffer(Vec::new());
        page::WorkbenchPage
            .render(&mut cx)
            .expect("SharedState consumer page renders");
        let seed_table = cx
            .rsjs_shared_state_seed_table_json()
            .expect("a committed provider with a live consumer publishes one seed");
        (seed_table, cx.into_output())
    };
    let html = String::from_utf8(out).expect("rendered HTML is UTF-8");
    let runtime_schema_hash =
        <state_definition::WorkbenchState as rsjs::SharedState>::schema_hash();

    assert_eq!(
        page::provider_seed_evaluations(),
        1,
        "the final provider rewrite must evaluate the authored state expression exactly once"
    );
    assert_eq!(
        html.matches("<rsjs-island").count(),
        1,
        "the imported SharedState consumer must be the only browser owner: {html}"
    );
    assert!(
        html.contains("data-workbench-page")
            && html.contains("data-shared-counter")
            && html.contains("<output data-count")
            && html.contains(">5</output>")
            && html.contains("<span data-status")
            && html.contains(">ready</span>")
            && html.contains("data-rsjs-handler=")
            && html.contains("data-rsjs-bind="),
        "the consumer must SSR from the provider snapshot and retain its bindings: {html}"
    );
    assert!(
        html.contains("\"shared\":[{\"scope_id\":0")
            && html.contains("\"type_key\":\"acceptance.shared-state-consumer\"")
            && html.contains(&format!("\"schema_hash\":\"{runtime_schema_hash}\"")),
        "the adjacent owner metadata must bind to the exact provider scope and schema: {html}"
    );
    assert!(
        !html.contains("\"signals\":[5]")
            && !html.contains("\"signals\":[\"ready\"]"),
        "page-owned SharedState fields must not be duplicated as local signal seeds: {html}"
    );
    assert!(
        seed_table.contains("\"scope_id\":0")
            && seed_table.contains("\"type_key\":\"acceptance.shared-state-consumer\"")
            && seed_table.contains(&format!("\"schema_hash\":\"{runtime_schema_hash}\""))
            && seed_table.contains("\"count\":5")
            && seed_table.contains("\"status\":\"ready\""),
        "the response seed table must carry the provider state exactly once: {seed_table}"
    );

    let manifest = rsjs::manifest::manifest();
    let fragment = manifest
        .component_fragments
        .iter()
        .find(|fragment| fragment.struct_name == "SharedCounter")
        .expect("SharedCounter fragment");
    assert!(
        fragment
            .js_source
            .contains("acquire_shared_state(options.seedRoot ?? root, 0)")
            && fragment
                .js_source
                .contains(".field(\"count\")")
            && fragment
                .js_source
                .contains(".field(\"status\")")
            && fragment
                .js_source
                .contains("bind_delegated_events(root, \"click\""),
        "the fragment must acquire the adjacent provider scope and retain reads/writes:\n{}",
        fragment.js_source
    );
    assert!(
        !fragment.js_source.contains("read_signal_initials("),
        "SharedState fields must not become component-local signal seeds:\n{}",
        fragment.js_source
    );

    let rendered_owners = manifest
        .islands
        .iter()
        .filter(|island| {
            html.contains(&format!(
                "data-rsjs-island-root=\"{}\"",
                island.asset_id
            ))
        })
        .collect::<Vec<_>>();
    assert_eq!(
        rendered_owners.len(),
        1,
        "exactly one linked owner must activate for the rendered consumer"
    );
    assert!(
        rendered_owners[0]
            .component_fragment_imports
            .contains(&fragment.asset_id),
        "the rendered owner must import the SharedCounter fragment"
    );

    print!("{html}");
}
"#;

const CONFLICT_FIXTURE: &str = r#"
#![allow(dead_code, unexpected_cfgs, unused_imports)]

use proa_core::{DataLoader, WebContext, WebRenderSync, WriteBuf, WriteError};
use rsjs::rsjs;

mod first {
    use super::*;

    #[derive(Clone, Debug, rsjs::SharedState)]
    #[rsjs(type_key = "acceptance.shared-state-conflict")]
    pub struct State {
        pub value: u32,
    }

    pub struct Consumer {}

    #[rsjs(component, client)]
    impl<L: DataLoader, B: WriteBuf> WebRenderSync<L, B> for Consumer {
        fn render(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
            let state = rsjs::shared::<State>();
            let value = state.value();
            let clear =
                rsjs::event_handler(|_: rsjs::Event| value.set(0_u32));

            proa_macros::html_sync! {
                <button type="button" on_click={clear()}>{value.get()}</button>
            }
            .render(cx)
        }
    }
}

mod second {
    use super::*;

    #[derive(Clone, Debug, rsjs::SharedState)]
    #[rsjs(type_key = "acceptance.shared-state-conflict")]
    pub struct State {
        pub value: String,
    }

    pub struct Consumer {}

    #[rsjs(component, client)]
    impl<L: DataLoader, B: WriteBuf> WebRenderSync<L, B> for Consumer {
        fn render(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
            let state = rsjs::shared::<State>();
            let value = state.value();
            let replace =
                rsjs::event_handler(|_: rsjs::Event| value.set("changed".to_string()));

            proa_macros::html_sync! {
                <button type="button" on_click={replace()}>{value.get()}</button>
            }
            .render(cx)
        }
    }
}

pub struct ConflictPage;

impl<L: DataLoader, B: WriteBuf> WebRenderSync<L, B> for ConflictPage {
    fn render(self, cx: &mut WebContext<L, B>) -> Result<(), WriteError> {
        proa_macros::html_sync! {
            <main>
                {rsjs::SharedStateProvider {
                    state: first::State {
                        value: 1_u32,
                    },
                    children: first::Consumer {},
                }}
                {rsjs::SharedStateProvider {
                    state: second::State {
                        value: "second".to_owned(),
                    },
                    children: second::Consumer {},
                }}
            </main>
        }
        .render(cx)
    }
}

fn main() {}
"#;

#[test]
fn linked_shared_state_consumer_uses_exact_schema_and_activates_one_owner() {
    let _build_guard = FIXTURE_BUILD_LOCK
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let temp = write_fixture("rsjs_shared_state_consumer_fixture", CONSUMER_FIXTURE);
    let output = run_fixture(
        &temp,
        "rsjs_shared_state_consumer_fixture",
        "rsjs-shared-state-consumer",
    )
    .expect("a resolved SharedState consumer must link and compile");

    assert_transport_identity(&output);
    assert_canonical_consumer(&output);

    let executable = output
        .final_root_artifact
        .executable
        .as_ref()
        .expect("final SharedState consumer executable");
    let runtime = Command::new(executable).output().unwrap();
    assert!(
        runtime.status.success(),
        "{}",
        String::from_utf8_lossy(&runtime.stderr)
    );
    let html = String::from_utf8(runtime.stdout).unwrap();
    assert!(
        html.contains("data-shared-counter")
            && html.contains(">5</output>")
            && html.contains(">ready</span>")
            && html.contains("\"shared\":[{\"scope_id\":0"),
        "the final executable must preserve provider-to-consumer identity: {html}"
    );
}

#[test]
fn same_shared_state_type_key_with_different_exact_schemas_is_rejected() {
    let _build_guard = FIXTURE_BUILD_LOCK
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let temp = write_fixture("rsjs_shared_state_conflict_fixture", CONFLICT_FIXTURE);
    let error = run_fixture(
        &temp,
        "rsjs_shared_state_conflict_fixture",
        "rsjs-shared-state-conflict",
    )
    .expect_err("one public type_key cannot select two different wire schemas");

    let LinkedCargoError::SemanticLink(SemanticLinkError::SharedStateDeclarationConflict {
        type_key,
        first_schema,
        second_schema,
        ..
    }) = &error
    else {
        panic!("expected an exact linked SharedState schema conflict, got: {error:?}");
    };
    assert_eq!(type_key, "acceptance.shared-state-conflict");
    assert_ne!(first_schema, second_schema);
    let message = error.to_string();
    assert!(
        message.contains("acceptance.shared-state-conflict")
            && message.contains("incompatible schema ABIs")
            && message.contains("before any owner activates"),
        "the error must explain the page-wide schema identity contract: {message}"
    );
}

fn assert_transport_identity(output: &LinkedCargoOutput) {
    let component = output
        .transport
        .components
        .iter()
        .find(|component| component.identity.item == "SharedCounter")
        .expect("transport SharedCounter definition");
    assert_eq!(
        component.identity.module_path,
        ["participant"],
        "the definition identity must be the defining module, not an import alias"
    );
    assert_eq!(
        component.shared_state_acquisitions.len(),
        1,
        "the reachable shared::<Alias>() call owns one exact acquisition"
    );
    let claim = &component.shared_state_acquisitions[0];
    assert_eq!(claim.store_id, 0);

    let acquisition = output
        .transport
        .shared_state_acquisitions
        .iter()
        .find(|acquisition| acquisition.source_edge == claim.source_edge)
        .expect("global exact acquisition record");
    assert_eq!(
        acquisition.target, claim.target,
        "the component claim and relocation-resolved global record must select the same definition"
    );
    let definition = output
        .transport
        .shared_state_definitions
        .iter()
        .find(|definition| definition.definition == claim.target)
        .expect("relocation-selected SharedState definition");
    assert_eq!(definition.type_key, "acceptance.shared-state-consumer");
    assert!(
        !definition.schema_abi.is_empty(),
        "the selected definition must carry complete canonical schema bytes"
    );
}

fn assert_canonical_consumer(output: &LinkedCargoOutput) {
    let plan = &output.semantic;
    let consumer_key = component_key(
        "rsjs_shared_state_consumer_fixture",
        "SharedCounter",
        ["participant"],
    );
    let component = plan
        .components
        .iter()
        .find(|component| component.semantic_key == consumer_key)
        .expect("canonical SharedCounter");
    let DescriptorBody::Component(body) = &component.descriptor.body else {
        panic!("SharedCounter descriptor must be a component");
    };
    assert_eq!(
        body.shared_state.len(),
        1,
        "one acquired store must become one canonical SharedState reference"
    );
    let reference = body
        .shared_state
        .get(&SharedStateRefId::new(0))
        .expect("dense SharedState reference zero");
    assert_eq!(reference.type_key, "acceptance.shared-state-consumer");
    assert_eq!(
        body.origins.get(&reference.origin),
        Some(&OriginKind::SharedState),
        "the relocation-selected acquisition is the canonical browser origin"
    );
    assert!(
        body.effects
            .values()
            .any(|effect| *effect == EffectKind::SharedStateRead),
        "rendered field values must lower to a SharedState read effect"
    );
    assert!(
        body.effects
            .values()
            .any(|effect| *effect == EffectKind::SharedStateWrite),
        "bound set/update handlers must lower to a SharedState write effect"
    );

    let transport_component = output
        .transport
        .components
        .iter()
        .find(|component| component.identity.item == "SharedCounter")
        .unwrap();
    let target = transport_component.shared_state_acquisitions[0].target;
    let definition = output
        .transport
        .shared_state_definitions
        .iter()
        .find(|definition| definition.definition == target)
        .unwrap();
    assert_eq!(
        reference.schema_abi,
        SemanticHash::from_bytes(definition.schema_abi_hash),
        "canonical identity must use the transport-domain hash of the exact \
         relocation-selected schema-ABI bytes"
    );

    let consumer_sites = plan
        .sites
        .iter()
        .filter(|site| site.component == consumer_key)
        .collect::<Vec<_>>();
    assert!(
        !consumer_sites.is_empty() && consumer_sites.iter().all(|site| site.parent.is_some()),
        "the provider-dependent consumer must exist only at admitted lexical sites"
    );
    let root_decision = plan
        .root_decisions
        .iter()
        .find(|decision| decision.component == consumer_key)
        .expect("standalone-root decision for SharedCounter");
    assert!(
        matches!(
            &root_decision.disposition,
            StandaloneRootDisposition::ContextRequired { missing }
                if matches!(
                    missing.as_slice(),
                    [StandaloneContextRequirement::SharedState(requirement)]
                        if requirement.reference == SharedStateRefId::new(0)
                            && requirement.type_key == "acceptance.shared-state-consumer"
                )
        ),
        "the standalone consumer must be withheld with its exact missing provider requirement"
    );
    let expected_candidates = consumer_sites
        .iter()
        .map(|site| site.candidate)
        .collect::<BTreeSet<_>>();
    assert!(
        expected_candidates
            .iter()
            .all(|candidate| plan.owners.candidate_to_owner.contains_key(candidate)),
        "every active SharedCounter occurrence must belong to a linked owner"
    );
    let linked_schema = plan
        .shared_state
        .iter()
        .find(|schema| schema.type_key == "acceptance.shared-state-consumer")
        .expect("linked page-level SharedState schema");
    assert_eq!(linked_schema.schema_abi, reference.schema_abi);
    assert_eq!(
        linked_schema.consumers.len(),
        expected_candidates.len(),
        "every instantiated SharedCounter acquisition contributes one schema consumer"
    );
    let actual_candidates = linked_schema
        .consumers
        .iter()
        .map(|consumer| consumer.component)
        .collect::<BTreeSet<_>>();
    assert_eq!(
        actual_candidates, expected_candidates,
        "schema consumers must name the exact instantiated SharedCounter sites"
    );
    assert_eq!(
        plan.shared_state_providers.len(),
        1,
        "the admitted page contains one exact lexical provider occurrence"
    );
    assert_eq!(
        plan.shared_state_requirements.len(),
        expected_candidates.len(),
        "every admitted consumer occurrence must resolve one exact provider requirement"
    );
    let provider = &plan.shared_state_providers[0];
    assert_eq!(provider.type_key, "acceptance.shared-state-consumer");
    assert!(
        plan.shared_state_requirements.iter().all(|requirement| {
            expected_candidates.contains(&requirement.consumer)
                && requirement.reference == SharedStateRefId::new(0)
                && requirement.provider_host == provider.host
                && requirement.provider_site == provider.provider_site
                && requirement.type_key == provider.type_key
                && requirement.schema_abi == provider.schema_abi
        }),
        "linked acquisitions must target the exact provider occurrence with no nearest-key fallback"
    );
    assert!(
        linked_schema.consumers.iter().all(|consumer| {
            consumer.reference == SharedStateRefId::new(0)
                && consumer.origin == reference.origin
                && plan
                    .owners
                    .candidate_to_owner
                    .get(&consumer.component)
                    .is_some_and(|owner| {
                        plan.owners
                            .owners
                            .iter()
                            .any(|partition| partition.id == *owner)
                    })
        }),
        "each schema consumer must retain the exact local reference/origin and a published owner"
    );
}

fn component_key<I>(package: &str, name: &str, module_path: I) -> ComponentSemanticKey
where
    I: IntoIterator,
    I::Item: Into<String>,
{
    ComponentSemanticKey::v1(package, package, module_path, name).unwrap()
}

fn write_fixture(package: &str, source: &str) -> TempDir {
    let temp = TempDir::new().unwrap();
    fs::create_dir_all(temp.path().join("src")).unwrap();
    let proa_root = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap();
    fs::write(
        temp.path().join("Cargo.toml"),
        format!(
            "[package]\n\
             name = {package:?}\n\
             version = \"0.0.0\"\n\
             edition = \"2021\"\n\n\
             [workspace]\n\n\
             [dependencies]\n\
             proa_core = {{ path = {:?} }}\n\
             proa_macros = {{ path = {:?} }}\n\
             rsjs = {{ path = {:?} }}\n",
            proa_root.join("proa_core"),
            proa_root.join("proa_macros"),
            proa_root.join("rsjs"),
        ),
    )
    .unwrap();
    fs::write(temp.path().join("src/main.rs"), source).unwrap();
    temp
}

fn run_fixture(
    temp: &TempDir,
    package: &str,
    invocation: &str,
) -> Result<LinkedCargoOutput, LinkedCargoError> {
    let cargo = std::env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo"));
    let environment = BTreeMap::new();
    let package_id = fixture_package_id(temp.path(), &cargo, &environment);

    run_linked_cargo(&LinkedCargoRequest {
        descriptor: DescriptorCargoRequest {
            cargo,
            wrapper: PathBuf::from(env!("CARGO_BIN_EXE_cargo-proa")),
            working_directory: temp.path().to_path_buf(),
            staging_directory: temp.path().join("proa-generated/descriptor-staging"),
            target_directory: temp.path().join("proa-generated/descriptor-target"),
            source_exclusion_directory: temp.path().join("proa-generated"),
            cargo_args: vec!["build".into(), "--offline".into()],
            environment: environment.clone(),
        },
        root: DescriptorRootSelector {
            package_id,
            target_name: package.to_string(),
            target_kinds: BTreeSet::from(["bin".to_string()]),
            role: ArtifactRole::RootExecutable,
            artifact_path: None,
        },
        invocation: InvocationId::derive(invocation),
        producer: ProducerVersion::new(0, 2, 0),
        runtime_protocol: AbiVersion::new(1, 0),
        plan_directory: temp.path().join("proa-generated/plans"),
        final_target_root: temp.path().join("proa-generated/final-target"),
        final_cargo_args: vec!["build".into(), "--offline".into()],
        final_environment: environment,
    })
}

fn fixture_package_id(
    fixture: &Path,
    cargo: &OsString,
    environment: &BTreeMap<OsString, OsString>,
) -> String {
    let metadata = Command::new(cargo)
        .current_dir(fixture)
        .envs(environment)
        .args(["metadata", "--format-version=1", "--no-deps", "--offline"])
        .output()
        .unwrap();
    assert!(
        metadata.status.success(),
        "{}",
        String::from_utf8_lossy(&metadata.stderr)
    );
    let metadata: serde_json::Value = serde_json::from_slice(&metadata.stdout).unwrap();
    metadata["packages"][0]["id"].as_str().unwrap().to_string()
}