Skip to content

Commit

Permalink
Fixes #4141
Browse files Browse the repository at this point in the history
  • Loading branch information
oneness committed Jan 9, 2025
1 parent c9ca4b7 commit 58bfb96
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion compiler-core/src/build/package_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ where
// we overwrite any precompiled Erlang that was included in the Hex
// package. Otherwise we will build the potentially outdated precompiled
// version and not the newly compiled version.
Erlang::new(&build_dir, &include_dir).render(io, modules)?;
Erlang::new(&build_dir, &include_dir).render(io, modules, self.root)?;

if self.compile_beam_bytecode {
written.extend(modules.iter().map(Module::compiled_erlang_path));
Expand Down
6 changes: 4 additions & 2 deletions compiler-core/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ impl<'a> Erlang<'a> {
&self,
writer: Writer,
modules: &[Module],
root: &Utf8Path
) -> Result<()> {
for module in modules {
let erl_name = module.name.replace("/", "@");
self.erlang_module(&writer, module, &erl_name)?;
self.erlang_module(&writer, module, &erl_name, root)?;
self.erlang_record_headers(&writer, module, &erl_name)?;
}
Ok(())
Expand All @@ -47,11 +48,12 @@ impl<'a> Erlang<'a> {
writer: &Writer,
module: &Module,
erl_name: &str,
root: &Utf8Path
) -> Result<()> {
let name = format!("{erl_name}.erl");
let path = self.build_directory.join(&name);
let line_numbers = LineNumbers::new(&module.code);
let output = erlang::module(&module.ast, &line_numbers);
let output = erlang::module(&module.ast, &line_numbers, root);
tracing::debug!(name = ?name, "Generated Erlang module");
writer.write(&path, &output?)
}
Expand Down
19 changes: 14 additions & 5 deletions compiler-core/src/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use regex::{Captures, Regex};
use std::sync::OnceLock;
use std::{collections::HashMap, ops::Deref, str::FromStr, sync::Arc};
use vec1::Vec1;
use camino::Utf8Path;

const INDENT: isize = 4;
const MAX_COLUMNS: isize = 80;
Expand Down Expand Up @@ -150,13 +151,14 @@ pub fn record_definition(name: &str, fields: &[(&str, Arc<Type>)]) -> String {
.to_pretty_string(MAX_COLUMNS)
}

pub fn module<'a>(module: &'a TypedModule, line_numbers: &'a LineNumbers) -> Result<String> {
Ok(module_document(module, line_numbers)?.to_pretty_string(MAX_COLUMNS))
pub fn module<'a>(module: &'a TypedModule, line_numbers: &'a LineNumbers, root: &'a Utf8Path) -> Result<String> {
Ok(module_document(module, line_numbers, root)?.to_pretty_string(MAX_COLUMNS))
}

fn module_document<'a>(
module: &'a TypedModule,
line_numbers: &'a LineNumbers,
root: &'a Utf8Path
) -> Result<Document<'a>> {
let mut exports = vec![];
let mut type_defs = vec![];
Expand Down Expand Up @@ -217,8 +219,15 @@ fn module_document<'a>(
join(type_defs, lines(2)).append(lines(2))
};

let src_path = EcoString::from(module.type_info.src_path.as_str());

let src_path_full = EcoString::from(module.type_info.src_path.as_str());
let root_str = root.as_str();
let src_path_relative = root_str
.is_empty()
.then(|| src_path_full.clone())
.unwrap_or_else(|| src_path_full.strip_prefix(root_str)
.map(|relative_path| module.name.clone() + relative_path)
.unwrap_or_else(|| src_path_full.clone()));

let mut needs_function_docs = false;
let mut statements = Vec::with_capacity(module.definitions.len());
for definition in module.definitions.iter() {
Expand All @@ -227,7 +236,7 @@ fn module_document<'a>(
&module.name,
module.type_info.is_internal,
line_numbers,
&src_path,
&src_path_relative,
) {
needs_function_docs = needs_function_docs || env.needs_function_docs;
statements.push(statement_document);
Expand Down
4 changes: 3 additions & 1 deletion compiler-core/src/erlang/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
uid::UniqueIdGenerator,
warning::TypeWarningEmitter,
};
use camino::Utf8Path;

mod bit_arrays;
mod case;
Expand Down Expand Up @@ -112,10 +113,11 @@ pub fn compile_test_project(src: &str, src_path: &str, dep: Option<(&str, &str,
extra: parsed.extra,
dependencies: vec![],
};
let root = Utf8Path::new("/my/mod");
built_module.attach_doc_and_module_comments();

let line_numbers = LineNumbers::new(src);
module(&built_module.ast, &line_numbers).unwrap()
module(&built_module.ast, &line_numbers, &root).unwrap()
}

#[macro_export]
Expand Down

0 comments on commit 58bfb96

Please sign in to comment.