Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed export package-definitions not including cached modules #3669

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions compiler-core/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fn main() {
// capnpc::CompilerCommand::new()
// .file("schema.capnp")
// .output_path("generated/")
// .run()
// .expect("compiling schema.capnp");
capnpc::CompilerCommand::new()
.file("schema.capnp")
.output_path("generated/")
.run()
.expect("compiling schema.capnp");
}
468 changes: 409 additions & 59 deletions compiler-core/generated/schema_capnp.rs

Large diffs are not rendered by default.

33 changes: 23 additions & 10 deletions compiler-core/schema.capnp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ struct Option(Value) {

struct Module {
name @0 :Text;
types @1 :List(Property(TypeConstructor));
values @2 :List(Property(ValueConstructor));
accessors @3 :List(Property(AccessorsMap));
package @4 :Text;
typesConstructors @5 :List(Property(TypesVariantConstructors));
lineNumbers @6 :LineNumbers;
srcPath @7 :Text;
isInternal @8 :Bool;
requiredVersion @9 :Version;
documentation @1 :List(Text);
types @2 :List(Property(TypeConstructor));
typeAliases @3 :List(Property(TypeAliasConstructor));
values @4 :List(Property(ValueConstructor));
accessors @5 :List(Property(AccessorsMap));
package @6 :Text;
typesConstructors @7 :List(Property(TypesVariantConstructors));
lineNumbers @8 :LineNumbers;
srcPath @9 :Text;
isInternal @10 :Bool;
requiredVersion @11 :Version;
}

struct Version {
Expand All @@ -49,7 +51,8 @@ struct TypeValueConstructor {
}

struct TypeValueConstructorParameter {
type @0 :Type;
label @0 :Text;
type @1 :Type;
}

struct TypeConstructor {
Expand All @@ -65,6 +68,16 @@ struct TypeConstructor {
documentation @6 :Text;
}

struct TypeAliasConstructor {
publicity @0 :Publicity;
module @1 :Text;
origin @2 :SrcSpan;
type @3 :Type;
arity @4 :UInt16;
deprecated @5 :Text;
documentation @6 :Text;
}

struct AccessorsMap {
type @0 :Type;
accessors @1 :List(Property(RecordAccessor));
Expand Down
38 changes: 31 additions & 7 deletions compiler-core/src/analyse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use crate::{
hydrator::Hydrator,
prelude::*,
AccessorsMap, Deprecation, ModuleInterface, PatternConstructor, RecordAccessor, Type,
TypeConstructor, TypeValueConstructor, TypeValueConstructorField, TypeVariantConstructors,
ValueConstructor, ValueConstructorVariant, Warning,
TypeAliasConstructor, TypeConstructor, TypeValueConstructor, TypeValueConstructorField,
TypeVariantConstructors, ValueConstructor, ValueConstructorVariant, Warning,
},
uid::UniqueIdGenerator,
warning::TypeWarningEmitter,
Expand Down Expand Up @@ -296,6 +296,7 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
let Environment {
module_types: types,
module_types_constructors: types_constructors,
module_type_aliases: type_aliases,
module_values: values,
accessors,
names: type_names,
Expand All @@ -317,14 +318,21 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
self.warnings.emit(warning.clone());
}

// println!(
// "Generated module {} has documentation size {}",
// self.module_name,
// documentation.len()
// );
let module = ast::Module {
documentation,
documentation: documentation.clone(),
name: self.module_name.clone(),
definitions: typed_statements,
type_info: ModuleInterface {
name: self.module_name,
documentation,
types,
types_value_constructors: types_constructors,
type_aliases,
values,
accessors,
origin: self.origin,
Expand Down Expand Up @@ -965,7 +973,10 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
}
};

fields.push(TypeValueConstructorField { type_: t.clone() });
fields.push(TypeValueConstructorField {
label: label.clone().map_or(None, |(_, label)| Some(label.clone())),
type_: t.clone(),
});

// Register the type for this parameter
args_types.push(t);
Expand Down Expand Up @@ -1177,7 +1188,7 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
// in some fashion.
let mut hydrator = Hydrator::new();
let parameters = self.make_type_vars(args, &mut hydrator, environment);
let tryblock = || {
let mut tryblock = || {
hydrator.disallow_new_type_variables();
let type_ = hydrator.type_from_ast(resolved_type, environment, &mut self.problems)?;

Expand All @@ -1191,14 +1202,27 @@ impl<'a, A> ModuleAnalyzer<'a, A> {
TypeConstructor {
origin: *location,
module: self.module_name.clone(),
parameters,
type_,
parameters: parameters.clone(),
type_: type_.clone(),
deprecation: deprecation.clone(),
publicity: *publicity,
documentation: documentation.as_ref().map(|(_, doc)| doc.clone()),
},
)?;

environment.insert_type_alias(
name.clone(),
TypeAliasConstructor {
origin: *location,
module: self.module_name.clone(),
type_: type_.as_ref().clone(),
arity: parameters.len(),
publicity: *publicity,
deprecation: deprecation.clone(),
documentation: documentation.as_ref().map(|(_, doc)| doc.clone()),
},
)?;

if let Some(name) = hydrator.unused_type_variables().next() {
return Err(Error::UnusedTypeAliasParameter {
location: *location,
Expand Down
11 changes: 10 additions & 1 deletion compiler-core/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::ast::{
CallArg, CustomType, DefinitionLocation, Pattern, TypeAst, TypedArg, TypedDefinition,
TypedExpr, TypedFunction, TypedPattern, TypedStatement,
};
use crate::package_interface;
use crate::type_::Type;
use crate::{
ast::{Definition, SrcSpan, TypedModule},
Expand Down Expand Up @@ -208,6 +209,7 @@ fn mode_includes_tests() {
pub struct Package {
pub config: PackageConfig,
pub modules: Vec<Module>,
pub cached_modules: Vec<type_::ModuleInterface>,
}

impl Package {
Expand All @@ -225,7 +227,7 @@ impl Package {
}
}

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Module {
pub name: EcoString,
pub code: EcoString,
Expand Down Expand Up @@ -261,6 +263,13 @@ impl Module {
.map(|span| Comment::from((span, self.code.as_str())).content.into())
.collect();

self.ast.type_info.documentation = self.ast.documentation.clone();
// println!(
// "Module {} has documentation of length {}",
// self.name,
// self.ast.documentation.len()
// );

// Order statements to avoid misassociating doc comments after the
// order has changed during compilation.
let mut statements: Vec<_> = self.ast.definitions.iter_mut().collect();
Expand Down
1 change: 1 addition & 0 deletions compiler-core/src/build/module_loader/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
build::SourceFingerprint,
io::{memory::InMemoryFileSystem, FileSystemWriter},
line_numbers::LineNumbers,
package_interface,
};
use std::time::Duration;

Expand Down
23 changes: 14 additions & 9 deletions compiler-core/src/build/package_compiler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::analyse::{ModuleAnalyzerConstructor, TargetSupport};
use crate::line_numbers::{self, LineNumbers};
use crate::package_interface::{self, ModuleInterface};
use crate::type_::PRELUDE_MODULE_NAME;
use crate::{
ast::{SrcSpan, TypedModule, UntypedModule},
Expand All @@ -22,6 +23,7 @@ use crate::{
};
use askama::Template;
use ecow::EcoString;
use std::borrow::BorrowMut;
use std::collections::HashSet;
use std::{collections::HashMap, fmt::write, time::SystemTime};
use vec1::Vec1;
Expand Down Expand Up @@ -104,7 +106,7 @@ where
stale_modules: &mut StaleTracker,
incomplete_modules: &mut HashSet<EcoString>,
telemetry: &dyn Telemetry,
) -> Outcome<Vec<Module>, Error> {
) -> Outcome<(Vec<Module>, Vec<type_::ModuleInterface>), Error> {
let span = tracing::info_span!("compile", package = %self.config.name.as_str());
let _enter = span.enter();

Expand Down Expand Up @@ -146,7 +148,7 @@ where
};

// Load the cached modules that have previously been compiled
for module in loaded.cached.into_iter() {
for module in loaded.cached.clone().into_iter() {
// Emit any cached warnings.
// Note that `self.cached_warnings` is set to `Ignore` (such as for
// dependency packages) then this field will not be populated.
Expand Down Expand Up @@ -182,9 +184,12 @@ where
incomplete_modules,
);

let modules = match outcome {
let mut modules = match outcome {
Outcome::Ok(modules) => modules,
Outcome::PartialFailure(_, _) | Outcome::TotalFailure(_) => return outcome,
Outcome::PartialFailure(modules, err) => {
return Outcome::PartialFailure((modules, loaded.cached), err)
}
Outcome::TotalFailure(err) => return Outcome::TotalFailure(err),
};

tracing::debug!("performing_code_generation");
Expand All @@ -196,8 +201,7 @@ where
if let Err(error) = self.encode_and_write_metadata(&modules) {
return error.into();
}

Outcome::Ok(modules)
Outcome::Ok((modules, loaded.cached))
}

fn compile_erlang_to_beam(&mut self, modules: &HashSet<Utf8PathBuf>) -> Result<(), Error> {
Expand Down Expand Up @@ -279,7 +283,7 @@ where
Ok(())
}

fn encode_and_write_metadata(&mut self, modules: &[Module]) -> Result<()> {
fn encode_and_write_metadata(&mut self, modules: &Vec<Module>) -> Result<()> {
if !self.write_metadata {
tracing::debug!("package_metadata_writing_disabled");
return Ok(());
Expand All @@ -291,7 +295,8 @@ where
let artefact_dir = self.out.join(paths::ARTEFACT_DIRECTORY_NAME);

tracing::debug!("writing_module_caches");
for module in modules {
for mut module in modules.clone() {
module.attach_doc_and_module_comments();
let module_name = module.name.replace("/", "@");

// Write metadata file
Expand Down Expand Up @@ -646,7 +651,7 @@ pub(crate) struct CachedModule {
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub(crate) struct CacheMetadata {
pub struct CacheMetadata {
pub mtime: SystemTime,
pub codegen_performed: bool,
pub dependencies: Vec<(EcoString, SrcSpan)>,
Expand Down
6 changes: 6 additions & 0 deletions compiler-core/src/build/package_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ where
let bytes = self.io.read_bytes(&path)?;
let mut module = metadata::ModuleDecoder::new(self.ids.clone()).read(bytes.as_slice())?;

// println!(
// "Loaded cached module {} has {} types",
// module.name,
// module.types.len()
// );

// Load warnings
if self.cached_warnings.should_use() {
let path = dir.join(name.as_ref()).with_extension("cache_warnings");
Expand Down
2 changes: 2 additions & 0 deletions compiler-core/src/build/package_loader/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ fn write_cache(
name: name.into(),
origin: Origin::Src,
package: "my_package".into(),
documentation: vec![],
types: Default::default(),
types_value_constructors: Default::default(),
type_aliases: Default::default(),
values: Default::default(),
accessors: Default::default(),
line_numbers: line_numbers.clone(),
Expand Down
12 changes: 8 additions & 4 deletions compiler-core/src/build/project_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ where
pub fn compile_root_package(&mut self) -> Outcome<Package, Error> {
let config = self.config.clone();
self.compile_gleam_package(&config, true, self.paths.root().to_path_buf())
.map(|modules| Package { config, modules })
.map(|(modules, cachedModules)| Package {
config,
modules,
cached_modules: cachedModules,
})
}

/// Checks that version file found in the build directory matches the
Expand Down Expand Up @@ -288,7 +292,7 @@ where
// longer need to have the package borrowed from self.packages.
let package = self.packages.get(name).expect("Missing package").clone();
let result = match usable_build_tools(&package)?.as_slice() {
&[BuildTool::Gleam] => self.compile_gleam_dep_package(&package),
&[BuildTool::Gleam] => self.compile_gleam_dep_package(&package).map(|_| vec![]),
&[BuildTool::Rebar3] => self.compile_rebar3_dep_package(&package).map(|_| vec![]),
&[BuildTool::Mix] => self.compile_mix_dep_package(&package).map(|_| vec![]),
&[BuildTool::Mix, BuildTool::Rebar3] => self
Expand Down Expand Up @@ -489,7 +493,7 @@ where
fn compile_gleam_dep_package(
&mut self,
package: &ManifestPackage,
) -> Result<Vec<Module>, Error> {
) -> Result<(Vec<Module>, Vec<type_::ModuleInterface>), Error> {
// TODO: Test
let package_root = match &package.source {
// If the path is relative it is relative to the root of the
Expand Down Expand Up @@ -520,7 +524,7 @@ where
config: &PackageConfig,
is_root: bool,
root_path: Utf8PathBuf,
) -> Outcome<Vec<Module>, Error> {
) -> Outcome<(Vec<Module>, Vec<type_::ModuleInterface>), Error> {
let out_path =
self.paths
.build_directory_for_package(self.mode(), self.target(), &config.name);
Expand Down
1 change: 1 addition & 0 deletions compiler-core/src/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ pub fn generate_html<IO: FileSystemReader>(
}

pub fn generate_json_package_interface(path: Utf8PathBuf, package: &Package) -> OutputFile {
// println!("Generating Package Interface");
OutputFile {
path,
content: Content::Text(
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/docs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn compile_with_markdown_pages(
)
.unwrap();

for module in &mut modules {
for module in &mut modules.0 {
module.attach_doc_and_module_comments();
}

Expand All @@ -79,7 +79,7 @@ fn compile_with_markdown_pages(
super::generate_html(
&paths,
&config,
&modules,
&modules.0,
&docs_pages,
pages_fs,
SystemTime::UNIX_EPOCH,
Expand Down
1 change: 1 addition & 0 deletions compiler-core/src/exhaustiveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ impl ConstructorSpecialiser {
let parameters = parameters
.iter()
.map(|p| TypeValueConstructorField {
label: None,
type_: self.specialise_type(p.type_.as_ref()),
})
.collect_vec();
Expand Down
Loading
Loading