Skip to main content

bynk_project/
discovery.rs

1use std::collections::{HashMap, HashSet};
2use std::fs;
3use std::path::{Path, PathBuf};
4
5use bynk_syntax::ast::{
6    AdapterDecl, Case, Commons, CommonsItem, ConsumesDecl, ExportsDecl, SourceUnit, SuiteDecl,
7    TestTier, Trivia, UsesDecl,
8};
9use bynk_syntax::error::CompileError;
10use bynk_syntax::lexer;
11use bynk_syntax::parser;
12use bynk_syntax::span::Span;
13
14use crate::roots::{Roots, UnitKind};
15
16/// v0.118: a case's *effective* tier — its own `as <tier>`, else the suite
17/// default, else `unit`.
18pub fn case_effective_tier(case: &Case, suite: &SuiteDecl) -> TestTier {
19    case.tier.or(suite.tier).unwrap_or(TestTier::Unit)
20}
21
22/// v0.118: whether a suite's *effective* tier is `system` — the suite default
23/// is `system`, or any case opts up to `system`. Such a suite is emitted via
24/// the wired cross-Worker (`Integration`) machinery; otherwise it stays
25/// in-process (`Test`).
26pub fn suite_effective_tier_is_system(suite: &SuiteDecl) -> bool {
27    suite.tier == Some(TestTier::System)
28        || suite.cases.iter().any(|c| c.tier == Some(TestTier::System))
29}
30
31/// Read a source file from the overlay (keyed by canonicalised absolute
32/// path; falls back to the literal path so a not-yet-created overlay entry
33/// still matches). Every caller into this module now supplies a complete
34/// overlay — content-ownership track (#1086) slice 5 removed the disk-read
35/// fallback this used to have on a miss, so an incomplete overlay is a real
36/// `NotFound` error here, not a silent disk read.
37///
38/// Finding #55/#65: tries the literal path first, `canonicalize()` only on a
39/// miss — an in-memory/wasm project's synthetic overlay keys never exist on
40/// disk, so `canonicalize()` was a guaranteed-failing syscall on every read of
41/// every such file, for no benefit (the literal-path lookup below already
42/// finds the same entry).
43pub fn read_source(path: &Path, overlay: &HashMap<PathBuf, String>) -> std::io::Result<String> {
44    if let Some(text) = overlay.get(path) {
45        return Ok(text.clone());
46    }
47    let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
48    if let Some(text) = overlay.get(&canonical) {
49        return Ok(text.clone());
50    }
51    Err(std::io::Error::new(
52        std::io::ErrorKind::NotFound,
53        format!("no overlay entry for `{}`", path.display()),
54    ))
55}
56
57/// An adapter's `.binding.ts` module: overlay-first (an open, unsaved
58/// binding buffer), else a real disk read. Content-ownership track (#1086)
59/// scope note, found under slice 5's implementation: unlike a project's
60/// `.bynk` sources — enumerable ahead of time by extension, and this
61/// track's actual charter — a binding module's *path* is only known once
62/// its declaring adapter has been parsed (`adapter … { binding: "…" }`), so
63/// no discovery walk (`bynk-testkit`, `bynk-driver::discovery`) can
64/// pre-populate it into a sources map the way `.bynk` files are. Keeping a
65/// disk-read fallback here — the CLI's real production path has always
66/// worked exactly this way, `#1077`/`#1081` notwithstanding — is a
67/// deliberate, narrow carve-out, not a straggler.
68pub fn read_adapter_binding(
69    path: &Path,
70    overlay: &HashMap<PathBuf, String>,
71) -> std::io::Result<String> {
72    if let Some(text) = overlay.get(path) {
73        return Ok(text.clone());
74    }
75    let canonical = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
76    if let Some(text) = overlay.get(&canonical) {
77        return Ok(text.clone());
78    }
79    fs::read_to_string(path)
80}
81
82/// A parsed `.bynk` file: its source, AST, and the two path forms it needs.
83///
84/// Slice 0: `source_path` and `identity_path` are **different things**, and
85/// conflating them is what made a two-root project's file identity ambiguous.
86/// They coincide for a single-root project, which is why one field sufficed
87/// until `include` could hold two entries.
88///
89/// P4.0 (#1113, [DECISION B]): fields are crate-private now that `ParsedFile`
90/// lives in `bynk-project` — `bynk-emit`'s `symbols`/`validate` read them
91/// through the accessors below instead of the direct field pokes a
92/// same-crate `pub(crate)` allowed before the move.
93#[derive(Clone)]
94pub struct ParsedFile {
95    /// The path **relative to the `include` root that contains this file** —
96    /// the form unit validation requires. `src/todos.bynk` under the `src`
97    /// root is `todos.bynk`, which is what lets it declare `context todos`
98    /// ([`crate::paths::unit_path_matches`], via
99    /// [`crate::consistency::check_path_name_alignment`]). Prefixing this
100    /// would make every unit in every project fail alignment.
101    pub(crate) source_path: PathBuf,
102    /// Slice 0: the path **relative to the project root** — this file's
103    /// identity, unique across `include` roots. `src/todos.bynk` and
104    /// `tests/todos.bynk` share a `source_path` (`todos.bynk`) but differ
105    /// here. Everything that *keys* a file — the analysed snapshots, the
106    /// diagnostic attribution — uses this; nothing that *validates a unit's
107    /// name* may.
108    ///
109    /// Equal to `source_path` for a single-root project (`Roots::Single`
110    /// resolves to one tree with an empty prefix), so single-root behaviour
111    /// is unchanged by construction.
112    pub(crate) identity_path: PathBuf,
113    /// v0.72: the absolute path the compiler read this file from, used as the
114    /// source-map `sources` entry so an editor's breakpoint (set on the real
115    /// `.bynk` file) resolves to the same path the debugger loads. `None` for
116    /// toolchain-injected synthetic units, which have no on-disk source.
117    pub(crate) abs_path: Option<PathBuf>,
118    pub(crate) source: String,
119    pub(crate) unit: SourceUnit,
120    pub(crate) kind: UnitKind,
121    /// v0.17: true for toolchain-injected units (the `bynk` surface) — exempt
122    /// from the reserved-namespace and missing-binding checks.
123    pub(crate) synthetic: bool,
124}
125
126impl ParsedFile {
127    /// Construct directly — used by `bynk-emit`'s first-party synthetic-unit
128    /// injection (`firstparty_parsed`), which builds a `ParsedFile` for a
129    /// toolchain-supplied source (`bynk.bynk`, `bynk.cloudflare`, …) that
130    /// never went through [`parse_sources`]'s discovery-driven path.
131    pub fn synthetic(
132        identity_path: PathBuf,
133        source_path: PathBuf,
134        source: String,
135        unit: SourceUnit,
136        kind: UnitKind,
137    ) -> Self {
138        ParsedFile {
139            source_path,
140            identity_path,
141            abs_path: None,
142            source,
143            unit,
144            kind,
145            synthetic: true,
146        }
147    }
148
149    /// General constructor — `bynk-emit`'s own tests use this to build a
150    /// hand-rolled `ParsedFile` fixture (a specific `source_path`/
151    /// `identity_path` pair, a non-synthetic unit) that neither
152    /// [`Self::synthetic`] (forces `source_path == identity_path`,
153    /// `synthetic: true`) nor [`parse_sources`] (needs a real token stream)
154    /// fits.
155    #[allow(clippy::too_many_arguments)]
156    pub fn new(
157        source_path: PathBuf,
158        identity_path: PathBuf,
159        abs_path: Option<PathBuf>,
160        source: String,
161        unit: SourceUnit,
162        kind: UnitKind,
163        synthetic: bool,
164    ) -> Self {
165        ParsedFile {
166            source_path,
167            identity_path,
168            abs_path,
169            source,
170            unit,
171            kind,
172            synthetic,
173        }
174    }
175
176    /// The path **relative to the `include` root that contains this file**.
177    pub fn source_path(&self) -> PathBuf {
178        self.source_path.clone()
179    }
180
181    /// The path **relative to the project root** — this file's identity,
182    /// unique across `include` roots. See the field's own doc for why this
183    /// and [`Self::source_path`] must not be conflated.
184    pub fn identity_path(&self) -> PathBuf {
185        self.identity_path.clone()
186    }
187
188    /// The absolute path this file was read from, when it has one — `None`
189    /// for toolchain-injected synthetic units.
190    pub fn abs_path(&self) -> Option<PathBuf> {
191        self.abs_path.clone()
192    }
193
194    pub fn kind(&self) -> UnitKind {
195        self.kind
196    }
197
198    /// Override the discovered kind — `bynk-emit`'s own tests use this to
199    /// build a scenario's intermediate unit as a commons regardless of what
200    /// AST shape (`context_using`, …) constructed it, without needing a
201    /// second builder per kind.
202    pub fn set_kind(&mut self, kind: UnitKind) {
203        self.kind = kind;
204    }
205
206    /// True for toolchain-injected units (the `bynk` surface).
207    pub fn is_synthetic(&self) -> bool {
208        self.synthetic
209    }
210
211    pub fn unit(&self) -> &SourceUnit {
212        &self.unit
213    }
214
215    /// Mutable access to the parsed unit — `bynk-emit`'s
216    /// `normalize_service_defaults` (service `by`/`given` default injection)
217    /// is the one caller that rewrites a unit's items in place, ahead of
218    /// grouping/checking.
219    pub fn unit_mut(&mut self) -> &mut SourceUnit {
220        &mut self.unit
221    }
222
223    /// The raw source text this file was parsed from.
224    pub fn source(&self) -> &str {
225        &self.source
226    }
227
228    /// v0.72: the source-map `sources` entry for this file — the absolute path
229    /// the compiler read it from (forward slashes), so an editor breakpoint set
230    /// on the real `.bynk` resolves to the same path the debugger loads. A
231    /// project-relative name would resolve against the emitted `.ts`'s directory,
232    /// which is the wrong place. Synthetic units (no on-disk source) fall back to
233    /// their relative path.
234    pub fn map_source_name(&self) -> String {
235        self.abs_path
236            .as_deref()
237            .unwrap_or(self.source_path.as_path())
238            .to_string_lossy()
239            .replace('\\', "/")
240    }
241
242    pub fn items(&self) -> &Vec<CommonsItem> {
243        match &self.unit {
244            SourceUnit::Commons(c) => &c.items,
245            SourceUnit::Context(c) => &c.items,
246            SourceUnit::Adapter(a) => &a.items,
247            SourceUnit::Suite(_) => {
248                // Tests don't contribute CommonsItem items; the production
249                // pipeline never asks them to. Return a singleton empty vec.
250                static EMPTY: std::sync::OnceLock<Vec<CommonsItem>> = std::sync::OnceLock::new();
251                EMPTY.get_or_init(Vec::new)
252            }
253        }
254    }
255
256    pub fn uses(&self) -> &Vec<UsesDecl> {
257        match &self.unit {
258            SourceUnit::Commons(c) => &c.uses,
259            SourceUnit::Context(c) => &c.uses,
260            SourceUnit::Adapter(a) => &a.uses,
261            SourceUnit::Suite(t) => &t.uses,
262        }
263    }
264
265    pub fn consumes(&self) -> &[ConsumesDecl] {
266        match &self.unit {
267            SourceUnit::Commons(_) => &[],
268            SourceUnit::Context(c) => &c.consumes,
269            // v0.18: adapter-to-adapter capability dependencies (spec §4.5).
270            SourceUnit::Adapter(a) => &a.consumes,
271            // An integration test's participant edges are resolved separately
272            // (the harness root consumes every participant); it has no
273            // `consumes` of its own.
274            SourceUnit::Suite(_) => &[],
275        }
276    }
277
278    /// `exports` clauses, for the unit kinds that have them (contexts and
279    /// adapters). Empty for commons/tests.
280    pub fn exports(&self) -> &[ExportsDecl] {
281        match &self.unit {
282            SourceUnit::Context(c) => &c.exports,
283            SourceUnit::Adapter(a) => &a.exports,
284            _ => &[],
285        }
286    }
287
288    pub fn adapter(&self) -> Option<&AdapterDecl> {
289        match &self.unit {
290            SourceUnit::Adapter(a) => Some(a),
291            _ => None,
292        }
293    }
294
295    pub fn test(&self) -> Option<&SuiteDecl> {
296        match &self.unit {
297            SourceUnit::Suite(t) => Some(t),
298            _ => None,
299        }
300    }
301
302    /// v0.118: a suite whose *effective* tier is `system` is emitted through
303    /// the wired cross-Worker machinery (the retired standalone `integration`
304    /// path, now re-driven from tiers). Returns the underlying [`SuiteDecl`]
305    /// when this file is such a suite.
306    pub fn integration(&self) -> Option<&SuiteDecl> {
307        match &self.unit {
308            SourceUnit::Suite(t) if suite_effective_tier_is_system(t) => Some(t),
309            _ => None,
310        }
311    }
312
313    /// Build a synthetic Commons AST node carrying the given items, so the
314    /// existing resolver/checker pipeline can be driven uniformly.
315    pub fn as_synthetic_commons(&self, items: Vec<CommonsItem>) -> Commons {
316        let (name, uses, documentation, form, span) = match &self.unit {
317            SourceUnit::Commons(c) => (
318                c.name.clone(),
319                c.uses.clone(),
320                c.documentation.clone(),
321                c.form,
322                c.span,
323            ),
324            SourceUnit::Context(c) => (
325                c.name.clone(),
326                c.uses.clone(),
327                c.documentation.clone(),
328                c.form,
329                c.span,
330            ),
331            SourceUnit::Suite(t) => (
332                t.target.clone(),
333                t.uses.clone(),
334                t.documentation.clone(),
335                t.form,
336                t.span,
337            ),
338            SourceUnit::Adapter(a) => (
339                a.name.clone(),
340                a.uses.clone(),
341                a.documentation.clone(),
342                a.form,
343                a.span,
344            ),
345        };
346        Commons {
347            name,
348            items,
349            uses,
350            documentation,
351            form,
352            span,
353            trivia: Trivia::default(),
354            trailing_comments: Vec::new(),
355        }
356    }
357}
358
359/// Parse already-read source text into a [`ParsedFile`]. The read happens
360/// at the call site (v0.24): the pipeline owns the text for snapshots and
361/// per-file error attribution, and the overlay supplies unsaved buffers.
362/// Slice 0: `prefix` is this tree's project-root-relative `include` prefix
363/// (`src`, `tests`, …), empty for a single-root project. It builds each file's
364/// `identity_path`; `source_path` stays relative to `root` (the tree), which is
365/// what unit validation reads. See [`ParsedFile`].
366pub fn parse_sources(
367    root: &Path,
368    prefix: &Path,
369    path: &Path,
370    source: String,
371    next_expr_id: &mut u32,
372    next_file_id: &mut u32,
373) -> Result<(Vec<ParsedFile>, Vec<CompileError>), Vec<CompileError>> {
374    // T3.5 (R2.2): one `FileId` per file this project parse touches, allocated
375    // here (the same choke point `next_expr_id` uses) rather than by the
376    // caller, so every span the lexer stamps for this file carries a real,
377    // distinct file identity instead of `FileId::UNKNOWN`.
378    let file = bynk_syntax::span::FileId(*next_file_id);
379    *next_file_id += 1;
380    let tokens = lexer::tokenize_in(&source, file).map_err(|e| vec![e])?;
381    // v0.113: a file may declare more than one top-level unit — an *atomic*
382    // file holding `commons`/`context` alongside a `suite` (DECISION S). Each
383    // unit becomes its own `ParsedFile` sharing the file's source and path, so
384    // the downstream grouping partitions *declarations* by kind: the source
385    // units flow to the build, the suites to `bynkc test` only.
386    // ADR 0117: a warning-severity parse diagnostic (an orphan doc block)
387    // must not hard-fail discovery — the parsed units flow to the build and
388    // the warnings ride out to the caller's severity-aware sink.
389    // T3.4 (R2.4): `next_expr_id` continues one `ExprId` counter across every
390    // file `phase_parse` parses in this project, not just this one file — a
391    // multi-file commons later merges sibling files' methods into one
392    // `check_record` call (`collect_unit_methods`), and two independently
393    // zero-based files would otherwise collide on the same id in the same
394    // `expr_types` map. Caught live by finding #28's debug assertion on
395    // `bynkc/tests/fixtures/positive/64_full_time_commons` before this fix.
396    let (units, warnings) = parser::parse_units_with_warnings_from(&tokens, &source, next_expr_id)?;
397    let rel = path.strip_prefix(root).unwrap_or(path).to_path_buf();
398    // v0.72: store an *absolute* path — `path` is relative when the compiler
399    // was invoked with a relative input (`bynkc test .`), and a relative map
400    // `source` would resolve against the emitted `.ts`'s directory, not the
401    // real file. `std::path::absolute` resolves against cwd without touching
402    // the filesystem (so it works for not-yet-saved overlay buffers too).
403    let abs_path = std::path::absolute(path).ok();
404    let files = units
405        .into_iter()
406        .map(|unit| {
407            let kind = match &unit {
408                SourceUnit::Commons(_) => UnitKind::Commons,
409                SourceUnit::Context(_) => UnitKind::Context,
410                // v0.118: a suite whose effective tier is `system` is emitted
411                // through the wired cross-Worker machinery (classified as
412                // `Integration`); unit/integration-tier suites stay in-process.
413                SourceUnit::Suite(t) if suite_effective_tier_is_system(t) => UnitKind::Integration,
414                SourceUnit::Suite(_) => UnitKind::Test,
415                SourceUnit::Adapter(_) => UnitKind::Adapter,
416            };
417            ParsedFile {
418                abs_path: abs_path.clone(),
419                identity_path: prefix.join(&rel),
420                source_path: rel.clone(),
421                source: source.clone(),
422                unit,
423                kind,
424                synthetic: false,
425            }
426        })
427        .collect();
428    Ok((files, warnings))
429}
430
431pub fn discover_bynk_files(
432    root: &Path,
433    excludes: &[PathBuf],
434) -> Result<Vec<PathBuf>, CompileError> {
435    if !root.exists() {
436        return Err(CompileError::new(
437            "bynk.project.no_root",
438            Span::default(),
439            format!("project root does not exist: {}", root.display()),
440        ));
441    }
442    // v0.113: skip excluded subtrees (author `exclude` + the tool's own caches)
443    // and hidden directories, so an `include` root at the project root does not
444    // sweep up generated, vendored, or dot-directory `.bynk`.
445    let is_excluded = |dir: &Path| {
446        excludes.iter().any(|ex| dir == ex || dir.starts_with(ex))
447            || dir
448                .file_name()
449                .and_then(|n| n.to_str())
450                .is_some_and(|n| n.starts_with('.') && n != ".")
451    };
452    let mut out = Vec::new();
453    let mut stack = vec![root.to_path_buf()];
454    while let Some(dir) = stack.pop() {
455        let rd = match fs::read_dir(&dir) {
456            Ok(r) => r,
457            Err(e) => {
458                return Err(CompileError::new(
459                    "bynk.project.read_failed",
460                    Span::default(),
461                    format!("could not read directory `{}`: {e}", dir.display()),
462                ));
463            }
464        };
465        for entry in rd.flatten() {
466            let p = entry.path();
467            if p.is_dir() {
468                if !is_excluded(&p) {
469                    stack.push(p);
470                }
471            } else if p.extension().and_then(|e| e.to_str()) == Some("bynk") {
472                out.push(p);
473            }
474        }
475    }
476    out.sort();
477    Ok(out)
478}
479
480/// Slice A: the `.bynk` files these roots contain — the **same walk**
481/// `compile_project` performs, honouring `exclude` and the tool's own `out`/
482/// `node_modules` caches.
483///
484/// P4.2 (#1122, Decision B): moved here from `bynk-emit/src/project.rs` — its
485/// body called only `bynk-project`-local functions already, with no
486/// `bynk-emit`-specific state. `bynk-emit` re-exports it at its existing
487/// `bynk_emit::project::discover_project_files` path so `read_disk_sources`
488/// and `bynk-testkit` need no edit; `bynk-ide` calls this path directly.
489pub fn discover_project_files(roots: &Roots) -> Vec<PathBuf> {
490    let trees = roots.trees();
491    let excludes = roots.excludes();
492    let mut out = Vec::new();
493    for (root, _prefix) in &trees {
494        // Every tree past the first is optional — a project may simply have
495        // no such subtree (R3.9, #1113: every `include` entry is walked, not
496        // just the first two). `unwrap_or_default` already treats a missing
497        // root the same as "no files here" for every tree, first included —
498        // no need to `root.exists()` before calling `discover_bynk_files`
499        // (itself a `fs::read_dir`) just to decide whether to call it: that
500        // would cost a redundant `stat()` per tree for the same answer.
501        out.extend(discover_bynk_files(root, &excludes).unwrap_or_default());
502    }
503    out.sort();
504    out.dedup();
505    out
506}
507
508pub fn check_file_directory_conflicts(
509    root: &Path,
510    files: &[PathBuf],
511) -> Result<(), Vec<CompileError>> {
512    let mut errors: Vec<CompileError> = Vec::new();
513    let mut bynk_files: HashSet<PathBuf> = HashSet::new();
514    let mut dirs_with_bynk: HashSet<PathBuf> = HashSet::new();
515    for p in files {
516        let rel = p.strip_prefix(root).unwrap_or(p);
517        bynk_files.insert(rel.to_path_buf());
518        if let Some(parent) = rel.parent() {
519            dirs_with_bynk.insert(parent.to_path_buf());
520        }
521    }
522    for f in &bynk_files {
523        let stem = f.with_extension("");
524        if dirs_with_bynk.contains(&stem) {
525            errors.push(
526                CompileError::new(
527                    "bynk.project.file_and_directory",
528                    Span::default(),
529                    format!(
530                        "commons at `{}` is ambiguous: both `{}` and `{}/` exist with `.bynk` content",
531                        f.with_extension("").display(),
532                        f.display(),
533                        stem.display()
534                    ),
535                )
536                .with_note(
537                    "a commons can be a single `.bynk` file OR a directory of `.bynk` files, not both",
538                ),
539            );
540        }
541    }
542    if errors.is_empty() {
543        Ok(())
544    } else {
545        Err(errors)
546    }
547}
548
549#[cfg(test)]
550mod tests {
551    use super::*;
552
553    /// Finding #55/#65: a synthetic path that never exists on disk (the
554    /// in-memory/wasm case) must still resolve via the overlay's literal-path
555    /// entry — `canonicalize()` on such a path always fails, so the fix tries
556    /// the literal path first rather than paying for that failing syscall on
557    /// every read.
558    #[test]
559    fn read_source_finds_a_synthetic_overlay_path_that_does_not_exist_on_disk() {
560        let path = PathBuf::from("./__bynk_in_memory__/t.bynk");
561        let mut overlay = HashMap::new();
562        overlay.insert(path.clone(), "context t\n".to_string());
563        let got = read_source(&path, &overlay).expect("the overlay entry must be found");
564        assert_eq!(got, "context t\n");
565    }
566
567    /// Content-ownership track (#1086) slice 5: a real on-disk file with no
568    /// overlay entry must now error, never silently fall back to reading it
569    /// off disk — the disk-read fallback this test guards the absence of was
570    /// deleted in this slice; every caller supplies a complete overlay.
571    #[test]
572    fn read_source_errors_on_a_real_file_with_no_overlay_entry_rather_than_reading_disk() {
573        let dir = std::env::temp_dir().join(format!(
574            "bynk-emit-discovery-fallback-test-{}",
575            std::process::id()
576        ));
577        std::fs::create_dir_all(&dir).expect("create test dir");
578        let path = dir.join("t.bynk");
579        std::fs::write(&path, "context t\n").expect("write real file");
580        let got = read_source(&path, &HashMap::new());
581        std::fs::remove_dir_all(&dir).ok();
582        assert!(
583            got.is_err(),
584            "a real file with no overlay entry must not be silently read from disk"
585        );
586        assert_eq!(got.unwrap_err().kind(), std::io::ErrorKind::NotFound);
587    }
588}