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

Use relative paths in generated erlang -file attributes #4193

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
20 changes: 16 additions & 4 deletions compiler-core/src/erlang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::{
},
Result,
};
use camino::Utf8Path;
use ecow::{eco_format, EcoString};
use heck::ToSnakeCase;
use im::HashSet;
Expand Down Expand Up @@ -150,13 +151,18 @@ 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,7 +223,13 @@ 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 = &module.type_info.src_path;
let src_path_relative = EcoString::from(
src_path_full
.strip_prefix(root)
.unwrap_or_else(|_| src_path_full)
.to_string(),
);

let mut needs_function_docs = false;
let mut statements = Vec::with_capacity(module.definitions.len());
Expand All @@ -227,7 +239,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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn a() { "\n" "\r" "\t" "\\" "\"" "\\^" }

-export([a/0]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec a() -> binary().
a() ->
<<"\n"/utf8>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec main() -> integer().
main() ->
A = (2 * (3 + 1)) div 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec main() -> bitstring().
main() ->
Code = <<"hello world"/utf8>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec main() -> integer().
main() ->
X = begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
1.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
1.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
1.
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ pub fn main() {

-export([function/0, main/0]).

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec function() -> integer().
function() ->
1.

-file("/root/project/test/my/mod.gleam", 8).
-file("project/test/my/mod.gleam", 8).
-spec main() -> integer().
main() ->
function().
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
fun some_module:function/0().
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
some_module:function().
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn x(y) {

-export([x/1]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec x({ok, any()} | {error, any()}) -> integer().
x(Y) ->
{ok, _} = case Y of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {

-type fn_box() :: {fn_box, fun((integer()) -> integer())}.

-file("/root/project/test/my/mod.gleam", 5).
-file("project/test/my/mod.gleam", 5).
-spec main() -> integer().
main() ->
B = {fn_box, fun(X) -> X end},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec main() -> integer().
main() ->
T = {fun(X) -> X end},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub type Box {

-type box() :: {box, integer()}.

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec main(any()) -> fun((integer()) -> box()).
main(A) ->
fun(Field@0) -> {box, Field@0} end.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ pub fn main() {

-export(['moduleInfo'/0, main/0]).

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec 'moduleInfo'() -> integer().
'moduleInfo'() ->
1.

-file("/root/project/test/my/mod.gleam", 6).
-file("project/test/my/mod.gleam", 6).
-spec main() -> integer().
main() ->
'moduleInfo'().
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
some_module:'moduleInfo'().
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
some_module:'moduleInfo'().
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ pub fn main() {

-export(['moduleInfo'/0, main/0]).

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec 'moduleInfo'() -> integer().
'moduleInfo'() ->
1.

-file("/root/project/test/my/mod.gleam", 8).
-file("project/test/my/mod.gleam", 8).
-spec main() -> integer().
main() ->
'moduleInfo'().
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
fun some_module:'moduleInfo'/0().
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 4).
-file("project/test/my/mod.gleam", 4).
-spec main() -> integer().
main() ->
some_module:'moduleInfo'().
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec main() -> float().
main() ->
case 1.0 of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn main() {

-export([main/0]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec main() -> bitstring().
main() ->
Fifteen = 15,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let x = #(100000000000000000, #(2000000000, 3000000000000, 40000000000), 50000,

-export([go/0]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec go() -> {integer(),
{integer(), integer(), integer()},
integer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn go() {

-export([go/0]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec go() -> integer().
go() ->
Y = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn go() {

-export([go/0]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec go() -> integer().
go() ->
Fifteen = 16#F,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn go() {

-export([go/0]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec go() -> integer().
go() ->
Y = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn t() { True }

-export([t/0]).

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec t() -> boolean().
t() ->
true.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Null { Null } fn x() { Null }

-type null() :: null.

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec x() -> null().
x() ->
null.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Point { Point(x: Int, y: Int) }

-type point() :: {point, integer(), integer()}.

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec x() -> point().
x() ->
{point, 4, 6},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Point { Point(x: Int, y: Int) } fn x(y) { let Point(a, b) = y a }

-type point() :: {point, integer(), integer()}.

-file("/root/project/test/my/mod.gleam", 1).
-file("project/test/my/mod.gleam", 1).
-spec x(point()) -> integer().
x(Y) ->
{point, A, B} = Y,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ pub type State{ Start(Int) End(Int) }

-type state() :: {start, integer()} | {'end', integer()}.

-file("/root/project/test/my/mod.gleam", 2).
-file("project/test/my/mod.gleam", 2).
-spec build(fun((integer()) -> I)) -> I.
build(Constructor) ->
Constructor(1).

-file("/root/project/test/my/mod.gleam", 3).
-file("project/test/my/mod.gleam", 3).
-spec main() -> state().
main() ->
build(fun(Field@0) -> {'end', Field@0} end).
Loading
Loading