pedant-core 0.23.0

Analysis engine for pedant: IR extraction, style checks, and capability detection
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
//! Manifest traversal that turns a repository root into a Go module project.
//!
//! The root `go.mod` is the sole `replace` and `exclude` authority. An admitted
//! module contributes its own `module` and `require` declarations and nothing
//! else, which is what a single-main-module build does. Only a requirement an
//! admitted module contributed, replaced by the root with an in-root local
//! directory, brings another manifest into the project.

use std::collections::{BTreeMap, VecDeque};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use super::error::GoProjectError;
use super::exclusion::GoExclusion;
use super::identity::{GoModuleId, ProjectAuthority, index_of, position};
use super::limits::GoResolutionLimits;
use super::manifest::{GoManifestDocument, ParsedExclusion, ParsedReplacement, ParsedRequirement};
use super::module::GoModule;
use super::paths;
use super::project::GoProject;
use super::replacement::{GoReplacement, GoReplacementTarget};
use super::requirement::{GoRequirement, GoRequirementResolution};
use crate::hash::digest_bytes;
use crate::observe::{self, Observation};
use crate::resolution::capacity::admits_one_more;

/// The file name a module manifest always has.
const MANIFEST: &str = "go.mod";

/// The one reason a repository root anchors no Go project.
const NO_ROOT_MANIFEST: &str = "the project root holds no readable go.mod";

/// One admitted module, before the project mints its identity.
struct LoadedModule {
    path: Arc<str>,
    directory: Arc<str>,
    manifest: Arc<str>,
    depth: u32,
    digest: [u8; 32],
}

/// One requirement, before the project mints the identities it names.
struct LoadedRequirement {
    module: u32,
    path: Box<str>,
    version: Box<str>,
    resolution: LoadedResolution,
}

/// What a requirement resolved to, keyed by project-local module index.
enum LoadedResolution {
    External,
    ReplacedModule { path: Arc<str>, version: Arc<str> },
    LocalModule(u32),
}

/// One root replacement, and whether an admitted requirement selected it.
struct ReplacementEntry {
    declaration: ParsedReplacement,
    applied: bool,
}

/// Everything one load retains while it walks the module graph.
struct Loader<'a> {
    root: &'a Path,
    limits: GoResolutionLimits,
    modules: Vec<LoadedModule>,
    resolved: Vec<LoadedRequirement>,
    replacements: Vec<ReplacementEntry>,
    exclusions: Box<[ParsedExclusion]>,
    directories: BTreeMap<PathBuf, u32>,
    declared: BTreeMap<Arc<str>, u32>,
    pending: VecDeque<(u32, Box<[ParsedRequirement]>)>,
    revision: Vec<u8>,
}

/// Read the main module at `root` and every local replacement it requires.
pub(super) fn load_project(
    root: &Path,
    limits: GoResolutionLimits,
) -> Result<GoProject, GoProjectError> {
    let canonical = paths::canonical_root(root)?;
    observe::record(Observation::ProjectLoad);
    let document = read_root_manifest(&canonical)?;
    Loader::new(&canonical, limits).run(document)
}

/// The main module's manifest, which a Go project cannot be loaded without.
fn read_root_manifest(root: &Path) -> Result<GoManifestDocument, GoProjectError> {
    match confined_manifest(root, root)? {
        Some(manifest) => GoManifestDocument::read(root, &manifest),
        None => Err(GoProjectError::InvalidRoot {
            path: paths::path_text(root),
            reason: Box::from(NO_ROOT_MANIFEST),
        }),
    }
}

/// The canonical `go.mod` one directory holds, confined to the repository root.
///
/// The manifest is resolved, not reconstructed. Repository-relative naming is
/// lexical and cannot see a symlink, so a `go.mod` linked out of the root would
/// otherwise be read and its tokens echoed back in a published refusal.
///
/// Only "no such file" is absence. A `go.mod` that exists and cannot be read is
/// reported by the read that fails on it, never reported as one the directory
/// does not have.
fn confined_manifest(root: &Path, directory: &Path) -> Result<Option<PathBuf>, GoProjectError> {
    Ok(paths::canonical_in_root(root, &directory.join(MANIFEST))?)
}

impl<'a> Loader<'a> {
    fn new(root: &'a Path, limits: GoResolutionLimits) -> Self {
        Self {
            root,
            limits,
            modules: Vec::new(),
            resolved: Vec::new(),
            replacements: Vec::new(),
            exclusions: Box::from([]),
            directories: BTreeMap::new(),
            declared: BTreeMap::new(),
            pending: VecDeque::new(),
            revision: Vec::new(),
        }
    }

    /// Admit the main module, then resolve requirements until none is pending.
    fn run(mut self, root_document: GoManifestDocument) -> Result<GoProject, GoProjectError> {
        self.exclusions = root_document.exclusions;
        self.replacements = root_document
            .replacements
            .into_vec()
            .into_iter()
            .map(|declaration| ReplacementEntry {
                declaration,
                applied: false,
            })
            .collect();
        let main = LoadedModule {
            path: Arc::clone(&root_document.module),
            directory: Arc::from(""),
            manifest: Arc::from(root_document.relative),
            depth: 0,
            digest: root_document.digest,
        };
        self.retain_module(self.root.to_path_buf(), main, root_document.requirements)?;
        while let Some((owner, requirements)) = self.pending.pop_front() {
            self.resolve_module_requirements(owner, requirements)?;
        }
        self.seal()
    }

    /// Resolve every requirement one admitted module declares.
    fn resolve_module_requirements(
        &mut self,
        owner: u32,
        requirements: Box<[ParsedRequirement]>,
    ) -> Result<(), GoProjectError> {
        for requirement in requirements.into_vec() {
            self.resolve_requirement(owner, requirement)?;
        }
        Ok(())
    }

    /// Resolve one requirement against the root manifest's directives.
    fn resolve_requirement(
        &mut self,
        owner: u32,
        requirement: ParsedRequirement,
    ) -> Result<(), GoProjectError> {
        self.check_not_excluded(&requirement)?;
        let resolution = self.resolve_target(owner, &requirement)?;
        self.resolved.push(LoadedRequirement {
            module: owner,
            path: requirement.path,
            version: requirement.version,
            resolution,
        });
        Ok(())
    }

    /// A root exclusion that matches an admitted requirement refuses the load.
    fn check_not_excluded(&self, requirement: &ParsedRequirement) -> Result<(), GoProjectError> {
        let excluded = self.exclusions.iter().any(|exclusion| {
            exclusion.path == requirement.path && exclusion.version == requirement.version
        });
        match excluded {
            false => Ok(()),
            true => Err(GoProjectError::ExcludedRequirement {
                module: requirement.path.clone(),
                version: requirement.version.clone(),
            }),
        }
    }

    /// What the applicable replacement, if any, resolves a requirement to.
    fn resolve_target(
        &mut self,
        owner: u32,
        requirement: &ParsedRequirement,
    ) -> Result<LoadedResolution, GoProjectError> {
        let Some(selected) = self.applicable_replacement(requirement) else {
            return Ok(LoadedResolution::External);
        };
        self.replacements[selected].applied = true;
        let directory = match &self.replacements[selected].declaration.target {
            GoReplacementTarget::Module { path, version } => {
                return Ok(LoadedResolution::ReplacedModule {
                    path: Arc::clone(path),
                    version: Arc::clone(version),
                });
            }
            GoReplacementTarget::LocalDirectory(directory) => directory.clone(),
        };
        self.admit_local(owner, (&directory, requirement))
            .map(LoadedResolution::LocalModule)
    }

    /// The replacement one requirement selects: an exact `(path, version)`
    /// entry outranks a path-only entry for the same path.
    fn applicable_replacement(&self, requirement: &ParsedRequirement) -> Option<usize> {
        let exact = self.replacements.iter().position(|entry| {
            entry.declaration.path == requirement.path
                && entry.declaration.version.as_deref() == Some(&*requirement.version)
        });
        exact.or_else(|| {
            self.replacements.iter().position(|entry| {
                entry.declaration.path == requirement.path && entry.declaration.version.is_none()
            })
        })
    }

    /// Admit the in-root local module a replacement names, or return the one
    /// already admitted from that canonical directory.
    fn admit_local(
        &mut self,
        owner: u32,
        replacement: (&str, &ParsedRequirement),
    ) -> Result<u32, GoProjectError> {
        let (_, requirement) = replacement;
        let canonical = self.canonical_target(replacement)?;
        if let Some(admitted) = self.directories.get(&canonical) {
            return Ok(*admitted);
        }
        let document = self.read_module_manifest(&canonical, replacement)?;
        let relative: Arc<str> = Arc::from(paths::relative_text(self.root, &canonical)?);
        self.check_declared_module(&document, (&relative, &requirement.path))?;
        let module = LoadedModule {
            path: Arc::clone(&document.module),
            directory: relative,
            manifest: Arc::from(document.relative),
            depth: self.depth_of(owner)?.saturating_add(1),
            digest: document.digest,
        };
        self.retain_module(canonical, module, document.requirements)
    }

    /// The canonical form of a replacement target that stays inside the root.
    fn canonical_target(
        &self,
        replacement: (&str, &ParsedRequirement),
    ) -> Result<PathBuf, GoProjectError> {
        let (directory, requirement) = replacement;
        match paths::canonical_in_root(self.root, &self.root.join(directory))? {
            Some(canonical) => Ok(canonical),
            None => Err(GoProjectError::MissingReplacementManifest {
                directory: directory.into(),
                module: requirement.path.clone(),
            }),
        }
    }

    /// The replacement target's manifest, distinguishing an absent one from an
    /// unreadable one.
    fn read_module_manifest(
        &self,
        canonical: &Path,
        replacement: (&str, &ParsedRequirement),
    ) -> Result<GoManifestDocument, GoProjectError> {
        let (directory, requirement) = replacement;
        match confined_manifest(self.root, canonical)? {
            Some(manifest) => GoManifestDocument::read(self.root, &manifest),
            None => Err(GoProjectError::MissingReplacementManifest {
                directory: directory.into(),
                module: requirement.path.clone(),
            }),
        }
    }

    /// The target must declare the required path, and no two directories may
    /// declare one module path.
    fn check_declared_module(
        &self,
        document: &GoManifestDocument,
        site: (&str, &str),
    ) -> Result<(), GoProjectError> {
        let (relative, expected) = site;
        if document.module.as_ref() != expected {
            return Err(GoProjectError::ReplacementModuleMismatch {
                directory: relative.into(),
                declared: document.module.as_ref().into(),
                expected: expected.into(),
            });
        }
        match self.declared.get(&document.module) {
            None => Ok(()),
            Some(admitted) => Err(GoProjectError::ConflictingLocalModules {
                module: document.module.as_ref().into(),
                first: self.directory_of(*admitted)?.into(),
                second: relative.into(),
            }),
        }
    }

    /// Check both load ceilings, then retain one module and queue its
    /// requirements.
    ///
    /// Every module enters the project here, so a ceiling checked in this one
    /// body dominates every insertion into the retained tables.
    fn retain_module(
        &mut self,
        canonical: PathBuf,
        module: LoadedModule,
        requirements: Box<[ParsedRequirement]>,
    ) -> Result<u32, GoProjectError> {
        check_manifest_capacity(self.modules.len(), self.limits)?;
        check_dependency_depth(module.depth, self.limits)?;
        let index = position(self.modules.len());
        self.revision.extend_from_slice(&module.digest);
        self.directories.insert(canonical, index);
        self.declared.insert(Arc::clone(&module.path), index);
        self.modules.push(module);
        self.pending.push_back((index, requirements));
        Ok(index)
    }

    /// The depth of an already-admitted module.
    ///
    /// Absence is reported rather than answered. A depth taken as zero would
    /// restart the replacement-depth count that `check_dependency_depth`
    /// bounds, so an unknown owner would widen the very ceiling it feeds.
    fn depth_of(&self, module: u32) -> Result<u32, GoProjectError> {
        admitted(&self.modules, module).map(|held| held.depth)
    }

    /// The repository-relative directory of an already-admitted module.
    ///
    /// Absence is reported rather than answered. An empty directory renders as
    /// the repository root in a `ConflictingLocalModules` refusal, which names
    /// a directory the conflict is not in.
    fn directory_of(&self, module: u32) -> Result<&str, GoProjectError> {
        admitted(&self.modules, module).map(|held| &*held.directory)
    }

    /// Mint identities and seal the project.
    fn seal(self) -> Result<GoProject, GoProjectError> {
        let authority =
            ProjectAuthority::new(root_fingerprint(self.root)?, digest_bytes(&self.revision));
        let mut modules = self
            .modules
            .into_iter()
            .enumerate()
            .map(|(index, module)| GoModule {
                id: GoModuleId::new(authority, position(index)),
                path: module.path,
                directory: module.directory,
                manifest: module.manifest,
                depth: module.depth,
            });
        let Some(main) = modules.next() else {
            return Err(GoProjectError::InvalidRoot {
                path: paths::path_text(self.root),
                reason: Box::from(NO_ROOT_MANIFEST),
            });
        };
        let required: Box<[GoModule]> = modules.collect();
        Ok(GoProject {
            root: self.root.into(),
            limits: self.limits,
            authority,
            main,
            required,
            requirements: self
                .resolved
                .into_iter()
                .map(|requirement| published_requirement(authority, requirement))
                .collect(),
            replacements: self
                .replacements
                .into_iter()
                .map(published_replacement)
                .collect(),
            exclusions: self
                .exclusions
                .into_vec()
                .into_iter()
                .map(|exclusion| GoExclusion {
                    path: exclusion.path,
                    version: exclusion.version,
                })
                .collect(),
        })
    }
}

/// One module a load already admitted, by its project-local index.
///
/// Absence is a refusal rather than an answer: every index reaching this lookup
/// is one `retain_module` returned, so a miss is the loader disagreeing with
/// itself, and both readers of it would otherwise resolve that disagreement
/// into a depth of zero or a directory that renders as the repository root.
fn admitted(modules: &[LoadedModule], module: u32) -> Result<&LoadedModule, GoProjectError> {
    modules
        .get(index_of(module))
        .ok_or(GoProjectError::MissingAdmittedModule { index: module })
}

/// The published form of one loaded requirement.
fn published_requirement(
    authority: ProjectAuthority,
    requirement: LoadedRequirement,
) -> GoRequirement {
    GoRequirement {
        module: GoModuleId::new(authority, requirement.module),
        path: requirement.path,
        version: requirement.version,
        resolution: match requirement.resolution {
            LoadedResolution::External => GoRequirementResolution::External,
            LoadedResolution::ReplacedModule { path, version } => {
                GoRequirementResolution::ReplacedModule { path, version }
            }
            LoadedResolution::LocalModule(index) => {
                GoRequirementResolution::LocalModule(GoModuleId::new(authority, index))
            }
        },
    }
}

/// The published form of one root replacement.
fn published_replacement(entry: ReplacementEntry) -> GoReplacement {
    GoReplacement {
        path: entry.declaration.path,
        version: entry.declaration.version,
        target: entry.declaration.target,
        applied: entry.applied,
    }
}

/// SHA-256 over the canonical root's text, which scopes every issued identity.
fn root_fingerprint(root: &Path) -> Result<[u8; 32], GoProjectError> {
    match root.to_str() {
        Some(text) => Ok(digest_bytes(text.as_bytes())),
        None => Err(GoProjectError::NonUtf8Path {
            path: paths::path_text(root),
        }),
    }
}

/// One more manifest must still fit under the configured ceiling.
fn check_manifest_capacity(held: usize, limits: GoResolutionLimits) -> Result<(), GoProjectError> {
    match admits_one_more(held, limits.max_module_manifests) {
        true => Ok(()),
        false => Err(GoProjectError::ManifestLimitExceeded {
            limit: limits.max_module_manifests,
        }),
    }
}

/// One module's replacement depth must sit under the configured ceiling.
fn check_dependency_depth(depth: u32, limits: GoResolutionLimits) -> Result<(), GoProjectError> {
    match depth > limits.max_dependency_depth {
        true => Err(GoProjectError::DependencyDepthLimitExceeded {
            limit: limits.max_dependency_depth,
        }),
        false => Ok(()),
    }
}