Skip to content

Commit 091794c

Browse files
Fixed export package-definitions not including cached modules
Signed-off-by: Pedro H C Francisco <[email protected]>
1 parent 6afffcb commit 091794c

File tree

11 files changed

+83
-46
lines changed

11 files changed

+83
-46
lines changed

compiler-core/src/build.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::ast::{
2020
CallArg, CustomType, DefinitionLocation, Pattern, TypeAst, TypedArg, TypedDefinition,
2121
TypedExpr, TypedFunction, TypedPattern, TypedStatement,
2222
};
23+
use crate::package_interface;
2324
use crate::type_::Type;
2425
use crate::{
2526
ast::{Definition, SrcSpan, TypedModule},
@@ -208,6 +209,7 @@ fn mode_includes_tests() {
208209
pub struct Package {
209210
pub config: PackageConfig,
210211
pub modules: Vec<Module>,
212+
pub cached_metadata: Vec<package_compiler::CacheMetadata>,
211213
}
212214

213215
impl Package {
@@ -225,7 +227,7 @@ impl Package {
225227
}
226228
}
227229

228-
#[derive(Debug)]
230+
#[derive(Debug, Clone)]
229231
pub struct Module {
230232
pub name: EcoString,
231233
pub code: EcoString,

compiler-core/src/build/module_loader.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ where
8484
}
8585
}
8686

87-
Ok(Input::Cached(self.cached(name, meta)))
87+
Ok(Input::Cached((self.cached(name, meta.clone()), meta)))
8888
}
8989

9090
/// Read the timestamp file from the artefact directory for the given

compiler-core/src/build/module_loader/tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
build::SourceFingerprint,
44
io::{memory::InMemoryFileSystem, FileSystemWriter},
55
line_numbers::LineNumbers,
6+
package_interface,
67
};
78
use std::time::Duration;
89

@@ -212,11 +213,13 @@ fn write_cache(
212213
) {
213214
let line_numbers = LineNumbers::new(source);
214215
let cache_metadata = CacheMetadata {
216+
name: "".into(),
215217
mtime: SystemTime::UNIX_EPOCH + Duration::from_secs(seconds),
216218
codegen_performed,
217219
dependencies: vec![],
218220
fingerprint: SourceFingerprint::new(source),
219221
line_numbers,
222+
interface: package_interface::ModuleInterface::default(),
220223
};
221224
let path = Utf8Path::new(path);
222225
fs.write_bytes(&path, &cache_metadata.to_binary()).unwrap();

compiler-core/src/build/package_compiler.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::analyse::{ModuleAnalyzerConstructor, TargetSupport};
22
use crate::line_numbers::{self, LineNumbers};
3+
use crate::package_interface::{self, ModuleInterface};
34
use crate::type_::PRELUDE_MODULE_NAME;
45
use crate::{
56
ast::{SrcSpan, TypedModule, UntypedModule},
@@ -22,6 +23,7 @@ use crate::{
2223
};
2324
use askama::Template;
2425
use ecow::EcoString;
26+
use std::borrow::BorrowMut;
2527
use std::collections::HashSet;
2628
use std::{collections::HashMap, fmt::write, time::SystemTime};
2729
use vec1::Vec1;
@@ -104,7 +106,7 @@ where
104106
stale_modules: &mut StaleTracker,
105107
incomplete_modules: &mut HashSet<EcoString>,
106108
telemetry: &dyn Telemetry,
107-
) -> Outcome<Vec<Module>, Error> {
109+
) -> Outcome<(Vec<Module>, Vec<CacheMetadata>), Error> {
108110
let span = tracing::info_span!("compile", package = %self.config.name.as_str());
109111
let _enter = span.enter();
110112

@@ -168,6 +170,8 @@ where
168170
}
169171
}
170172

173+
println!("{:?}", loaded.cached_metadata);
174+
171175
// Type check the modules that are new or have changed
172176
tracing::info!(count=%loaded.to_compile.len(), "analysing_modules");
173177
let outcome = analyse(
@@ -182,9 +186,12 @@ where
182186
incomplete_modules,
183187
);
184188

185-
let modules = match outcome {
189+
let mut modules = match outcome {
186190
Outcome::Ok(modules) => modules,
187-
Outcome::PartialFailure(_, _) | Outcome::TotalFailure(_) => return outcome,
191+
Outcome::PartialFailure(modules, err) => {
192+
return Outcome::PartialFailure((modules, loaded.cached_metadata), err)
193+
}
194+
Outcome::TotalFailure(err) => return Outcome::TotalFailure(err),
188195
};
189196

190197
tracing::debug!("performing_code_generation");
@@ -197,7 +204,7 @@ where
197204
return error.into();
198205
}
199206

200-
Outcome::Ok(modules)
207+
Outcome::Ok((modules, loaded.cached_metadata))
201208
}
202209

203210
fn compile_erlang_to_beam(&mut self, modules: &HashSet<Utf8PathBuf>) -> Result<(), Error> {
@@ -304,11 +311,13 @@ where
304311
let name = format!("{}.cache_meta", &module_name);
305312
let path = artefact_dir.join(name);
306313
let info = CacheMetadata {
314+
name: module.name.clone(),
307315
mtime: module.mtime,
308316
codegen_performed: self.perform_codegen,
309317
dependencies: module.dependencies.clone(),
310318
fingerprint: SourceFingerprint::new(&module.code),
311319
line_numbers: module.ast.type_info.line_numbers.clone(),
320+
interface: package_interface::ModuleInterface::from_module(module),
312321
};
313322
self.io.write_bytes(&path, &info.to_binary())?;
314323

@@ -594,28 +603,28 @@ pub(crate) fn module_name(package_path: &Utf8Path, full_module_path: &Utf8Path)
594603
#[derive(Debug)]
595604
pub(crate) enum Input {
596605
New(UncompiledModule),
597-
Cached(CachedModule),
606+
Cached((CachedModule, CacheMetadata)),
598607
}
599608

600609
impl Input {
601610
pub fn name(&self) -> &EcoString {
602611
match self {
603612
Input::New(m) => &m.name,
604-
Input::Cached(m) => &m.name,
613+
Input::Cached(m) => &m.0.name,
605614
}
606615
}
607616

608617
pub fn source_path(&self) -> &Utf8Path {
609618
match self {
610619
Input::New(m) => &m.path,
611-
Input::Cached(m) => &m.source_path,
620+
Input::Cached(m) => &m.0.source_path,
612621
}
613622
}
614623

615624
pub fn dependencies(&self) -> Vec<EcoString> {
616625
match self {
617626
Input::New(m) => m.dependencies.iter().map(|(n, _)| n.clone()).collect(),
618-
Input::Cached(m) => m.dependencies.iter().map(|(n, _)| n.clone()).collect(),
627+
Input::Cached(m) => m.0.dependencies.iter().map(|(n, _)| n.clone()).collect(),
619628
}
620629
}
621630

@@ -645,13 +654,15 @@ pub(crate) struct CachedModule {
645654
pub line_numbers: LineNumbers,
646655
}
647656

648-
#[derive(Debug, serde::Serialize, serde::Deserialize)]
649-
pub(crate) struct CacheMetadata {
657+
#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
658+
pub struct CacheMetadata {
659+
pub name: EcoString,
650660
pub mtime: SystemTime,
651661
pub codegen_performed: bool,
652662
pub dependencies: Vec<(EcoString, SrcSpan)>,
653663
pub fingerprint: SourceFingerprint,
654664
pub line_numbers: LineNumbers,
665+
pub interface: package_interface::ModuleInterface,
655666
}
656667

657668
impl CacheMetadata {
@@ -664,22 +675,24 @@ impl CacheMetadata {
664675
}
665676
}
666677

667-
#[derive(Debug, Default, PartialEq, Eq)]
678+
#[derive(Debug, Default)]
668679
pub(crate) struct Loaded {
669680
pub to_compile: Vec<UncompiledModule>,
670681
pub cached: Vec<type_::ModuleInterface>,
682+
pub cached_metadata: Vec<CacheMetadata>,
671683
}
672684

673685
impl Loaded {
674686
fn empty() -> Self {
675687
Self {
676688
to_compile: vec![],
677689
cached: vec![],
690+
cached_metadata: vec![],
678691
}
679692
}
680693
}
681694

682-
#[derive(Debug, PartialEq, Eq)]
695+
#[derive(Debug, PartialEq, Eq, Clone)]
683696
pub(crate) struct UncompiledModule {
684697
pub path: Utf8PathBuf,
685698
pub name: EcoString,

compiler-core/src/build/package_loader.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,20 @@ where
148148
// A cached module with dependencies that are stale must be
149149
// recompiled as the changes in the dependencies may have affect
150150
// the output, making the cache invalid.
151-
Input::Cached(info) if self.stale_modules.includes_any(&info.dependencies) => {
152-
tracing::debug!(module = %info.name, "module_to_be_compiled");
153-
self.stale_modules.add(info.name.clone());
154-
let module = self.load_and_parse(info)?;
151+
Input::Cached(info) if self.stale_modules.includes_any(&info.0.dependencies) => {
152+
tracing::debug!(module = %info.0.name, "module_to_be_compiled");
153+
self.stale_modules.add(info.0.name.clone());
154+
let module = self.load_and_parse(info.0)?;
155155
loaded.to_compile.push(module);
156156
}
157157

158158
// A cached module with no stale dependencies can be used as-is
159159
// and does not need to be recompiled.
160160
Input::Cached(info) => {
161-
tracing::debug!(module = %info.name, "module_to_load_from_cache");
162-
let module = self.load_cached_module(info)?;
161+
tracing::debug!(module = %info.0.name, "module_to_load_from_cache");
162+
let module = self.load_cached_module(info.0)?;
163163
loaded.cached.push(module);
164+
loaded.cached_metadata.push(info.1);
164165
}
165166
}
166167
}
@@ -324,7 +325,7 @@ where
324325
src: module.code.clone(),
325326
}
326327
}
327-
Input::Cached(cached_module) => {
328+
Input::Cached((cached_module, _)) => {
328329
let (_, location) = cached_module
329330
.dependencies
330331
.iter()

compiler-core/src/build/package_loader/tests.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::*;
55
use crate::{
66
build::SourceFingerprint,
77
io::{memory::InMemoryFileSystem, FileSystemWriter},
8-
line_numbers,
8+
line_numbers, package_interface,
99
parse::extra::ModuleExtra,
1010
warning::NullWarningEmitterIO,
1111
Warning,
@@ -39,11 +39,13 @@ fn write_cache(
3939
let line_numbers = line_numbers::LineNumbers::new(src);
4040
let mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(seconds);
4141
let cache_metadata = CacheMetadata {
42+
name: name.into(),
4243
mtime,
4344
codegen_performed: true,
4445
dependencies: deps,
4546
fingerprint: SourceFingerprint::new(src),
4647
line_numbers: line_numbers.clone(),
48+
interface: package_interface::ModuleInterface::default(),
4749
};
4850
let path = Utf8Path::new("/artefact").join(format!("{name}.cache_meta"));
4951
fs.write_bytes(&path, &cache_metadata.to_binary()).unwrap();
@@ -62,7 +64,7 @@ fn write_cache(
6264
warnings: vec![],
6365
minimum_required_version: Version::new(0, 1, 0),
6466
};
65-
let path = Utf8Path::new("/artefact").join(format!("{name}.cache"));
67+
let path: Utf8PathBuf = Utf8Path::new("/artefact").join(format!("{name}.cache"));
6668
fs.write_bytes(
6769
&path,
6870
&metadata::ModuleEncoder::new(&cache).encode().unwrap(),

compiler-core/src/build/project_compiler.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::{
2121
use ecow::EcoString;
2222
use hexpm::version::Version;
2323
use itertools::Itertools;
24-
use pubgrub::range::Range;
24+
use pubgrub::{package, range::Range};
2525
use std::{
2626
cmp,
2727
collections::{HashMap, HashSet},
@@ -208,7 +208,11 @@ where
208208
pub fn compile_root_package(&mut self) -> Outcome<Package, Error> {
209209
let config = self.config.clone();
210210
self.compile_gleam_package(&config, true, self.paths.root().to_path_buf())
211-
.map(|modules| Package { config, modules })
211+
.map(|(modules, cached_metadata)| Package {
212+
config,
213+
modules,
214+
cached_metadata,
215+
})
212216
}
213217

214218
/// Checks that version file found in the build directory matches the
@@ -288,7 +292,9 @@ where
288292
// longer need to have the package borrowed from self.packages.
289293
let package = self.packages.get(name).expect("Missing package").clone();
290294
let result = match usable_build_tools(&package)?.as_slice() {
291-
&[BuildTool::Gleam] => self.compile_gleam_dep_package(&package),
295+
&[BuildTool::Gleam] => self
296+
.compile_gleam_dep_package(&package)
297+
.map(|(modules, _)| modules),
292298
&[BuildTool::Rebar3] => self.compile_rebar3_dep_package(&package).map(|_| vec![]),
293299
&[BuildTool::Mix] => self.compile_mix_dep_package(&package).map(|_| vec![]),
294300
&[BuildTool::Mix, BuildTool::Rebar3] => self
@@ -489,7 +495,7 @@ where
489495
fn compile_gleam_dep_package(
490496
&mut self,
491497
package: &ManifestPackage,
492-
) -> Result<Vec<Module>, Error> {
498+
) -> Result<(Vec<Module>, Vec<package_compiler::CacheMetadata>), Error> {
493499
// TODO: Test
494500
let package_root = match &package.source {
495501
// If the path is relative it is relative to the root of the
@@ -520,7 +526,7 @@ where
520526
config: &PackageConfig,
521527
is_root: bool,
522528
root_path: Utf8PathBuf,
523-
) -> Outcome<Vec<Module>, Error> {
529+
) -> Outcome<(Vec<Module>, Vec<package_compiler::CacheMetadata>), Error> {
524530
let out_path =
525531
self.paths
526532
.build_directory_for_package(self.mode(), self.target(), &config.name);
@@ -590,14 +596,16 @@ where
590596
};
591597

592598
// Compile project to Erlang or JavaScript source code
593-
compiler.compile(
599+
let outcome = compiler.compile(
594600
&mut self.warnings,
595601
&mut self.importable_modules,
596602
&mut self.defined_modules,
597603
&mut self.stale_modules,
598604
&mut self.incomplete_modules,
599605
self.telemetry,
600-
)
606+
);
607+
608+
outcome
601609
}
602610
}
603611

compiler-core/src/docs/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn compile_with_markdown_pages(
6363
)
6464
.unwrap();
6565

66-
for module in &mut modules {
66+
for module in &mut modules.0 {
6767
module.attach_doc_and_module_comments();
6868
}
6969

@@ -79,7 +79,7 @@ fn compile_with_markdown_pages(
7979
super::generate_html(
8080
&paths,
8181
&config,
82-
&modules,
82+
&modules.0,
8383
&docs_pages,
8484
pages_fs,
8585
SystemTime::UNIX_EPOCH,

0 commit comments

Comments
 (0)